You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
chatai/dist/dev/mp-weixin/utils/file.js

61 lines
2.0 KiB

"use strict";
const common_vendor = require("../common/vendor.js");
const downloading = common_vendor.ref(false);
async function saveImageToPhotosAlbum(url) {
if (!url)
return common_vendor.index.$u.toast("图片错误");
if (downloading.value) {
return;
}
common_vendor.index.showLoading({ title: "下载中" });
downloading.value = true;
try {
const res = await common_vendor.index.downloadFile({ url, timeout: 1e4 });
await common_vendor.index.saveImageToPhotosAlbum({
filePath: res.tempFilePath
});
common_vendor.index.hideLoading();
common_vendor.index.showToast({
title: "保存成功",
icon: "success"
});
downloading.value = false;
} catch (error) {
common_vendor.index.hideLoading();
console.log(error);
downloading.value = false;
if (error.errMsg == "saveImageToPhotosAlbum:fail cancel") {
common_vendor.index.$u.toast("取消保存");
return;
}
if (error.errMsg == "downloadFile:fail fail:timeout") {
common_vendor.index.$u.toast("下载图片超时,请重新下载");
return;
}
if (error.errMsg == "saveImageToPhotosAlbum:fail auth deny") {
const res = await common_vendor.index.showModal({
title: "提示",
content: "您关闭了权限,请前往设置打开权限"
});
if (res.confirm) {
const setting = await common_vendor.index.openSetting();
if (setting.authSetting["scope.writePhotosAlbum"]) {
common_vendor.index.showModal({
title: "提示",
content: "获取权限成功,再次保存图片即可成功",
showCancel: false
});
} else {
common_vendor.index.showModal({
title: "提示",
content: "获取权限失败,无法保存到相册",
showCancel: false
});
}
}
return;
}
common_vendor.index.$u.toast(error.errMsg || "保存失败");
}
}
exports.saveImageToPhotosAlbum = saveImageToPhotosAlbum;