|
@@ -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
|
|