From 8bb9173a95a87e3ceab4f48b5b34041af38b7c70 Mon Sep 17 00:00:00 2001 From: zhengyiming <540361168@qq.com> Date: 星期五, 28 三月 2025 15:23:14 +0800 Subject: [PATCH] fix: 四期需求 --- apps/taro/src/hooks/user.ts | 100 +++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 86 insertions(+), 14 deletions(-) diff --git a/apps/taro/src/hooks/user.ts b/apps/taro/src/hooks/user.ts index e651456..36ec443 100644 --- a/apps/taro/src/hooks/user.ts +++ b/apps/taro/src/hooks/user.ts @@ -1,14 +1,41 @@ 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 { + useLifeRechargeContext, + APIgetLifePayWxIndentityParams, + WxMiniAppIndentityInfo, + LifePayPhoneMesssageCodeLoginOutput, +} from '@life-payment/core-vue'; import { isInWeChat } from '@/utils/env'; + +export function useVirtualUserInfo() { + const { blLifeRecharge } = useLifeRechargeContext(); + + const { data: virtualUserInfo } = useQuery({ + queryKey: ['blLifeRecharge/lifePayUserMesssageByIduser', blLifeRecharge.accountModel.userId], + queryFn: async () => { + return await blLifeRecharge.services.lifePayUserMesssageByIduser( + { + id: blLifeRecharge.accountModel.userId, + }, + { + showLoading: false, + } + ); + }, + placeholderData: () => ({} as LifePayPhoneMesssageCodeLoginOutput), + enabled: computed(() => !!blLifeRecharge.accountModel.userId), + staleTime: Infinity, + }); + + return { virtualUserInfo }; +} export function useUser() { const userStore = useUserStore(); @@ -27,6 +54,11 @@ return userStore.getCurrentUserInfo(); } + const { blLifeRecharge } = useLifeRechargeContext(); + const { virtualUserInfo } = useVirtualUserInfo(); + + const isChannelAccount = computed(() => virtualUserInfo.value.isBackClientUser); + return { user: userInfo, userDetail: userDetail, @@ -36,6 +68,8 @@ virtualPhoneNumber, wxCode, wxOpenId, + isChannelAccount, + virtualUserInfo, }; } @@ -58,7 +92,6 @@ const isAuth = computed(() => !needAuth || isLogin.value); const router = Taro.useRouter(); - console.log('222', 222); useInitWeixinJSBridge(); Taro.useReady(async () => { @@ -66,7 +99,7 @@ // userStore.getCurrentUserInfo(); } if (needAuth && !isLogin.value) { - Taro.navigateTo({ + RouteHelper.navigateTo({ url: `${RouterPath.loginByForm}?redirect=${router.path}&${object2query(router.params)}`, }); } @@ -85,42 +118,81 @@ const { blLifeRecharge } = useLifeRechargeContext(); + const { getLifePayWxIndentity } = useLifePayWxIndentity(); + Taro.useReady(async () => { try { //@ts-ignore - if (!code && isInWeChat && !wxCode.value) { - location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf940ff1d35a98493&redirect_uri=${location.href}&response_type=code&scope=snsapi_base#wechat_redirect`; + 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 ?? ''); - let res = await getLifePayWxIndentity(); - userStore.setWxOpenId(res.openId); + getLifePayWxIndentity(code); } } catch (error) {} }); +} - async function getLifePayWxIndentity() { +let wxIndentityPromise: Promise<WxMiniAppIndentityInfo>; + +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, - }); + if (!wxIndentityPromise) { + wxIndentityPromise = blLifeRecharge.services + .getLifePayWxIndentity(params, { + showLoading: false, + }) + .finally(() => { + wxIndentityPromise = undefined; + }); + } + + let res = await wxIndentityPromise; + if (res.openId) { + 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.value) { + return wxOpenId.value; + } else { + let rea = await getLifePayWxIndentity(wxCode.value); + return rea.openId ? rea.openId : wxOpenId.value; + } + } + + 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}`, }); } -- Gitblit v1.9.1