二次授权因wx.opensetting官方已废弃,所以这里用最新的方案按钮解决
代码如下
wxml:
<button wx:if="{{authBtn}}" open-type="openSetting" bindopensetting='checkAuth'>重新授权相册</button>
data = {
authBtn: false,
}
methods = {
saveImg(data) {
let that = this
wx.getSetting({ //获取设置
success(res) {
wx.authorize({//获取相册授权信息
scope: 'scope.writePhotosAlbum',
success() {//已授权
wx.saveImageToPhotosAlbum({ //保存到相册
filePath: `images/morePage/code-${data}.jpg`,
success() {
wx.showToast({
title: '保存成功',
icon: 'success',
})
},
fail() {// 一般保存出错会出现(可删除)
that.imgAuthBtn = true
wx.showToast({
title: '未知错误请反馈',
icon: 'none',
})
}
})
},
fail() {//未授权
that.imgAuthBtn = true
that.$apply()
wx.showToast({
title: '保存失败请授权',
icon: 'none',
})
}
})
}
})
},
checkImgAuthFun(res) { //二次以上检验是否授权图片
let that = this;
if (!res.detail.authSetting['scope.writePhotosAlbum']) { //二次以上授权, 如果未授权
that.imgAuthBtn = true //显示授权按钮
wx.showToast({
title: '授权失败',
icon: 'none',
})
} else {
that.imgAuthBtn = false //不显示授权按钮
wx.showToast({
title: '授权成功',
icon: 'none',
})
}
},
}