TestEShopDist/src/use/useCheckAuthorize.tsx
2022-05-24 20:32:20 +08:00

51 lines
1.8 KiB
TypeScript

import { alert } from "@/common/common";
import Taro from "@tarojs/taro";
import { memo, useCallback, useState } from "react";
type Scope = 'scope.userLocation'|'scope.userLocation'|'scope.record'|'scope.camera'|'scope.bluetooth'|'scope.writePhotosAlbum'|'scope.addPhoneContact'|'scope.addPhoneCalendar'|'scope.werun'|'scope.address'|'scope.invoiceTitle'|'scope.invoice'|'scope.userInfo'
type Param = {
scope: Scope,
msg: string //检查不通过时警告
}
export default ({scope, msg}: Param) => {
//检查授权
const check = useCallback(() => {
return new Promise((reslove, reject) => {
Taro.getSetting({
success: (res) => {
if(res.authSetting[scope]) {
reslove(true)
} else if (res.authSetting[scope] === undefined) {
Taro.authorize({
scope: scope,
success() {
reslove(true)
},
fail() {
alert.none(msg)
reject(false)
}
})
} else {
Taro.openSetting({
success(res) {
if(res.authSetting[scope]) {
reslove(true)
} else {
alert.none(msg)
reject(false)
}
}
})
}
}
})
})
}, [scope])
return {
check,
}
}