This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.ts
121 lines (115 loc) · 3.48 KB
/
request.ts
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
import Http from 'luch-request';
import statuses from 'statuses';
import { constantCase } from 'change-case';
import * as AxiosLogger from 'axios-logger';
import axiosRetry from 'axios-retry';
import { showModal } from '@/utils/modal';
import { getToken } from '@/utils/storage';
import { DefaultHeaders } from '@/constants';
export const reLaunchCodes = new Set(['TOKEN_OUTDATED']);
/** @desc 错误统一处理方法 */
export const handleShowError = (response: IResponse) => {
if (reLaunchCodes.has(response.code)) {
showModal({
content: '请重新登录',
success: () => {
uni.clearStorageSync();
uni.reLaunch({
url: '/pages/index',
});
},
});
} else {
showModal({
content: `发生了错误,错误代码:${response.code},错误信息:${response.message}。`,
});
}
};
/** @desc 请求实例 */
const instance = new Http({
baseURL: process.env.VUE_APP_REQUEST_BASE_URL || '',
header: {
...DefaultHeaders,
},
sslVerify: false,
});
instance.interceptors.request.use((config) => ({
...config,
header: {
...config.header,
'X-Token': getToken() || '',
},
}));
if (process.env.NODE_ENV === 'development') {
instance.interceptors.request.use(
// @ts-ignore
(request) => AxiosLogger.requestLogger(request, { prefixText: false }),
// @ts-ignore
(error) => AxiosLogger.errorLogger(error, { prefixText: false }),
);
}
// @ts-ignore
axiosRetry(instance, { retryDelay: axiosRetry.exponentialDelay });
if (process.env.NODE_ENV === 'development') {
instance.interceptors.response.use(
// @ts-ignore
(response) => AxiosLogger.responseLogger(response, { prefixText: false }),
// @ts-ignore
(error) => AxiosLogger.errorLogger(error, { prefixText: false }),
);
}
instance.interceptors.response.use(
(response) => {
const { data, config } = response;
if (!data.success && config.custom?.showError !== false) {
handleShowError(data);
}
return data;
},
(error) => {
if (error.errMsg.includes('request:fail abort')) {
return {
success: false,
message: '请求已取消',
code: 'REQUEST_CANCELLED',
};
}
const response: IResponse = {
success: false,
message: '',
code: '',
};
if (error.statusCode && (error.statusCode < 200 || error.statusCode >= 300)) {
try {
response.code = constantCase(statuses(error.statusCode).toString());
response.message = constantCase(statuses(error.statusCode).toString());
} catch {
response.code = 'ERROR_OCCURRED';
response.message = '发生了错误';
}
} else if (
error.errMsg === 'request:fail abort statusCode:-1' ||
error.errMsg.toUpperCase().includes('TIMEOUT') ||
error.errMsg.toUpperCase().includes('CONNRESET') ||
error.errMsg.toUpperCase().includes('CONNECTION_RESET')
) {
// 超时
response.message = '请求超时';
response.code = 'REQUEST_TIMEOUT';
} else if (error.data && Object.keys(error.data).length === 0) {
// 发送了请求,没有收到响应
response.message = '服务器无响应';
response.code = 'NO_RESPONSE';
} else {
// 请求时发生错误
response.message = '请求错误';
response.code = 'REQUEST_ERROR';
}
// 处理错误
if (error.config.custom?.showError !== false) {
handleShowError(response);
}
return response;
},
);
export const request = instance.middleware.bind(instance);