index.js
10 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* Shopro-request
* @description api模块管理,loading配置,请求拦截,错误处理
*/
import Request from 'luch-request';
import { apiPath, baseUrl, tenantId } from '@/sheep/config';
import $store from '@/sheep/store';
import $platform from '@/sheep/platform';
import AuthUtil from '@/sheep/api/member/auth';
import { getTerminal } from '@/sheep/helper/const';
const options = {
// 显示操作成功消息 默认不显示
showSuccess: false,
// 成功提醒 默认使用后端返回值
successMsg: '',
// 显示失败消息 默认显示
showError: true,
// 失败提醒 默认使用后端返回信息
errorMsg: '',
// 显示请求时loading模态框 默认显示
showLoading: true,
// loading提醒文字
loadingMsg: '加载中',
// 需要授权才能请求 默认放开
auth: false,
// 是否传递 token
isToken: true,
};
/** 登录页路径 */
const LOGIN_PAGE = '/pages7/pages/index/login';
/** 跳转到登录页(navigateTo 失败时 reLaunch,避免页面栈异常) */
function navigateToLogin() {
uni.navigateTo({
url: LOGIN_PAGE,
fail: () => {
uni.reLaunch({ url: '/pages/xunji/xunji' });
},
});
}
// Loading全局实例
let LoadingInstance = {
target: null,
count: 0,
};
/**
* 关闭loading
*/
function closeLoading() {
if (LoadingInstance.count > 0) LoadingInstance.count--;
if (LoadingInstance.count === 0) uni.hideLoading();
}
/**
* @description 请求基础配置 可直接使用访问自定义请求
*/
const http = new Request({
baseURL: baseUrl + apiPath,
timeout: 8000,
method: 'GET',
header: {
Accept: 'text/json',
'Content-Type': 'application/json;charset=UTF-8',
platform: $platform.name,
},
// #ifdef APP-PLUS
sslVerify: false,
// #endif
// #ifdef H5
// 跨域请求时是否携带凭证(cookies)仅H5支持(HBuilderX 2.6.15+)
withCredentials: false,
// #endif
custom: options,
});
/**
* @description 请求拦截器
*/
http.interceptors.request.use(
(config) => {
// 自定义处理【auth 授权】:必须登录的接口,未登录则跳转登录页
if (config.custom.auth && !$store('user').isLogin) {
navigateToLogin();
return Promise.reject();
}
// 自定义处理【loading 加载中】:如果需要显示 loading,则显示 loading
if (config.custom.showLoading) {
LoadingInstance.count++;
LoadingInstance.count === 1 &&
uni.showLoading({
title: config.custom.loadingMsg,
mask: true,
fail: () => {
uni.hideLoading();
},
});
}
// 增加 token 令牌、terminal 终端、tenant 租户的请求头
const token = config.custom.isToken ? getAccessToken() : undefined;
if (token) {
config.header['Authorization'] = token;
}
config.header['terminal'] = getTerminal();
config.header['Accept'] = '*/*';
config.header['tenant-id'] = getTenantId();
return config;
},
(error) => {
return Promise.reject(error);
},
);
/**
* @description 响应拦截器
*/
http.interceptors.response.use(
(response) => {
// 约定:如果是 /auth/ 下的 URL 地址,并且返回了 accessToken 说明是登录相关的接口,则自动设置登录令牌
if (
response.config.url.indexOf('/app/auth/refreshToken') >= 0 &&
response.data?.data?.accessToken
) {
$store('user').setToken(response.data.data.accessToken, response.data.data.refreshToken);
}
// 自定处理【loading 加载中】:如果需要显示 loading,则关闭 loading
// response.config.custom.showLoading && closeLoading();
if (response.config.custom.showLoading) {
closeLoading();
}
// 自定义处理【error 错误提示】:如果需要显示错误提示,则显示错误提示
if (response.data.code !== 0) {
// 特殊:如果 401 错误码,则跳转到登录页 or 刷新令牌
if (response.data.code == 401) {
if ($store('user').isLogin) {
return refreshToken(response.config);
} else {
uni.showToast({
title: '请先登录',
icon: 'none',
mask: true,
});
navigateToLogin();
return Promise.reject(new Error());
}
}
// 特殊:处理分销用户绑定失败的提示
// if ((response.data.code + '').includes('1011007')) {
// console.error(`分销用户绑定失败,原因:${response.data.msg}`);
// } else if (response.config.custom.showError) {
// // 错误提示
// uni.showToast({
// title: response.data.msg || '服务器开小差啦,请稍后再试~',
// icon: 'none',
// mask: true,
// });
// }
uni.showToast({
title: response.data?.msg || '服务器开小差啦,请稍后再试~',
icon: 'none',
mask: true,
});
return Promise.reject(new Error(response.data.msg));
}
// 自定义处理【showSuccess 成功提示】:如果需要显示成功提示,则显示成功提示
if (
response.config.custom.showSuccess &&
response.config.custom.successMsg !== '' &&
response.data.code === 0
) {
uni.showToast({
title: response.config.custom.successMsg,
icon: 'none',
});
}
// 返回结果:包括 code + data + msg
return Promise.resolve(response.data);
},
(error) => {
const userStore = $store('user');
const isLogin = userStore.isLogin;
let errorMessage = '网络请求出错';
if (error !== undefined) {
switch (error.statusCode) {
case 400:
errorMessage = '请求错误';
break;
case 401:
errorMessage = isLogin ? '您的登录已过期' : '请先登录';
if (!error.config?.url?.includes('/app/auth/refreshToken')) {
if (isLogin) {
handleAuthorized();
} else {
navigateToLogin();
}
}
break;
case 403:
errorMessage = '拒绝访问';
break;
case 404:
errorMessage = '请求出错';
break;
case 408:
errorMessage = '请求超时';
break;
case 429:
errorMessage = '请求频繁, 请稍后再访问';
break;
case 500:
errorMessage = '服务器开小差啦,请稍后再试~';
break;
case 501:
errorMessage = '服务未实现';
break;
case 502:
errorMessage = '网络错误';
break;
case 503:
errorMessage = '服务不可用';
break;
case 504:
errorMessage = '网络超时';
break;
case 505:
errorMessage = 'HTTP 版本不受支持';
break;
}
if (error.errMsg.includes('timeout')) errorMessage = '请求超时';
// #ifdef H5
if (error.errMsg.includes('Network'))
errorMessage = window.navigator.onLine ? '服务器异常' : '请检查您的网络连接';
// #endif
}
if (error && error.config) {
if (error.config.custom.showError) {
uni.showToast({
title: error.data?.msg || errorMessage,
icon: 'none',
mask: true,
});
}
error.config.custom.showLoading && closeLoading();
}
return Promise.reject(error);
},
);
// Axios 无感知刷新令牌,参考 https://www.dashingdog.cn/article/11 与 https://segmentfault.com/a/1190000020210980 实现
let requestList = []; // 请求队列
let isRefreshToken = false; // 是否正在刷新中
const refreshToken = async (config) => {
// 如果当前已经是 refresh-token 的 URL 地址,并且还是 401 错误,说明是刷新令牌失败了,直接返回 Promise.reject(error)
if (config.url.indexOf('/app/auth/refreshToken') >= 0) {
return Promise.reject('error');
}
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
if (!isRefreshToken) {
isRefreshToken = true;
// 1. 如果获取不到刷新令牌,则只能执行登出操作
const refreshToken = getRefreshToken();
if (!refreshToken) {
return handleAuthorized();
}
// 2. 进行刷新访问令牌
try {
const refreshTokenResult = await AuthUtil.refreshToken(refreshToken);
if (refreshTokenResult.code !== 0) {
// 如果刷新不成功,直接抛出 e 触发 2.2 的逻辑
// noinspection ExceptionCaughtLocallyJS
throw new Error('刷新令牌失败');
}
// 2.1 刷新成功,则回放队列的请求 + 当前请求
const newToken = getAccessToken();
// 执行队列回放并把新 Token 传过去
requestList.forEach((cb) => cb(newToken));
requestList = [];
config.header.Authorization = newToken;
return request(config);
} catch (e) {
isRefreshToken = false;
requestList = []; // 彻底清空队列,拒绝死循环重试
return handleAuthorized();
} finally {
requestList = [];
isRefreshToken = false;
}
} else {
// 添加到队列,等待刷新获取到新的令牌
return new Promise((resolve) => {
requestList.push(() => {
config.header.Authorization = getAccessToken();
resolve(request(config));
});
});
}
};
/**
* 处理 401 未登录的错误
*/
const handleAuthorized = () => {
const userStore = $store('user');
const wasLogin = userStore.isLogin;
userStore.logout(true);
navigateToLogin();
// 登录超时 / 令牌失效
return Promise.reject({
code: 401,
msg: wasLogin ? '您的登录已过期' : '请先登录',
});
};
/** 获得访问令牌 */
export const getAccessToken = () => {
return uni.getStorageSync('token');
};
/** 获得刷新令牌 */
export const getRefreshToken = () => {
return uni.getStorageSync('refresh-token');
};
/** 获得租户编号 */
export const getTenantId = () => {
return uni.getStorageSync('tenant-id') || tenantId;
};
const request = (config) => {
return http.middleware(config);
};
export default request;