文档
代码
wx.getSetting({
success(res) {
console.log('wx.getSetting:', res)
/**
* res.authSetting['scope.userLocation'] === true 同意过授权
* res.authSetting['scope.userLocation'] === false 拒绝过授权
* res.authSetting['scope.userLocation'] === undefined 从未同意或者拒绝过授权
*/
if (res.authSetting['scope.userLocation'] === undefined) {
wx.authorize({
scope: 'scope.userLocation',
success (res) {
console.log('authorize success:', res)
},
})
}else if (res.authSetting['scope.userLocation'] === false) {
wx.showModal({
title: '',
content: '去小程序设置页面设置balabala',
showCancel: true,
cancelText: '取消',
cancelColor: '#000000',
confirmText: '确定',
confirmColor: '#3CC51F',
success: (result) => {
if(result.confirm){
wx.openSetting({
success (res) {
console.log('wx.openSetting success:', res.authSetting)
},
fail: (err)=>{
console.log('wx.openSetting fail:', err)
},
complete: (info)=>{
console.log('wx.openSetting complete:', info)
},
})
}
},
})
} else {
wx.getLocation({
type: 'wgs84',
success (res) {
console.log('wx.location success:', res)
},
fail (err) {
console.log('wx.location fail:', err)
},
complete(aa) {
console.log('wx.location complete:', aa)
},
})
}
},
})
效果图
授权弹窗
去设置弹窗
注意事项
1、wx.openSetting
- 3.0 版本开始,用户发生点击行为后,才可以跳转打开设置页,管理授权信息
- 真机调试才能打开设置页面
- 只会显示小程序已经向用户申请过的权限
2、wx.getSetting
返回值中只会出现小程序已经向用户申请过的权限
3、app.json文件中配置位置信息的用途说明
"permission": {
"scope.userLocation": {
"desc": "您的位置信息将用于定位"
}
}
4、弹窗出现机制
- 一旦用户选择了拒绝或者允许该权限,那么授权关系就会被记录在后台,直到用户主动删除小程序,之后再次调用该API时都不会再弹出权限申请框。
- 解决方式:对于用户拒绝授权的情况调用wx.openSetting跳转小程序设置页面,引导用户手动开启该权限
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。