1.功能
当用户未授权地理位置权限
时,引导用户开启地理位置权限,区别于之前的uni处理,uni的处理 的处理没有手机系统关闭位置权限的处理,但是uni文章中对于打开位置权限后重新返回小程序有做权限重获取,当前文章未做处理
2.无授权情况分为(截图示例为iphone手机)
2.1 手机系统未开启位置授权 无法通过代码跳转到该设置页面

2.2 微信、企微App未授权 可以通过代码跳转设置页面


2.3 小程序未授权 可以通过代码跳转设置页面

3.代码
其中$dialog为提示框,具有确认取消按钮,点击后会执行对应逻辑可以换成对应项目的组件
async function checkAndAuthLocationAuth(): Promise<boolean> {
return new Promise(async (resolve, reject) => {
const systemRes = await taro.getSystemInfo()
if (!systemRes.locationEnabled) {
$dialog({
title: '提示',
render: () => '当前系统,未开启定位权限,请在设置中开启系统定位权限!',
cancelButton: true,
confirmButton: true,
handleConfirm: async () => {
}
})
return reject(false)
} else {
const appAuthorizeSetting = await taro.getAppAuthorizeSetting()
const appAuthObj = {
title: '提示',
render: () =>
'当前App未开启定位权限,请在设置中开启App定位权限!',
cancelButton: true,
confirmButton: true,
handleConfirm: async () => {
taro.openAppAuthorizeSetting({})
}
}
switch (appAuthorizeSetting.locationAuthorized) {
case 'authorized':
const wxMiniSetting = await taro.getSetting()
if (wxMiniSetting.authSetting['scope.userLocation']) {
// 成功获取后你可以直接根据项目业务需求决定此处是否直接获取经纬度等
return resolve(true)
}else if (wxMiniSetting.authSetting['scope.userLocation'] === undefined) {
const authRes = await taro.authorize({ scope: 'scope.userLocation' })
if (authRes.errMsg === 'authorize:ok') {
// 成功获取后你可以直接根据项目业务需求决定此处是否直接获取经纬度等
resolve(true)
} else {
// 拒绝后这里可以直接再次提示以及一些业务逻辑处理
}
}
else {
$dialog({
title: '提示',
render: () =>
'您的账号尚未获取或已拒绝“获取位置信息”,暂时无法使用该功能,是否确认授权?',
cancelButton: true,
confirmButton: true,
handleConfirm: async () => {
taro.openSetting({})
},
})
}
return reject(false)
case 'denied':
$dialog(appAuthObj)
return reject(false)
case 'not determined':
$dialog(appAuthObj)
return reject('')
default:
break;
}
}
})
}
使用示例
await checkAndAuthLocationAuth()
// 业务逻辑 如果未成功获取则不会走到业务逻辑代码中
...
...
// 业务逻辑结束