lex-xin 4 月之前
父節點
當前提交
7639056368
共有 4 個文件被更改,包括 162 次插入56 次删除
  1. 119 0
      src/hooks/useAuthCode/index.ts
  2. 1 1
      src/hooks/useWeChatShare/index.ts
  3. 2 1
      src/state.ts
  4. 40 54
      src/views/intention-questionnaire/index.tsx

+ 119 - 0
src/hooks/useAuthCode/index.ts

@@ -0,0 +1,119 @@
+import request from '@/helpers/request';
+import { browser, getUrlCode } from '@/helpers/utils';
+import { goWechatAuth } from '@/state';
+import { ref } from 'vue';
+/** 微信Code签名 */
+const WX_CODE_SIGN = 'WXCODESIGN';
+/**
+ * 签名有效时间 3分钟
+ */
+const SIGN_VALID_TIME = 1000 * 60 * 3;
+
+/**
+ * 获取微信微信Code码
+ */
+export default function useAuthCode() {
+  // 微信Code
+  const weChatCode = ref<string>('');
+  // 微信appId
+  const wxAppId = ref<string>('');
+  /**
+   * 获取微信appId
+   */
+  const getAppId = async () => {
+    if(wxAppId.value) return
+    const { data } = await request.get('/edu-app/open/paramConfig/wechatAppId');
+    // 判断是否有微信appId
+    if (data) {
+      wxAppId.value = data;
+    }
+  };
+
+  /**
+   * 检查签名是否有效
+   */
+  const checkSignValid = () => {
+    const sign = sessionStorage.getItem(WX_CODE_SIGN);
+    if (!sign) {
+      return false;
+    }
+    const now = Date.now();
+    //
+    if (now - Number(sign) > SIGN_VALID_TIME) {
+      return false;
+    }
+    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 authUrl = url || window.location.href;
+    // 判断是否在微信浏览器中打开
+    if (!browser().weixin) {
+      console.warn('在微信浏览器中打开');
+      return;
+    }
+
+    // 获取链接上的code
+    const code = getUrlCode();
+    // 如果链接上有code 或者 签名无效
+    if (!code || !checkSignValid()) {
+      await getAppId();
+      if (!wxAppId.value) {
+        console.warn('没有获取到微信appId');
+        return;
+      }
+      // 设置签名
+      sessionStorage.setItem(WX_CODE_SIGN, Date.now().toString());
+      goWechatAuth(wxAppId.value, authUrl);
+    } else {
+      weChatCode.value = code;
+    }
+  };
+
+  /**
+   * 获取微信有效Code
+   */
+  const getAuthCode = () => {
+    return weChatCode.value;
+  };
+
+  /**
+   * 移除微信Code
+   */
+  const removeWeChatCode = () => {
+    weChatCode.value = '';
+  };
+
+  /**
+   * 移除微信签名
+   */
+  const removeWeChatSign = () => {
+    sessionStorage.removeItem(WX_CODE_SIGN);
+  };
+
+  return {
+    getOpenId,
+    getWeChatAuthCode,
+    getAuthCode,
+    removeWeChatSign,
+    removeWeChatCode
+  };
+}

+ 1 - 1
src/hooks/useWeChatShare/index.ts

