針對微信小程序拉起授權(quán)并拒絕授權(quán)后的再次授權(quán)處理
小程序在獲取某些用戶隱私數(shù)據(jù)的時候通常需要經(jīng)過授權(quán),然而有的用戶會點擊拒絕,針對這種情況需要進行處理下,方便用戶下次使用的時候能夠正常拉起授權(quán)
比如我們?nèi)绻@取用戶的位置,如果我們直接調(diào)用wx.getLocation(),然而用戶點擊了拒絕,怎么辦?此時我們需要在調(diào)用wx.getLocation()之前先要去調(diào)用一下wx.getSetting(),看看authSetting里面是否有’scope.userLocation’,有的話代表之前拉起過授權(quán),如果值是false代表拒絕授權(quán),需要調(diào)用wx.getSetting()接口手動設(shè)置授權(quán),如果沒有’scope.userLocation’,代表沒拉起過授權(quán)文章來源地址http://www.zghlxwxcb.cn/news/detail-624294.html
wx.getSetting({
success(res0) {
console.log('查看是否吊起過授權(quán)', res0)
// 查看是否吊起過授權(quán)
if (!Object.keys(res0.authSetting).includes('scope.userLocation')) {
wx.getLocation({
type: 'wgs84',
success(res1) {
console.log('吊起過授權(quán)---再次獲取定位', res1)
that.getAddress(res1)
},
fail() {
console.log('吊起過授權(quán)---失敗')
that.getAddress({ longitude: '106.713478', latitude: '26.578343' })
}
})
return
}
// 已經(jīng)吊起過授權(quán)并且拒絕授權(quán)
if (!res0.authSetting['scope.userLocation']) {
wx.showModal({
title: '您未開啟地理位置授權(quán)',
content: '為了給您提供更好的服務(wù),請您授權(quán)地理位置,謝謝',
success: res2 => {
if (res2.confirm) {
wx.openSetting({
success(res3) {
console.log('吊起過授權(quán)---并且拒絕--重新設(shè)置授權(quán)', res3)
// 成功授權(quán)地理位置
if (res3.authSetting['scope.userLocation']) {
wx.getLocation({
type: 'wgs84',
success(res4) {
console.log('重新上設(shè)置授權(quán)成功---再次獲取定位', res4)
that.getAddress(res4)
},
fail() {
console.log('重新授權(quán)失敗')
that.getAddress({ longitude: '106.713478', latitude: '26.578343' })
}
})
} else {
// 未授權(quán)地理位置,默認顯示貴陽
console.log('重新授權(quán)---未允許,默認顯示貴陽')
that.getAddress({ longitude: '106.713478', latitude: '26.578343' })
}
}
})
} else {
console.log('拒絕授權(quán)地理位置,默認顯示貴陽')
that.getAddress({ longitude: '106.713478', latitude: '26.578343' })
}
}
})
} else {
wx.getLocation({
type: 'wgs84',
success(res) {
console.log('吊起過授權(quán),直接獲取位置')
that.getAddress(res)
},
fail() {
console.log('授權(quán)失敗,默認顯示貴陽')
that.getAddress({ longitude: '106.713478', latitude: '26.578343' })
}
})
}
}
})
文章來源:http://www.zghlxwxcb.cn/news/detail-624294.html
到了這里,關(guān)于針對微信小程序拉起授權(quán)并拒絕授權(quán)后的再次授權(quán)處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!