From c7ab8c4fca1c690f3e5536dff520eaa9c4010fd1 Mon Sep 17 00:00:00 2001
From: zhengyiming <540361168@qq.com>
Date: 星期一, 24 三月 2025 19:52:35 +0800
Subject: [PATCH] fix: 三期bug

---
 apps/taro/src/hooks/user.ts |   56 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/apps/taro/src/hooks/user.ts b/apps/taro/src/hooks/user.ts
index 67a64fe..b6523b1 100644
--- a/apps/taro/src/hooks/user.ts
+++ b/apps/taro/src/hooks/user.ts
@@ -1,13 +1,16 @@
 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,
+} from '@life-payment/core-vue';
 import { isInWeChat } from '@/utils/env';
 
 export function useUser() {
@@ -84,6 +87,8 @@
 
   const { blLifeRecharge } = useLifeRechargeContext();
 
+  const { getLifePayWxIndentity } = useLifePayWxIndentity();
+
   Taro.useReady(async () => {
     try {
       //@ts-ignore
@@ -94,23 +99,58 @@
       }
       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() {

--
Gitblit v1.9.1