operator() {
uni.showToast({
title: "点击保存相册",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
// 先检查用户是否已拒绝过授权
uni.getSetting({
success: (res) => {
uni.showToast({
title: "调用getSetting成功",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
if (res.authSetting['scope.writePhotosAlbum'] === false) {
uni.showModal({
content: '需要相册权限才能保存,是否前往设置?',
confirmText: '去设置',
success: (res) => {
if (res.confirm) uni.openSetting();
}
});
} else {
// 未拒绝过,调用 authorize
uni.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
uni.showToast({
title: "调用authorize成功",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
uni.showActionSheet({
itemList: ["保存图片到相册"],
success: (res) => {
uni.showToast({
title: "调用showActionSheet成功",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
if (res.tapIndex === 0) {
uni.showLoading({
title: "保存中..."
});
uni.downloadFile({
url: this.code,
success: (result) => {
uni.showToast({
title: "调用downloadFile成功",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
uni.saveImageToPhotosAlbum({
filePath: result.tempFilePath,
success: () => {
uni.showToast({
title: "调用saveImageToPhotosAlbum成功",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
uni.hideLoading();
uni.showToast({
title: "保存成功"
});
},
fail: (err) => {
uni.showToast({
title: "调用saveImageToPhotosAlbum失败",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
uni.hideLoading();
uni.showToast({
title: "保存失败,请重试",
icon: 'none'
});
}
});
},
fail: (err) => {
uni.showToast({
title: "调用downloadFile失败",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
uni.hideLoading();
uni.showToast({
title: "下载图片失败",
icon: 'none'
});
}
});
}
},
fail: (err) => {
uni.showToast({
title: "调用showActionSheet失败",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
console.error('操作菜单失败', err);
}
});
},
fail: (err) => {
uni.showToast({
title: "调用authorize失败",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
if (err.errMsg.includes('auth deny')) {
uni.showModal({
content: '需要相册权限才能保存,是否前往设置?',
confirmText: '去设置',
success: (res) => {
if (res.confirm) uni.openSetting();
}
});
}
}
});
}
},
fail: (err) => {
uni.showToast({
title: "调用getSetting失败",
icon: "none", // 不显示图标
duration: 2000 // 2秒后自动消失
});
console.error('获取设置失败', err);
}
});
}