小程序-授权
目录
小程序 · 授权
微信小程序拒绝授权后再次授权
当用户点击授权弹框的拒绝时,再次使用功能需要再次授权,此时调用wx.authorize方法会返回错误信息errMsg: “authorize:fail:auth deny”,指用户已经拒绝授权,不会在弹出授权页面。
错误码:-12006,auth deny
问题原因
如果用户拒绝授权后,短期内调用不会出现弹窗,而是直接进入 fail 回调。如果是开发环境,请点击开发工具左侧 缓存-清除授权数据;如果是手机,请进入小程序后点击右上菜单-关于xx-右上角菜单-设置中进行权限的手动设置,或删除小程序后重新添加。
解决方法
使用 wx.getSetting 获取用户当前的授权状态,调用 wx.openSetting 打开设置界面,引导用户开启授权
getWXSetting() {
// 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
// , 'scope.writePhotosAlbum' 'scope.userInfo', 'scope.userLocation',
let that = this
wx.getSetting({
success(res) {
console.log('getSetting', res.authSetting)
if (!res.authSetting['scope.userLocation']) {
wx.authorize({
scope: 'scope.userLocation',
success(res) {
// 用户已经同意小程序使用功能,后续调用接口不会弹窗询问
// 获取位置信息
that.getLocation()
console.log('1', res)
},
fail(res) {
errMsg: 'authorize:fail auth deny'
wx.showModal({
title: '提示',
content: '检测到您没打开获取信息功能权限,是否去设置打开?',
success(res) {
if (res.confirm) {
console.log('用户点击确定')
wx.openSetting({
success: (res) => {
console.log(res)
},
})
} else if (res.cancel) {
console.log('用户点击取消')
// 拒绝跳转介绍页
wx.redirectTo({
url: '/pages/home/introduce/introduce',
})
}
},
})
console.log('2', res)
},
})
} else {
that.getLocation()
}
},
})
},
参考: