|
1
2
|
// 运行时配置
|
sanmu
authored
|
3
|
import { RequestConfig, history } from '@umijs/max';
|
|
4
|
|
|
5
6
7
8
|
import '@inspir/assembly-css/dist/special.css';
import { message } from 'antd';
import { RESPONSE_CODE } from './constants/enum';
|
|
9
10
|
import { RunTimeLayoutConfig } from '@umijs/max';
import GlobleHeader from './components/UserHeader';
|
sanmu
authored
|
11
|
import './style/global.css';
|
|
12
|
import { getUserInfo } from './utils';
|
|
13
|
|
|
14
15
|
// 全局初始化数据配置,用于 Layout 用户信息和权限初始化
// 更多信息见文档:https://umijs.org/docs/api/runtime-config#getinitialstate
|
|
16
17
|
export async function getInitialState() {
return getUserInfo();
|
|
18
19
|
}
|
|
20
|
export const layout: RunTimeLayoutConfig = () => {
|
|
21
|
return {
|
|
22
23
24
25
26
27
|
headerRender: GlobleHeader,
siderWidth: '190px',
layout: 'mix',
// 其他属性见:https://procomponents.ant.design/components/layout#prolayout
|
|
28
29
|
};
};
|
|
30
31
32
33
34
35
36
37
38
39
40
41
42
|
// export const layout = () => {
// return {
// menu: {
// locale: false,
// header: GlobleHeader,
// headerRender:GlobleHeader,
// rightContentRender: () => GlobleHeader,
// // footerRender: () => <Footer />,
// },
// // collapsed: true,
// // breakpoint:false
// };
// };
|
|
43
44
45
46
|
export const request: RequestConfig = {
// 错误处理
errorHandler: (error) => {
|
sanmu
authored
|
47
48
49
50
51
|
console.log(
'%c [ error ]-35',
'font-size:13px; background:pink; color:#bf2c9f;',
error,
);
|
|
52
53
54
|
const { response, data } = error;
if (response && response.status) {
// 401重定向
|
|
55
|
if (response.status === 401) {
|
sanmu
authored
|
56
|
if (!location.pathname.includes('login')) {
|
|
57
|
localStorage.removeItem('token');
|
sanmu
authored
|
58
|
localStorage.removeItem('userInfo');
|
|
59
|
message.error('token失效,请重新登录!');
|
sanmu
authored
|
60
|
history.push('/login');
|
|
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
return null;
}
}
}
return data;
},
// 请求处理
requestInterceptors: [
(url: string, options) => {
const controller = new AbortController(); // create a controller
const { signal } = controller;
let authHeader: { Authorization?: string; systemid?: string } = {};
if (localStorage.getItem('token')) {
authHeader = {
Authorization: localStorage.getItem('token') || undefined,
};
}
|
sanmu
authored
|
80
81
82
|
// const requestOption: AxiosRequestConfig = {
// method: options.method || 'get',
// };
|
|
83
|
|
sanmu
authored
|
84
85
86
|
// if (options.header) {
// requestOption.headers = options.header;
// }
|
|
87
|
|
sanmu
authored
|
88
89
90
|
// if (options.body) {
// options.data = options.body;
// }
|
|
91
92
93
94
95
96
|
if (options.query) {
options.params = options.query;
}
return {
|
sanmu
authored
|
97
|
url: '/api' + url,
|
|
98
99
100
101
102
103
104
105
106
|
options: {
...options,
signal,
interceptors: true,
headers: authHeader,
},
};
},
],
|
|
107
108
109
110
111
112
|
responseInterceptors: [
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
[
(response) => {
// 不再需要异步处理读取返回体内容,可直接在data中读出,部分字段可在 config 中找到
const { data = {} as any } = response;
|
|
113
114
115
116
|
if (
data.result !== RESPONSE_CODE.SUCCESS &&
data.result !== undefined
) {
|
|
117
|
message.error(data.message);
|
|
118
|
}
|
sanmu
authored
|
119
|
|
|
120
121
122
123
|
if (data.result === 401) {
history.push('/login');
}
|
|
124
125
126
|
// do something
return response;
},
|
sanmu
authored
|
127
128
129
130
131
132
133
134
135
|
(error: any) => {
if (error?.response?.status === 401) {
if (!location.pathname.includes('login')) {
localStorage.removeItem('token');
localStorage.removeItem('userInfo');
message.error('token失效,请重新登录!');
history.push('/login');
}
}
|
|
136
137
138
139
|
return Promise.reject(error);
},
],
],
|
|
140
|
};
|