Commit 7d71d83f authored by mqf_0707's avatar mqf_0707

授权优化

parent 005c5202
...@@ -42,5 +42,9 @@ export default { ...@@ -42,5 +42,9 @@ export default {
version: '*', version: '*',
provider: '3000000026642582' provider: '3000000026642582'
} }
},
memberBenefit: {
version: '*',
provider: '3000000043103783'
} }
} }
\ No newline at end of file
...@@ -17,7 +17,7 @@ Component({ ...@@ -17,7 +17,7 @@ Component({
this.postMessage(data.type, data) this.postMessage(data.type, data)
} }
}, },
didUnMount() { didUnmount() {
// 页面被关闭 // 页面被关闭
this.main && this.main.destroy(); this.main && this.main.destroy();
}, },
......
import { useEffect, useRef, useState } from 'react' import { useEffect, useRef, useState } from 'react'
import Taro, { useDidShow, showToast } from '@tarojs/taro' import Taro, { useDidShow, showToast } from '@tarojs/taro'
import { getSetting, openSetting, authorize, getAuthUserInfo } from 'tbcc-sdk-ts/lib/core/tb' import { getSetting, openSetting, authorize, getAuthUserInfo } from 'tbcc-sdk-ts/lib/core/tb'
import { useThrottle } from '@/hooks/useThrottle'
// 授权 // 授权
export function useAuth(authSuccess = () => {}) { export function useAuth(authSuccess = () => {}) {
const [visible, setVisible ] = useState(false) const [visible, setVisible ] = useState(false)
const goToOpenSetting = useRef(false) const goToOpenSetting = useRef(false)
// 授权信息调用是否完成 防止用户调用了授权API 未弹出再次点击
const completeHandle = useRef(true)
// 获取授权信息 // 获取授权信息
const onHandleSetting = async () => { const onHandleSetting = useThrottle(async () => {
const res = await getSetting() const res = await getSetting()
if(!res.authSetting?.userInfo) { if(!res.authSetting?.userInfo) {
const authResult = await authorize().catch(err => { const authResult = await authorize().catch(err => {
setVisible(true) setVisible(true)
showToast({ title: '请先授权,授权成功后才可参与活动' }) showToast({ title: '请先授权,授权成功后才可参与活动' })
completeHandle.current = true
}) })
if(authResult) { if(authResult) {
authUserInfo() authUserInfo()
...@@ -20,14 +24,16 @@ export function useAuth(authSuccess = () => {}) { ...@@ -20,14 +24,16 @@ export function useAuth(authSuccess = () => {}) {
}else { }else {
authUserInfo() authUserInfo()
} }
} })
// 打开授权设置 // 打开授权设置
const onOpenSetting = async () => { const onOpenSetting = useThrottle(async () => {
if(!completeHandle.current) return;
goToOpenSetting.current = true goToOpenSetting.current = true
showToast({ title: '打开授权开关,授权成功后才可参与活动', duration: 1000, success: () => openSetting() }) showToast({ title: '打开授权开关,授权成功后才可参与活动', duration: 1000, success: () => openSetting() })
} })
// 获取用户授权信息 // 获取用户授权信息
const authUserInfo = async() => { const authUserInfo = async() => {
completeHandle.current = true
const info = await getAuthUserInfo() const info = await getAuthUserInfo()
if (info) { if (info) {
authSuccess(info) authSuccess(info)
...@@ -42,6 +48,7 @@ export function useAuth(authSuccess = () => {}) { ...@@ -42,6 +48,7 @@ export function useAuth(authSuccess = () => {}) {
if(goToOpenSetting.current) { if(goToOpenSetting.current) {
// 重新授权 // 重新授权
onHandleSetting() onHandleSetting()
completeHandle.current = false
goToOpenSetting.current = false goToOpenSetting.current = false
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment