Yansen 2 years ago
parent
commit
3d2b361124
2 changed files with 41 additions and 35 deletions
  1. 12
    6
      src/main.js
  2. 29
    29
      src/utils/wx.js

+ 12
- 6
src/main.js View File

2
 import VueSplide from '@splidejs/vue-splide';
2
 import VueSplide from '@splidejs/vue-splide';
3
 import 'animate.css'
3
 import 'animate.css'
4
 import '@splidejs/vue-splide/css/core';
4
 import '@splidejs/vue-splide/css/core';
5
+import { getCode, redirect } from '@/utils/wx';
5
 
6
 
6
 import App from './App.vue'
7
 import App from './App.vue'
7
 import store from './store'
8
 import store from './store'
10
 
11
 
11
 import './style.less'
12
 import './style.less'
12
 
13
 
13
-const app = createApp(App);
14
-app.use(store);
15
-app.use(router);
16
-app.use(i18n);
17
-app.use(VueSplide);
18
-app.mount('#app');
14
+const code = getCode();
15
+if (!code) {
16
+  redirect();
17
+} else {
18
+  const app = createApp(App);
19
+  app.use(store);
20
+  app.use(router);
21
+  app.use(i18n);
22
+  app.use(VueSplide);
23
+  app.mount('#app');  
24
+}

+ 29
- 29
src/utils/wx.js View File

1
 
1
 
2
-function wxsdk() {
3
-  const isDev = import.meta.env.DEV
2
+let APPID = '';
3
+const isDev = import.meta.env.DEV
4
+
5
+/**
6
+ * 获取 code
7
+ * @returns 
8
+ */
9
+export function getCode () {
10
+  if (isDev) return '123';
11
+
12
+  const matched = /[?&]*code=([^&]+)/.exec(location.search)
13
+  if (matched) {
14
+    return decodeURIComponent(matched[1])
15
+  }
16
+}
4
 
17
 
18
+/**
19
+ * 跳转授权页面
20
+ */
21
+export function redirect () {
22
+  if (isDev) return;
23
+
24
+  const appid = APPID || 'wx5d0c5a7f210d872f';
25
+  const local = encodeURIComponent(location.origin + location.pathname)
26
+  const scope = 'snsapi_userinfo'; // snsapi_base
27
+  const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=${scope}&state=123#wechat_redirect`
28
+  window.location.href = url;
29
+}
30
+
31
+function wxsdk() {
5
   // API_BASE 来源 public/config.js
32
   // API_BASE 来源 public/config.js
6
   const apiBase = API_BASE // import.meta.env.VITE_APP_API_BASE
33
   const apiBase = API_BASE // import.meta.env.VITE_APP_API_BASE
7
 
34
 
8
-  let APPID = '';
9
 
35
 
10
   // 分享接口
36
   // 分享接口
11
   const shareAPIs = [
37
   const shareAPIs = [
69
     });
95
     });
70
   }
96
   }
71
 
97
 
72
-  /**
73
-   * 获取 code
74
-   * @returns 
75
-   */
76
-  function getCode () {
77
-    if (isDev) return '123';
78
-
79
-    const matched = /[?&]*code=([^&]+)/.exec(location.search)
80
-    if (matched) {
81
-      return decodeURIComponent(matched[1])
82
-    }
83
-  }
84
-  
85
-  /**
86
-   * 跳转授权页面
87
-   */
88
-  function redirect () {
89
-    if (isDev) return;
90
-
91
-    const appid = APPID || 'wx5d0c5a7f210d872f';
92
-    const local = encodeURIComponent(location.origin + location.pathname)
93
-    const scope = 'snsapi_userinfo'; // snsapi_base
94
-    const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=${scope}&state=123#wechat_redirect`
95
-    window.location.href = url;
96
-  }
97
-
98
   return {
98
   return {
99
     share,
99
     share,
100
     getCode,
100
     getCode,