app.ts
2.07 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
// 运行时配置
import { RequestConfig } from '@umijs/max';
import { AxiosRequestConfig } from 'axios';
// 全局初始化数据配置,用于 Layout 用户信息和权限初始化
// 更多信息见文档:https://umijs.org/docs/api/runtime-config#getinitialstate
export async function getInitialState(): Promise<{ name: string }> {
return { name: '@umijs/max' };
}
export const layout = () => {
return {
logo: 'https://img.alicdn.com/tfs/TB1YHEpwUT1gK0jSZFhXXaAtVXa-28-27.svg',
menu: {
locale: false,
},
};
};
export const request: RequestConfig = {
// 错误处理
errorHandler: (error) => {
const { response, data } = error;
if (response && response.status) {
// 401重定向
if (response.status == 401) {
if (!location.pathname.includes(loginPath)) {
localStorage.removeItem('token');
message.error('token失效,请重新登录!');
// history.push(loginPath);
return null;
}
}
}
return data;
},
// 请求处理
requestInterceptors: [
(url: string, options) => {
console.log(
'%c [ options ]-41',
'font-size:13px; background:pink; color:#bf2c9f;',
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,
};
}
const requestOption: AxiosRequestConfig = {
method: options.method || 'get',
};
if (options.header) {
requestOption.headers = options.header;
}
if (options.body) {
requestOption.data = options.body;
}
if (options.query) {
options.params = options.query;
}
return {
url: `/api${url}`,
options: {
...options,
signal,
interceptors: true,
headers: authHeader,
},
};
},
],
};