fix: 修复保存网络图片

This commit is contained in:
2025-12-06 23:32:16 +08:00
parent 0a864fd400
commit bbb5170802
2 changed files with 41 additions and 26 deletions

View File

@@ -1,35 +1,48 @@
import Taro from "@tarojs/taro";
export function saveImage(url) {
Taro.getSetting().then(async (res) => {
if (!res.authSetting["scope.writePhotosAlbum"]) {
// 下载网络图片,返回本地 temp 文件
const download = () => {
return Taro.downloadFile({ url }).then(res => {
if (res.statusCode === 200) {
return res.tempFilePath
}
throw new Error("图片下载失败")
})
}
const save = async () => {
try {
const filePath = await download()
await Taro.saveImageToPhotosAlbum({ filePath })
Taro.showToast({ title: "保存成功" })
} catch (e) {
console.error(e)
Taro.showToast({ title: "图片保存失败", icon: "none" })
}
}
Taro.getSetting().then(res => {
const authorized = res.authSetting["scope.writePhotosAlbum"]
if (!authorized) {
Taro.authorize({
scope: "scope.writePhotosAlbum",
success: async () => {
try {
Taro.saveImageToPhotosAlbum({ filePath: url });
Taro.showToast({ title: "保存成功" });
} catch (e) {
Taro.showToast({ title: "图片保存失败", icon: "none" });
}
success: () => {
save()
},
fail: () => {
Taro.showModal({
title: "提示",
content: "需要开启相册权限才能保存图片",
success: (r) => {
if (r.confirm) Taro.openSetting();
},
});
},
});
if (r.confirm) Taro.openSetting()
}
})
}
})
} else {
try {
Taro.saveImageToPhotosAlbum({ filePath: url });
Taro.showToast({ title: "保存成功" });
} catch (e) {
Taro.showToast({ title: "图片保存失败", icon: "none" });
}
save()
}
});
}
})
}