1.前言
微信进行授权操作,如果用户点击了拒绝,则再次调用该方法就不会出现对应的授权窗口。
可使用以下接口重新调取授权窗口。
wx.openSetting(OBJECT)
设置:调起客户端小程序设置界面,返回用户设置的操作结果;基础库 1.1.0 开始支持,低版本需做“兼容处理”
2.代码
wx.getSetting({
success(res) {
console.log("保存图片授权状态:" + res.authSetting['scope.writePhotosAlbum']);
if (!res.authSetting['scope.writePhotosAlbum']) { //未授权
wx.authorize({
scope:'scope.writePhotosAlbum',
success() {
console.log('授权成功');
wx.downloadFile({
url: imgSrc,
success:function (res1) {
console.log(res1);
wx.saveImageToPhotosAlbum({
filePath: res1.tempFilePath,
success:function (data) {
console.log(data);
self.afterSaveImage();
self.btnState[5] = 0;
},
fail:function (err) {
console.log(err);
self.btnState[5] = 0;
}
});
}
});
},
fail() {
console.log("授权失败");
wx.showModal({
title: '提示',
content: '点击确定,保存图片到相册。',
success:function(res){
if (res.confirm){
wx.openSetting({
success(res){
console.log("重新获得保存图片授权状态");
if (res.authSetting["scope.writePhotosAlbum"]){ //如果用户重新同意了授权登录
wx.downloadFile({
url: imgSrc,
success:function (res1) {
console.log(res1);
wx.saveImageToPhotosAlbum({
filePath: res1.tempFilePath,
success:function (data) {
console.log(data);
self.afterSaveImage();
self.btnState[5] = 0;
},
fail:function (err) {
console.log(err);
self.btnState[5] = 0;
}
});
}
});
}
},
fail(){
console.log("重新获得保存图片授权状态失败");
}
})
}
}
})
}
});
}else{ //已授权
wx.downloadFile({
url: imgSrc,
success:function (res1) {
console.log(res1);
wx.saveImageToPhotosAlbum({
filePath: res1.tempFilePath,
success:function (data) {
console.log(data);
self.afterSaveImage();
self.btnState[5] = 0;
},
fail:function (err) {
console.log(err);
self.btnState[5] = 0;
}
});
}
});
}
},
fail() {
console.log("获得保存图片授权状态失败");
}
});