lex-xin 4 ay önce
ebeveyn
işleme
2c01b978f9

+ 83 - 33
src/hooks/useAuthCode/index.ts

@@ -4,13 +4,13 @@ import { goWechatAuth } from '@/state';
 import { ref } from 'vue';
 /** 微信Code签名 */
 const WX_CODE_SIGN = 'WXCODESIGN';
-/**
- * 签名有效时间 3分钟
- */
+/** 签名有效时间 3分钟 */
 const SIGN_VALID_TIME = 1000 * 60 * 3;
+/** 微信OpenId */
+const WX_OPEN_ID = 'WXOPENID';
 
 /**
- * 获取微信微信Code码
+ * 获取微信微信Code码 | 获取微信OPENID - 用于微信授权
  */
 export default function useAuthCode() {
   // 微信Code
@@ -21,7 +21,7 @@ export default function useAuthCode() {
    * 获取微信appId
    */
   const getAppId = async () => {
-    if(wxAppId.value) return
+    if (wxAppId.value) return;
     const { data } = await request.get('/edu-app/open/paramConfig/wechatAppId');
     // 判断是否有微信appId
     if (data) {
@@ -45,25 +45,11 @@ export default function useAuthCode() {
     return true;
   };
 
-  const getOpenId = async (code: string) => {
-    await getAppId()
-    if(!wxAppId.value) {
-      return
-    }
-    const { data } = await request.get('/edu-app/open/paramConfig/wechatOpenId', {
-      params: {
-        code,
-        appId: wxAppId.value
-      }
-    });
-    return data;
-  }
-
   /**
    * 去微信获取Code
    * @param url 当前页面的url
    */
-  const getWeChatAuthCode = async (url: string) => {
+  const getWeChatAuthCode = async (url?: string) => {
     const authUrl = url || window.location.href;
     // 判断是否在微信浏览器中打开
     if (!browser().weixin) {
@@ -81,39 +67,103 @@ export default function useAuthCode() {
         return;
       }
       // 设置签名
-      sessionStorage.setItem(WX_CODE_SIGN, Date.now().toString());
+      onWeChatSign('SET', Date.now().toString());
       goWechatAuth(wxAppId.value, authUrl);
     } else {
       weChatCode.value = code;
+      // onWeChatSign('REMOVE');
     }
   };
 
   /**
-   * 获取微信有效Code
+   * 微信Code操作
+   * @param type 操作类型
    */
-  const getAuthCode = () => {
-    return weChatCode.value;
+  const onWeChatCode = (type: 'GET' | 'REMOVE') => {
+    if (type === 'GET') {
+      return weChatCode.value;
+    }
+    weChatCode.value = '';
   };
 
   /**
-   * 移除微信Code
+   * 微信sign操作
+   * @param type 操作类型
+   * @param value 值
    */
-  const removeWeChatCode = () => {
-    weChatCode.value = '';
+  const onWeChatSign = (type: 'GET' | 'REMOVE' | 'SET', value?: string) => {
+    if (type === 'GET') {
+      return sessionStorage.getItem(WX_CODE_SIGN);
+    } else if (type === 'REMOVE') {
+      sessionStorage.removeItem(WX_CODE_SIGN);
+    } else if (type === 'SET') {
+      sessionStorage.setItem(WX_CODE_SIGN, value || '');
+    }
+  };
+
+  /**
+   * 获取缓存微信OpenId
+   * @param type 操作类型
+   * @param value 值
+   */
+  const onWeChatCatchOpenId = (
+    type: 'GET' | 'REMOVE' | 'SET',
+    value?: string
+  ) => {
+    if (type === 'GET') {
+      return sessionStorage.getItem(WX_OPEN_ID);
+    } else if (type === 'REMOVE') {
+      sessionStorage.removeItem(WX_OPEN_ID);
+    } else if (type === 'SET') {
+      sessionStorage.setItem(WX_OPEN_ID, value || '');
+    }
   };
 
   /**
-   * 移除微信签名
+   * 获取微信OpenId
+   * @param url 当前页面的url
+   * @param code 微信Code
    */
-  const removeWeChatSign = () => {
-    sessionStorage.removeItem(WX_CODE_SIGN);
+  const getOpenId = async (url?: string, code?: string) => {
+    // 获取缓存OpenId
+    const openId = onWeChatCatchOpenId('GET');
+    if (openId) {
+      return openId;
+    }
+    console.log(url, 'url');
+    // debugger
+    // 如果没有code - 去获取code
+    if (!code) {
+      await getWeChatAuthCode(url);
+    }
+
+    if (!code && !weChatCode.value) {
+      console.warn('没有获取到微信Code');
+      return;
+    }
+
+    await getAppId();
+    if (!wxAppId.value) {
+      return;
+    }
+    const { data } = await request.post(
+      '/edu-app/open/paramConfig/wechatOpenId',
+      {
+        data: {
+          code: code || weChatCode.value,
+          appId: wxAppId.value
+        }
+      }
+    );
+    onWeChatCatchOpenId('SET', data);
+    return data;
   };
 
   return {
     getOpenId,
     getWeChatAuthCode,
-    getAuthCode,
-    removeWeChatSign,
-    removeWeChatCode
+    onWeChatCode,
+    onWeChatSign,
+    onWeChatCatchOpenId
   };
 }

+ 8 - 2
src/state.ts

@@ -76,8 +76,14 @@ export const goWechatAuth = (wxAppId: string, urlString?: string) => {
   // 开发环境
   console.log(import.meta.env.DEV, "import.meta.env.DEV")
   if (import.meta.env.DEV) {
-    const replaceUrl =
-      `https://kt.colexiu.com/getWxCode?appid=${
+    // const replaceUrl =
+    //   `https://kt.colexiu.com/getWxCode?appid=${
+    //     wxAppId || 'wx8654c671631cfade'
+    //   }&state=STATE&redirect_uri=` +
+    //   encodeURIComponent(urlString || window.location.href);
+
+      const replaceUrl =
+      `https://online.lexiaoya.cn/getWxCode?appid=${
         wxAppId || 'wx8654c671631cfade'
       }&state=STATE&redirect_uri=` +
       encodeURIComponent(urlString || window.location.href);

+ 33 - 37
src/views/intention-questionnaire/index.tsx

@@ -84,23 +84,19 @@ export default defineComponent({
     onMounted(async () => {
       forms.meetingType = route.query.type || forms.meetingType;
       forms.code = route.query.code || forms.code;
-      // nextTick(() => {
-      //   const videoRef: any = document.querySelector('#register-video')
-      //   const videoRef2: any = document.querySelector('#register-video2')
-      //   if(videoRef) {
-      //     const rect = videoRef?.getBoundingClientRect()
-      //     console.log(rect)
-      //     videoRef.style.height = rect.width / 16 * 9 + 'px'
-      //   }
-      //   if(videoRef2) {
-      //     const rect = videoRef2?.getBoundingClientRect()
-      //     console.log(rect)
-      //     videoRef2.style.height = rect.width / 16 * 9 + 'px'
-      //   }
-      // })
       try {
-        // 判断是否获取微信code码
-        if (!forms.code && browser().weixin) return;
+        const openId = await authCode.getOpenId(
+          getHttpOrigin() +
+            window.location.pathname +
+            '#' +
+            route.path +
+            '?' +
+            qs.stringify({
+              ...route.query
+            })
+        );
+        console.log(openId, 'openId ----- ');
+        if (browser().weixin) return;
         const { data } = await request.get(
           '/edu-app/open/meetingQuestionSetting/detail?type=' +
             forms.meetingType +
@@ -113,8 +109,8 @@ export default defineComponent({
           forms.contentC = data.contentC || forms.contentC;
           forms.contentD = data.contentD || forms.contentD;
           forms.openId = data.openId || forms.openId;
-          sessionStorage.setItem('active-open-id', forms.openId);
-          sessionStorage.removeItem('isWxcode');
+          // sessionStorage.setItem('active-open-id', forms.openId);
+          // sessionStorage.removeItem('isWxcode');
         }
         forms.contentShow = true;
       } catch {
@@ -125,26 +121,26 @@ export default defineComponent({
       forms.player2 = _initVideo('two', forms, videoIntervalRef2);
     });
 
-    const getAppIdAndCode = async (url?: string) => {
-      try {
-        // const { data } = await request.get(
-        //   '/edu-app/open/paramConfig/wechatAppId'
-        // );
-        // // 判断是否有微信appId
-        // if (data) {
-        //   sessionStorage.setItem('isWxcode', '1');
-        //   closeToast();
-        //   goWechatAuth(data, url);
-        // }
+    // const getAppIdAndCode = async (url?: string) => {
+    //   try {
+    //     // const { data } = await request.get(
+    //     //   '/edu-app/open/paramConfig/wechatAppId'
+    //     // );
+    //     // // 判断是否有微信appId
+    //     // if (data) {
+    //     //   sessionStorage.setItem('isWxcode', '1');
+    //     //   closeToast();
+    //     //   goWechatAuth(data, url);
+    //     // }
 
-        sessionStorage.setItem('isWxcode', '1');
-        closeToast();
-        goWechatAuthTemp('wxccc2efd2678adbe3', url);
-      } catch (e) {
-        //
-        console.log(e);
-      }
-    };
+    //     sessionStorage.setItem('isWxcode', '1');
+    //     closeToast();
+    //     goWechatAuthTemp('wxccc2efd2678adbe3', url);
+    //   } catch (e) {
+    //     //
+    //     console.log(e);
+    //   }
+    // };
 
     // if (browser().weixin) {
     //   //授权