forked from wxlite-plus/mp-req
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype.js
213 lines (201 loc) · 4.63 KB
/
prototype.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const sessionStorageKey = 'mp-req-session-id';
let sessionId = wx.getStorageSync(sessionStorageKey);
const loginQueue = [];
let isLoginning = false;
const req = {
apiUrl: '',
code2sessionId: null,
isSessionAvailable: null,
sessionHeaderKey: 'sessionId',
init(opt = {}) {
const {
apiUrl,
code2sessionId,
isSessionAvailable,
sessionHeaderKey,
} = opt;
if (apiUrl) {
req.apiUrl = apiUrl;
}
if (code2sessionId) {
req.code2sessionId = code2sessionId;
}
if (isSessionAvailable) {
req.isSessionAvailable = isSessionAvailable;
}
if (sessionHeaderKey) {
req.sessionHeaderKey = sessionHeaderKey;
}
},
};
/**
* 判断请求状态是否成功
* @param {number} status
*/
function isHttpSuccess(status) {
return (status >= 200 && status < 300) || status === 304;
}
/**
* promise请求
* @param {object} options {}
*/
function requestP(options = {}) {
const {
success,
fail,
} = options;
// 统一注入约定的header
const header = Object.assign({
[req.sessionHeaderKey]: sessionId,
}, options.header);
return new Promise((res, rej) => {
wx.request(Object.assign(
{},
options,
{
header,
success(r) {
const isSuccess = isHttpSuccess(r.statusCode);
if (isSuccess) { // 成功的请求状态
if (success) {
success(r.data);
return;
}
res(r.data);
} else {
const err = {
msg: `服务器好像出了点小问题,请与客服联系~(错误代码:${r.statusCode})`,
detail: r,
};
if (fail) {
fail(err);
return;
}
rej(err);
}
},
fail(err) {
if (fail) {
fail(err);
return;
}
rej(err);
},
},
));
});
}
/**
* 登录
*/
function login() {
return new Promise((res, rej) => {
// 微信登录
wx.login({
success(r1) {
if (r1.code) {
// 获取sessionId
if (!req.code2sessionId) {
rej('code2sessionId未定义');
return;
}
req.code2sessionId(r1.code)
.then((r2) => {
const newSessionId = r2;
sessionId = newSessionId; // 更新sessionId
// 保存sessionId
wx.setStorage({
key: sessionStorageKey,
data: newSessionId,
});
res(r2);
})
.catch(rej);
} else {
rej(r1);
}
},
fail: rej,
});
});
}
/**
* 获取sessionId
*/
function getSessionId() {
return new Promise((res, rej) => {
// 本地sessionId丢失,重新登录
if (!sessionId) {
loginQueue.push({ res, rej });
if (!isLoginning) {
isLoginning = true;
login()
.then((r1) => {
isLoginning = false;
while (loginQueue.length) {
loginQueue.shift().res(r1);
}
})
.catch((err) => {
isLoginning = false;
while (loginQueue.length) {
loginQueue.shift().rej(err);
}
});
}
} else {
res(sessionId);
}
});
}
/**
* ajax高级封装
* @param {object} options {}
* @param {boolean} keepLogin true
*/
function request(options = {}, keepLogin = true) {
if (keepLogin) {
return new Promise((res, rej) => {
getSessionId()
.then(() => {
// 获取sessionId成功之后,发起请求
requestP(options)
.then((r2) => {
if (!req.isSessionAvailable) {
rej('isSessionAvailable未定义');
return;
}
if (!req.isSessionAvailable(r2)) {
/**
* 登录状态无效,则重新走一遍登录流程
* 销毁本地已失效的sessionId
*/
sessionId = '';
getSessionId()
.then(() => {
requestP(options)
.then(res)
.catch(rej);
})
.catch(rej);
} else {
res(r2);
}
})
.catch(rej);
})
.catch(rej);
});
}
// 不需要sessionId,直接发起请求
return requestP(options);
}
/**
* 插件接口
* @param {object} plugin
*/
function use(plugin) {
return plugin.install(req, request);
}
req.use = use;
module.exports = req;