Your Name 2 år sedan
förälder
incheckning
31a6642c88
5 ändrade filer med 75 tillägg och 10 borttagningar
  1. 4
    1
      public/config.js
  2. 2
    1
      src/main.js
  3. 9
    2
      src/pages/creation/index.vue
  4. 11
    1
      src/store/index.js
  5. 49
    5
      src/utils/wx.js

+ 4
- 1
public/config.js Visa fil

@@ -9,4 +9,7 @@ const shareOptTpl = {
9 9
 const link1URL = 'http://h5.njyunzhi.com/wenhua818-2/index.html';
10 10
 
11 11
 // 分享接口地址
12
-const API_BASE = 'http://test-h5.njyunzhi.com/api/wx/jsapi';
12
+const API_BASE = 'http://test-h5.njyunzhi.com';
13
+
14
+// 公众号ID
15
+const APPID = 'wxd3bab568bc42d1de';

+ 2
- 1
src/main.js Visa fil

@@ -7,7 +7,7 @@ import store from './store'
7 7
 import wxsdk from './utils/wx'
8 8
 import router from './router'
9 9
 
10
-const { share } = wxsdk(location.href)
10
+const { share, getCode } = wxsdk(location.href)
11 11
 
12 12
 const app = createApp(App);
13 13
 app.use(VueViewer);
@@ -15,4 +15,5 @@ app.use(store);
15 15
 app.use(router);
16 16
 app.mount('#app');
17 17
 
18
+getCode(store);
18 19
 share();

+ 9
- 2
src/pages/creation/index.vue Visa fil

@@ -2,13 +2,13 @@
2 2
   <div class="page" :style="pgStyle">
3 3
     <Image :src="backImg" :actions="actions" @load="onLoad" />
4 4
     <TGradient :style="gdStyle" color="#0f2c37" />
5
-    <input class="input-box input-line" placeholder="请输入您的祝福语!" :style="iiStyle" @input="onAuthor" />
5
+    <input class="input-box input-line" placeholder="请输入您的姓名!" :style="iiStyle" @input="onAuthor" ref="inputRef" />
6 6
     <textarea class="input-box input-area" placeholder="请输入您的祝福语!" :style="ttStyle" @input="onContent"></textarea>
7 7
   </div>
8 8
 </template>
9 9
 
10 10
 <script setup>
11
-  import { ref } from 'vue';
11
+  import { onMounted, ref } from 'vue';
12 12
   import { useRouter } from 'vue-router';
13 13
   import { useModel } from '@zjxpcyc/vue-tiny-store';
14 14
   import Image from '@/components/Image.vue'
@@ -33,10 +33,12 @@
33 33
   ];
34 34
 
35 35
   const router = useRouter();
36
+  const inputRef = ref();
36 37
   const gdStyle = ref({});
37 38
   const ttStyle = ref({});
38 39
   const iiStyle = ref({});
39 40
   const pgStyle = ref({});
41
+  const [user] = useModel('user');
40 42
   const [author, poemContent] = useModel('poem');
41 43
 
42 44
   const onLoad = (e) => {
@@ -79,6 +81,11 @@
79 81
     poemContent.value = e.target.value;
80 82
   }
81 83
 
84
+  onMounted(() => {
85
+    inputRef.value.value = (user.value || {}).nickname || undefined;
86
+    author.value = inputRef.value.value;
87
+  })
88
+
82 89
 </script>
83 90
 
84 91
 <style lang="less" scoped>

+ 11
- 1
src/store/index.js Visa fil

@@ -1,6 +1,16 @@
1 1
 import { ref } from 'vue';
2 2
 import createStore from '@zjxpcyc/vue-tiny-store';
3 3
 
4
+const useUser = () => {
5
+  const user = ref();
6
+  const setUser = u => user.value = u;
7
+
8
+  return [
9
+    user,
10
+    setUser,
11
+  ]
12
+}
13
+
4 14
 const usePoem = () => {
5 15
   const name = ref();
6 16
   const content = ref();
@@ -24,6 +34,6 @@ const useQRCode = () => {
24 34
   ]
25 35
 };
26 36
 
27
-const store = createStore({ poem: usePoem, qrcode: useQRCode });
37
+const store = createStore({ user: useUser, poem: usePoem, qrcode: useQRCode });
28 38
 
29 39
 export default store;

+ 49
- 5
src/utils/wx.js Visa fil

@@ -25,7 +25,7 @@ function wxsdk(url) {
25 25
 
26 26
   function init() {
27 27
     // 请求后台接口
28
-    fetch(`${apiBase}?url=${encodeURIComponent(url)}`).then(res => res.json()).then((res) => {
28
+    fetch(`${apiBase}/api/wx/jsapi?url=${encodeURIComponent(url)}`).then(res => res.json()).then((res) => {
29 29
       if (res.code === 1000) {
30 30
         const data = res.data;
31 31
 
@@ -44,9 +44,6 @@ function wxsdk(url) {
44 44
     })
45 45
   }
46 46
 
47
-  // 立即执行
48
-  init();
49
-
50 47
   function share(params = {}) {
51 48
     if (isDev) return;
52 49
 
@@ -62,8 +59,55 @@ function wxsdk(url) {
62 59
     });
63 60
   }
64 61
 
62
+  function getCode(store) {
63
+    const [,setUser] = store.getState('user');
64
+    if (isDev) {
65
+      setUser({ nickname: 123 });
66
+      return ;
67
+    }
68
+
69
+    const matched = /[?&]+code=([^?&]+)/.exec(location.href);
70
+    if (!matched) {
71
+      const target = encodeURIComponent(location.origin + location.pathname)
72
+      location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${target}&state=abc123#wechat_redirect`
73
+      return false;
74
+    }
75
+
76
+    const onError = () => {
77
+      //
78
+      const errorTimes = localStorage.getItem("error-times") || 0;
79
+      if (errorTimes < 3) {
80
+        errorTimes += 1;
81
+        localStorage.setItem("error-times", errorTimes);
82
+
83
+        // 重来一次
84
+        location.href = location.origin + location.pathname;
85
+        return;
86
+      } else {
87
+        // 未获取到任何信息
88
+        setUser({});
89
+      }
90
+    }
91
+
92
+    const code = matched[1];    
93
+    // 请求后台接口
94
+    fetch(`${apiBase}/api/wx/user?code=${code}`).then(res => res.json()).then((res) => {
95
+      if (res.code === 1000) {
96
+        setUser(res.data);
97
+        localStorage.setItem("error-times", "0");
98
+      } else {
99
+        onError();
100
+      }
101
+    }).catch((err) => {
102
+      console.error(err);
103
+      onError();
104
+    })
105
+  }
106
+
65 107
   return {
66
-    share
108
+    init,
109
+    share,
110
+    getCode,
67 111
   }
68 112
 }
69 113