Your Name 2 年前
父节点
当前提交
31a6642c88
共有 5 个文件被更改,包括 75 次插入10 次删除
  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 查看文件

9
 const link1URL = 'http://h5.njyunzhi.com/wenhua818-2/index.html';
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 查看文件

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

+ 9
- 2
src/pages/creation/index.vue 查看文件

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

+ 11
- 1
src/store/index.js 查看文件

1
 import { ref } from 'vue';
1
 import { ref } from 'vue';
2
 import createStore from '@zjxpcyc/vue-tiny-store';
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
 const usePoem = () => {
14
 const usePoem = () => {
5
   const name = ref();
15
   const name = ref();
6
   const content = ref();
16
   const content = ref();
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
 export default store;
39
 export default store;

+ 49
- 5
src/utils/wx.js 查看文件

25
 
25
 
26
   function init() {
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
       if (res.code === 1000) {
29
       if (res.code === 1000) {
30
         const data = res.data;
30
         const data = res.data;
31
 
31
 
44
     })
44
     })
45
   }
45
   }
46
 
46
 
47
-  // 立即执行
48
-  init();
49
-
50
   function share(params = {}) {
47
   function share(params = {}) {
51
     if (isDev) return;
48
     if (isDev) return;
52
 
49
 
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
   return {
107
   return {
66
-    share
108
+    init,
109
+    share,
110
+    getCode,
67
   }
111
   }
68
 }
112
 }
69
 
113