在微信小程序中,當?shù)谝淮芜M入小程序是會請求授權的雨效,如果用戶拒絕的授權之后再次進入小程序是不會再請求授權的噩茄,這種情況無疑會影響小程序的正常功能囊颅。
所以我們需要用到小程序的授權設置功能
wx.openSetting(Object object)
wx.openSetting
當需要位置權限時炉峰,但又被拒絕之后就需要重新調(diào)起
使用wx.openSetting方法跳轉(zhuǎn)到設置頁畏妖,在調(diào)用成功的回調(diào)中判斷相應權限是否可用
wx.openSetting(){
success(res){
//判斷授權信息
if (res.authSetting["scope.userLocation"]) {
that.onLoad()
}
}
}
需要注意的是判斷需要res.authSetting["scope.userLocation"]格式才正確,下面是官方文檔的格式
格式
下面為完整代碼疼阔,業(yè)務代碼部分請自行 添加
// 獲取位置
wx.getLocation({
success: function(res) {
//業(yè)務代碼
}, 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