-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestConfig.ts
More file actions
75 lines (70 loc) · 1.98 KB
/
requestConfig.ts
File metadata and controls
75 lines (70 loc) · 1.98 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
import type {RequestOptions} from '@@/plugin-request/request';
import type {RequestConfig} from '@umijs/max';
import {history} from '@umijs/max';
import {message} from 'antd';
// 与后端约定的响应数据格式
interface ResponseStructure {
success: boolean;
data: any;
errorCode?: number;
errorMessage?: string;
}
/**
* @name 请求拦截处理
* @doc https://umijs.org/docs/max/request#配置
*/
export const requestConfig: RequestConfig = {
baseURL: process.env.NODE_ENV === 'production' ?
"https://back.freefish.love/" :
'http://localhost:9001/',
withCredentials: true,
// 请求拦截器
requestInterceptors: [
(config: RequestOptions) => {
const token = localStorage.getItem("token");
if (config && config.headers && token) {
config.headers.token = JSON.parse(token);
}
return config;
},
],
// 响应拦截器
responseInterceptors: [
(response) => {
// 拦截响应数据,进行个性化处理
const {data} = response as unknown as ResponseStructure;
const {code} = data;
const {pathname} = location;
const loginPath = '/user/login';
if (data && code === 0) {
return response;
} else {
switch (code) {
case 40001:
if (!pathname.includes('/interfaceInfo/')) {
message.error(data.message);
history.push(loginPath);
}
break;
case 40100:
if (pathname !== '/'
&& pathname !== '/interface/list'
&& pathname !== '/user/login'
&& pathname !== '/user/register'
&& pathname !== '/analyse'
) {
message.error(data.message);
history.push(loginPath);
}
break;
default:
if (!pathname.includes('/interfaceInfo/')) {
message.error(data.message);
}
break;
}
}
return response;
},
],
};