Files
TestRestructureWechatLymark…/src/use/useCheckAuthorize.tsx
T

68 lines
1.7 KiB
TypeScript

import Taro from '@tarojs/taro'
import { memo, useCallback, useState } from 'react'
import { alert } from '@/common/common'
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'
interface Param {
scope: Scope
msg: string // 检查不通过时警告
}
export default ({ scope, msg }: Param) => {
// 这个hook微信授权检查授权
const check = useCallback(() => {
return new Promise((resolve, reject) => {
Taro.getSetting({
success: (res) => {
if (res.authSetting[scope]) {
resolve(true)
}
else if (res.authSetting[scope] === undefined) {
Taro.authorize({
scope,
success() {
resolve(true)
},
fail() {
alert.none(msg)
// eslint-disable-next-line prefer-promise-reject-errors
reject(false)
},
})
}
else {
Taro.openSetting({
success(res) {
if (res.authSetting[scope]) {
resolve(true)
}
else {
alert.none(msg)
// eslint-disable-next-line prefer-promise-reject-errors
reject(false)
}
},
})
}
},
})
})
}, [scope])
return {
check,
}
}