policeSecurity/collect_information/src/utils/index.ts

68 lines
2.5 KiB
TypeScript

import Taro from "@tarojs/taro";
/**
* 授权综合逻辑
* @param {*} scope 权限代表
* @param {*} successCallback 成功回调
* @param {*} failCallback 失败回调
*/
export function pullAuth(scope, successCallback, failCallback) {
const map = new Map([
['scope.userInfo', '用户信息'],
['scope.userLocation', '地理位置'],
['scope.userLocationBackground', '后台定位'],
['scope.address', '通信地址'],
['scope.record', '录音功能'],
['scope.writePhotosAlbum', '保存到相册'],
['scope.camera', '摄像头'],
['scope.invoice', '获取发票'],
['scope.invoiceTitle', '发票抬头'],
['scope.werun', '微信运动步数'],
])
Taro.getSetting({
success(res) {
if (!res.authSetting[scope]) {
Taro.authorize({
scope,
success(){ // 允许授权
successCallback()
},
fail(){ // 拒绝授权
if (map.has(scope)) {
let word = map.get(scope)
Taro.showModal({
content:`检测到您没打开${word}权限,是否去设置打开?`,
confirmText: "确认",
cancelText:'取消',
success: (res) => {
if(res.confirm){
Taro.openSetting({
success: (res) => {
successCallback()
},
fail: () => {
failCallback()
},
})
}else{
failCallback()
}
}
})
} else {
Taro.showToast({
title: '无此授权功能'
})
failCallback()
}
}
})
} else {
successCallback()
}
}
})
}