| | |
| | | import { useUserStore } from '@/stores/modules/user'; |
| | | import Taro from '@tarojs/taro'; |
| | | import { object2query } from '@life-payment/utils'; |
| | | import * as userServices from '@life-payment/services/api/User'; |
| | | import { useQuery } from '@tanstack/vue-query'; |
| | | import { MaybeRef } from 'vue'; |
| | | import { useRefeshDidShow } from './infiniteLoading'; |
| | | import { LoginFormTabs } from '@/subpackages/login/constants'; |
| | | import { useLoginedJump } from './login'; |
| | | import { useLifeRechargeContext, APIgetLifePayWxIndentityParams } from '@life-payment/core-vue'; |
| | | import { isInWeChat } from '@/utils/env'; |
| | | |
| | | export function useUser() { |
| | | const userStore = useUserStore(); |
| | | |
| | | const { userDetail, userInfo, locationCity } = storeToRefs(userStore); |
| | | const { |
| | | userDetail, |
| | | userInfo, |
| | | locationCity, |
| | | virtualUserId, |
| | | virtualPhoneNumber, |
| | | wxCode, |
| | | wxOpenId, |
| | | } = storeToRefs(userStore); |
| | | |
| | | function updateUserInfo() { |
| | | return userStore.getCurrentUserInfo(); |
| | |
| | | userDetail: userDetail, |
| | | updateUserInfo, |
| | | locationCity, |
| | | virtualUserId, |
| | | virtualPhoneNumber, |
| | | wxCode, |
| | | wxOpenId, |
| | | }; |
| | | } |
| | | |
| | | export function useIsLogin() { |
| | | const { user, userDetail } = useUser(); |
| | | return computed(() => !!user.value); |
| | | // return user?.isPersonal ? !!user : !!user && !!enterpriseInfo; |
| | | const { virtualUserId } = useUser(); |
| | | // return computed(() => !!user.value); |
| | | return computed(() => !!virtualUserId.value); |
| | | } |
| | | |
| | | type UseAuthOptions = { |
| | |
| | | const isAuth = computed(() => !needAuth || isLogin.value); |
| | | |
| | | const router = Taro.useRouter(); |
| | | useInitWeixinJSBridge(); |
| | | |
| | | Taro.useReady(async () => { |
| | | if (isLogin.value && userStore.firstGetUserDetail) { |
| | | userStore.getCurrentUserInfo(); |
| | | // userStore.getCurrentUserInfo(); |
| | | } |
| | | if (needAuth && !isLogin.value) { |
| | | Taro.navigateTo({ |
| | | url: `${RouterPath.authorization}?redirect=${router.path}&${object2query(router.params)}`, |
| | | RouteHelper.navigateTo({ |
| | | url: `${RouterPath.loginByForm}?redirect=${router.path}&${object2query(router.params)}`, |
| | | }); |
| | | } |
| | | }); |
| | |
| | | return { isAuth }; |
| | | } |
| | | |
| | | export function useInitWeixinJSBridge() { |
| | | const { wxCode } = useUser(); |
| | | |
| | | const userStore = useUserStore(); |
| | | const router = Taro.useRouter(); |
| | | |
| | | const code = router.params.code ?? ''; |
| | | |
| | | const { blLifeRecharge } = useLifeRechargeContext(); |
| | | |
| | | const { getLifePayWxIndentity } = useLifePayWxIndentity(); |
| | | |
| | | Taro.useReady(async () => { |
| | | try { |
| | | //@ts-ignore |
| | | if (!code && isInWeChat && !wxCode.value && location?.replace) { |
| | | location.replace( |
| | | `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf940ff1d35a98493&redirect_uri=${location.href}&response_type=code&scope=snsapi_base#wechat_redirect` |
| | | ); |
| | | } |
| | | if (!!code && !wxCode.value) { |
| | | userStore.setWxCode(router.params.code ?? ''); |
| | | getLifePayWxIndentity(code); |
| | | } |
| | | } catch (error) {} |
| | | }); |
| | | } |
| | | |
| | | export function useLifePayWxIndentity() { |
| | | const { blLifeRecharge } = useLifeRechargeContext(); |
| | | const userStore = useUserStore(); |
| | | |
| | | async function getLifePayWxIndentity(code: string) { |
| | | try { |
| | | let params: APIgetLifePayWxIndentityParams = { |
| | | code: code, |
| | | }; |
| | | let res = await blLifeRecharge.services.getLifePayWxIndentity(params, { |
| | | showLoading: false, |
| | | }); |
| | | userStore.setWxOpenId(res.openId); |
| | | return res; |
| | | } catch (error) {} |
| | | } |
| | | |
| | | return { getLifePayWxIndentity }; |
| | | } |
| | | |
| | | export function useEnsureOpenId() { |
| | | const { wxCode, wxOpenId } = useUser(); |
| | | const { getLifePayWxIndentity } = useLifePayWxIndentity(); |
| | | |
| | | async function ensureOpenId() { |
| | | if (wxOpenId) { |
| | | return wxOpenId.value; |
| | | } else { |
| | | let rea = await getLifePayWxIndentity(wxCode.value); |
| | | return rea.openId; |
| | | } |
| | | } |
| | | |
| | | return { ensureOpenId }; |
| | | } |
| | | |
| | | export function useGoLogin() { |
| | | const router = Taro.useRouter(); |
| | | const { redirectParams } = useLoginedJump(); |
| | | function goLoginFn() { |
| | | // Taro.navigateTo({ |
| | | // RouteHelper.navigateTo({ |
| | | // url: `${RouterPath.authorization}?redirect=${router.path}&${object2query(router.params)}`, |
| | | // }); |
| | | |
| | | Taro.navigateTo({ |
| | | RouteHelper.navigateTo({ |
| | | url: `${RouterPath.loginByForm}?redirect=${redirectParams.value}&tab=${LoginFormTabs.VerificationCodeLogin}`, |
| | | }); |
| | | } |