一、API特点
1、wx.saveImageToPhotosAlbum,保存图片到系统相册
特点:当用户拒绝后,下次保存时,会直接执行fail中的代码,不会重新调起授权窗口。
2、 wx.getSetting 和 wx.openSetting
(1)wx.getSetting 获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限
(2)wx.openSetting 调起客户端小程序设置界面,返回用户设置的操作结果。设置界面只会出现小程序已经向用户请求过的权限
特点:必须有事件才会触发,例如open-type、bind tap、bindlongpress等。
3、wx.authorize , 提前向用户发起授权请求。调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能或获取用户的某些数据,但不会实际调用对应接口
特点:授权拒绝后,短时间内不会再调起,会直接执行fail中的代码。
二、解决方法
<image src="" data-src="" bindlongpress="saveImage"></image>
saveImage(e) {
let that = this;
wx.getSetting({
success(settingdata) {
if(settingdata.authSetting.hasOwnProperty('scope.writePhotosAlbum') &&
settingdata.authSetting['scope.writePhotosAlbum'] === false) {
that.getAuthorize();
} else {
that.saveImageData();
}
},
fail:function(data) {
}
})
},
getAuthorize(){
let that = this;
wx.authorize({
scope: "scope.writePhotosAlbum",
success(res) {
console.log(res,'成功');
},
fail(res) {
that.openSettingMethod();
}
})
},
openSettingMethod(){
wx.showModal({
title: '提示',
content: '是否打开设置页面',
success (res) {
if (res.confirm) {
wx.openSetting({
success: function(res){}
})
} else if(res.cancel){
wx.showToast({
title: '已取消',
icon: 'none',
duration: 1000
})
}
}
})
},
saveImageData() {
let url = '';
wx.saveImageToPhotosAlbum({
filePath:url,
success: function(res) {
wx.showToast({
title: '已保存至相册,扫码关注',
icon: 'none',
duration: 1000
})
},
fail: function(res) {
wx.showToast({
title: '保存失败',
icon: 'none',
duration: 1000
})
}
})
}