@@ -32,7 +32,7 @@ export default function useWeChatShare(
       });
   };
   const setWeChatShare = (data: any) => {
-    console.log(wxChat, 'wxChat');
+    // console.log(wxChat, 'wxChat');
     wxChat.config({
       debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
       appId: data.appId, // 必填,公众号的唯一标识

+ 2 - 1
src/state.ts

@@ -74,9 +74,10 @@ export const goWechatAuthTemp = (wxAppId: string, urlString?: string) => {
  */
 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://online.lexiaoya.cn/getWxCode?appid=${
+      `https://kt.colexiu.com/getWxCode?appid=${
         wxAppId || 'wx8654c671631cfade'
       }&state=STATE&redirect_uri=` +
       encodeURIComponent(urlString || window.location.href);

+ 40 - 54
src/views/intention-questionnaire/index.tsx

@@ -1,51 +1,37 @@
-import {
-  defineComponent,
-  onMounted,
-  onUnmounted,
-  reactive,
-  ref,
-  nextTick
-} from 'vue';
+import { defineComponent, onMounted, onUnmounted, reactive, ref } from 'vue';
 import styles from './index.module.less';
-// import signinTips from './images/signin-tips.png';
-import {
-  Button,
-  CellGroup,
-  Field,
-  Picker,
-  Popup,
-  closeToast,
-  showToast,
-  Loading
-} from 'vant';
+import { closeToast } from 'vant';
 import { useRoute, useRouter } from 'vue-router';
-import threeMan from './images/update/three-man.png';
 import OWxTip from '@/components/m-wx-tip';
 import { browser, getHttpOrigin, getUrlCode } from '@/helpers/utils';
 import qs from 'query-string';
 import request from '@/helpers/request';
 import { goWechatAuth, goWechatAuthTemp } from '@/state';
-import { useInterval, useIntervalFn } from '@vueuse/core';
-import MMessageTip from '@/components/m-message-tip';
-import TCPlayer from 'tcplayer.js';
+import { useInterval } from '@vueuse/core';
 import 'tcplayer.js/dist/tcplayer.css';
 import { _initVideo } from './initVideo';
 import nextBtn from './images/next_btn.png';
 import useWeChatShare from '@/hooks/useWeChatShare';
+import useAuthCode from '@/hooks/useAuthCode';
 
 export default defineComponent({
   name: 'intention-questionnaire',
   setup() {
     const route = useRoute();
-    const shareTitle = route.query.type === 'primarySchoolNo' ? '(小学)关于开展音乐(器乐)课堂数字化转型的调查问卷' :
-    route.query.type === 'juniorSchoolNo' ? '(初中)关于开展音乐(器乐)课堂数字化转型的调查问卷' :
-    '关于开展音乐(器乐)课堂数字化转型的调查问卷'
+    const shareTitle =
+      route.query.type === 'primarySchoolNo'
+        ? '(小学)关于开展音乐(器乐)课堂数字化转型的调查问卷'
+        : route.query.type === 'juniorSchoolNo'
+        ? '(初中)关于开展音乐(器乐)课堂数字化转型的调查问卷'
+        : '关于开展音乐(器乐)课堂数字化转型的调查问卷';
     const weChatShare = useWeChatShare(
       shareTitle,
       '科学的教育改变世界,科技的力量让音乐传播更远,让孩子奏响心中的乐章',
       window.location.origin + '/classroom-app/shareImg/question-share.png'
     );
 
+    const authCode = useAuthCode();
+
     // 页面定时
     const pageTimer = useInterval(1000, { controls: true });
     pageTimer.pause();
@@ -114,7 +100,7 @@ export default defineComponent({
       // })
       try {
         // 判断是否获取微信code码
-        if (!forms.code  && browser().weixin) return;
+        if (!forms.code && browser().weixin) return;
         const { data } = await request.get(
           '/edu-app/open/meetingQuestionSetting/detail?type=' +
             forms.meetingType +
@@ -152,39 +138,39 @@ export default defineComponent({
         // }
 
         sessionStorage.setItem('isWxcode', '1');
-        closeToast()
-        goWechatAuthTemp('wxccc2efd2678adbe3', url)
+        closeToast();
+        goWechatAuthTemp('wxccc2efd2678adbe3', url);
       } catch (e) {
         //
         console.log(e);
       }
     };
 
-    if (browser().weixin) {
-      //授权
-      const openId = sessionStorage.getItem('active-open-id');
-      forms.openId = openId;
-      const code = getUrlCode();
-      const isWxcode = sessionStorage.getItem('isWxcode');
-      if (!code || isWxcode !== '1') {
-        const newUrl =
-          getHttpOrigin() +
-          window.location.pathname +
-          '#' +
-          route.path +
-          '?' +
-          qs.stringify({
-            ...route.query
-          });
-        getAppIdAndCode(newUrl);
-        return '';
-      } else {
-        forms.code = code;
+    // if (browser().weixin) {
+    //   //授权
+    //   const openId = sessionStorage.getItem('active-open-id');
+    //   forms.openId = openId;
+    //   const code = getUrlCode();
+    //   const isWxcode = sessionStorage.getItem('isWxcode');
+    //   if (!code || isWxcode !== '1') {
+    //     const newUrl =
+    //       getHttpOrigin() +
+    //       window.location.pathname +
+    //       '#' +
+    //       route.path +
+    //       '?' +
+    //       qs.stringify({
+    //         ...route.query
+    //       });
+    //     getAppIdAndCode(newUrl);
+    //     return '';
+    //   } else {
+    //     forms.code = code;
 
-        // 获取微信分享签名
-        weChatShare.getAppSignature()
-      }
-    }
+    //     // 获取微信分享签名
+    //     weChatShare.getAppSignature();
+    //   }
+    // }
     const nextSkip = () => {
       router.push({
         path: '/fill-questionnaire',
@@ -208,7 +194,7 @@ export default defineComponent({
       forms.isPageHide = true;
     };
     window.addEventListener('pagehide', onPageHide);
-
+    authCode.getWeChatAuthCode();
     onUnmounted(() => {
       window.removeEventListener('pageshow', onPageShow);
       window.removeEventListener('pagehide', onPageHide);