本篇文章记录一下当微信小程序在第一次授权被拒绝后,如何重新弹出授权。
我项目中用到了wx.chooseLocation接口,但是需要scope.userLocation授权
当用户第一次用时会正常弹出授权窗口,但是用户选择了拒绝,再次调用这个接口时,提示:“authorize:fail:auth deny”
因为用户已经明确拒绝了,所以再次调用就会直接失败。(应该是一定时间内不能重复发起授权)
我这里的处理方法是,直接打开小程序设置中的授权窗口,让用户自己去设置
这里用到了wx.openSetting
代码如下
wx.openSetting({
withSubscriptions: true,
success(res) {
console.log(res)
if (res.authSetting["scope.userLocation"]==true) {
//用户已经打开了位置权限,开始执行wx.chooseLocation
....
}
}
})
完整代码
//获取定位
getLocation: function () {
let _this = this;
//获取用户的授权
wx.getSetting({
success(res) {
// 判断定位的授权是否有权限
if (!res.authSetting['scope.userLocation']) {
//无权限去申请scope.userLocation权限
wx.authorize({
scope: 'scope.userLocation',
success() {
//申请成功
_this.onGetLocation();
},
fail(errMsg) {
//申请失败,准备打开openSetting
wx.showModal({
title: '需要获取您的地理位置授权,现在去授权吗?',
content: '',
showCancel: true,
cancelText: '再看看',
cancelColor: '#000000',
confirmText: '去授权',
confirmColor: '#3CC51F',
success: (result) => {
if (result.confirm) {
wx.openSetting({
withSubscriptions: true,
success(res) {
console.log(res)
if (res.authSetting["scope.userLocation"] == true) {
_this.onGetLocation();
}
}
})
}
},
fail: () => { },
complete: () => { }
});
//wx.showToast({ title: JSON.stringify(errMsg), icon: 'none' })
}
})
} else {
_this.onGetLocation();
}
}
})
},