在微信小程序中,当第一次进入小程序是会请求授权的,如果用户拒绝的授权之后再次进入小程序是不会再请求授权的,这种情况无疑会影响小程序的正常功能。
所以我们需要用到小程序的授权设置功能
wx.openSetting(Object object)
wx.openSetting
当需要位置权限时,但又被拒绝之后就需要重新调起
使用wx.openSetting方法跳转到设置页,在调用成功的回调中判断相应权限是否可用
wx.openSetting(){
success(res){
//判断授权信息
if (res.authSetting["scope.userLocation"]) {
that.onLoad()
}
}
}
需要注意的是判断需要res.authSetting["scope.userLocation"]格式才正确,下面是官方文档的格式
格式
下面为完整代码,业务代码部分请自行 添加
// 获取位置
wx.getLocation({
success: function(res) {
//业务代码
}, fail(){
wx.showModal({
title: '提醒',
content: '您拒绝了位置授权,将无法使用大部分功能,点击确定重新获取授权',
success(res){
//如果点击确定
if(res.confirm) {
wx.openSetting({
success(res){
// console.log(res.authSetting)
// console.log(res.authSetting["scope.userLocation"])
//如果同意了位置授权则userLocation=true
if (res.authSetting["scope.userLocation"]){
that.onLoad()
}
}
})
}
}
})
}
})
——————————————————————————————
便分享首好听的歌 Into You——J_ust-First kiss