- 在app.json文件中配置
permission
, 和pages
同级
"permission": {
"scope.userLocation": {
"desc": "申请获取您的定位信息用于判断您是否进入签到范围。"
}
},
- 具体授权
btn () {
// wx.getSystemInfo(Object object) 可以获取app是否获取定位权限
const that = this
wx.getLocation({
success (res) {
console.log(res)
// 调用业务逻辑
},
fail () {
wx.getSetting({
success (res) {
if (!res.authSetting['scope.userLocation']) {
wx.showModal({
title: '提示',
content: '若拒绝授权将无法判断您是否已进入签到范围。',
success (tip) {
if (tip.confirm) {
wx.openSetting({
success (data) {
if (data.authSetting['scope.userLocation']) {
wx.getLocation({
success (res) {
console.log(res)
// 调用业务逻辑
}
})
} else {
wx.showToast({
title: '授权失败',
icon: 'none',
duration: 1000
})
}
},
fail (res) {
wx.showToast({
title: '调起小程序权限设置界面失败',
icon: 'none',
duration: 1000
})
console.log(res, '2333')
}
})
}
}
})
} else {
wx.showModal({
title: '提示',
content: '您手机定位功能没有开启, 请在系统设置中打开定位服务。',
showCancel: false
})
}
},
fail (res) {
wx.showToast({
title: 'wx.getSetting获取用户之前授权的信息失败',
icon: 'none',
duration: 1000
})
}
})
}
})
},