fix: 修复保存网络图片
This commit is contained in:
@@ -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()
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user