From ad032ad0889156572271868601d6665e560318b4 Mon Sep 17 00:00:00 2001
From: zhengyiming <540361168@qq.com>
Date: 星期四, 06 三月 2025 13:04:29 +0800
Subject: [PATCH] fix: 对接

---
 apps/taro/src/hooks/user.ts |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/apps/taro/src/hooks/user.ts b/apps/taro/src/hooks/user.ts
index f1308ee..d30eebd 100644
--- a/apps/taro/src/hooks/user.ts
+++ b/apps/taro/src/hooks/user.ts
@@ -7,12 +7,21 @@
 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, virtualUserId, virtualPhoneNumber } =
-    storeToRefs(userStore);
+  const {
+    userDetail,
+    userInfo,
+    locationCity,
+    virtualUserId,
+    virtualPhoneNumber,
+    wxCode,
+    wxOpenId,
+  } = storeToRefs(userStore);
 
   function updateUserInfo() {
     return userStore.getCurrentUserInfo();
@@ -25,6 +34,8 @@
     locationCity,
     virtualUserId,
     virtualPhoneNumber,
+    wxCode,
+    wxOpenId,
   };
 }
 
@@ -47,6 +58,7 @@
   const isAuth = computed(() => !needAuth || isLogin.value);
 
   const router = Taro.useRouter();
+  useInitWeixinJSBridge();
 
   Taro.useReady(async () => {
     if (isLogin.value && userStore.firstGetUserDetail) {
@@ -62,6 +74,45 @@
   return { isAuth };
 }
 
+export function useInitWeixinJSBridge() {
+  const { wxCode } = useUser();
+
+  const userStore = useUserStore();
+  const router = Taro.useRouter();
+
+  const code = router.params.code ?? '';
+
+  const { blLifeRecharge } = useLifeRechargeContext();
+
+  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 ?? '');
+        let res = await getLifePayWxIndentity();
+        userStore.setWxOpenId(res.openId);
+      }
+    } catch (error) {}
+  });
+
+  async function getLifePayWxIndentity() {
+    try {
+      let params: APIgetLifePayWxIndentityParams = {
+        code: code,
+      };
+      let res = await blLifeRecharge.services.getLifePayWxIndentity(params, {
+        showLoading: false,
+      });
+      return res;
+    } catch (error) {}
+  }
+}
+
 export function useGoLogin() {
   const router = Taro.useRouter();
   const { redirectParams } = useLoginedJump();

--
Gitblit v1.9.1