/**
* 保存图片到本地相册
*/
function handleSaveImageClick() {
// 图片的URL
const imageUrl ="https:mgs.com/icon/客服二维码.jpg";
// 获取用户是否开启用户授权相册
Taro.getSetting({
success(res) {
console.log("res", res);
// 如果没有则获取授权
if (!res.authSetting["scope.writePhotosAlbum"]) {
Taro.authorize({
scope: "scope.writePhotosAlbum",
success() {
Taro.getImageInfo({
src: imageUrl,
success: res => {
const path = res.path;
Taro.saveImageToPhotosAlbum({
filePath: path,
success() {
Taro.showToast({
title: "保存成功"
});
},
fail() {
Taro.showToast({
title: "保存失败",
icon: "none"
});
}
});
}
});
},
fail() {
Taro.showModal({
title: "提示",
confirmText: "确认",
cancelText: "取消",
content: "是否开启相册权限",
success(res) {
if (res.confirm) {
Taro.openSetting();
} else if (res.cancel) {
}
}
});
}
});
} else {
// 有则直接保存
Taro.getImageInfo({
src: imageUrl,
success: res => {
const path = res.path;
Taro.saveImageToPhotosAlbum({
filePath: path,
success() {
Taro.showToast({
title: "保存成功"
});
},
fail() {
Taro.showToast({
title: "保存失败",
icon: "none"
});
}
});
}
});
}
},
fail() {
Taro.showToast({
title: "保存失败",
icon: "none"
});
}
});
}