小程序用戶取消授權(quán)地理位置后
wx.chooseLocation
需要授權(quán),如果第一次就不允許,則一直調(diào)用wx.chooseLocation的fail方法。
wx.getSetting
獲取用戶的當(dāng)前設(shè)置,返回值中只會(huì)出現(xiàn)小程序已經(jīng)向用戶請(qǐng)求過(guò)的權(quán)限。
思路:
在wx.chooseLocation的fail方法里調(diào)用wx.getSetting就漾。
代碼:
const that = this
wx.chooseLocation({
success: function(res) {
},
fail:function(res){
// console.log(res)
wx.getSetting({
success:function(res){
// console.log(res)
if (!res.authSetting['scope.userLocation']){
wx.showModal({
title: '是否授權(quán)當(dāng)前位置',
content: '請(qǐng)確認(rèn)授權(quán),否則此功能無(wú)法使用',
success:function(msg){
if(msg.confirm){
wx.openSetting({
success:function(e){
// console.log(e)
if (e.authSetting['scope.userLocation'] === true) {
wx.showToast({
title: '授權(quán)成功',
icon: 'success',
duration: 1000
})
wx.chooseLocation({
success: function(data) {
},
})
}
}
})
}else if(msg.cancel){
wx.showToast({
title: '調(diào)用授權(quán)窗口失敗',
icon: 'none',
duration: 1000
})
}
}
})
}
}
})
},
})
}