Blame view

src/app.ts 3.32 KB
calmound authored
1
2
// 运行时配置
sanmu authored
3
import { RequestConfig, history } from '@umijs/max';
calmound authored
4
calmound authored
5
6
7
8
import '@inspir/assembly-css/dist/special.css';
import { message } from 'antd';
import { RESPONSE_CODE } from './constants/enum';
sanmu authored
9
import './style/global.css';
zhongnanhuang authored
10
import { getUserInfo } from './utils';
calmound authored
11
calmound authored
12
13
// 全局初始化数据配置,用于 Layout 用户信息和权限初始化
// 更多信息见文档:https://umijs.org/docs/api/runtime-config#getinitialstate
zhongnanhuang authored
14
15
export async function getInitialState() {
  return getUserInfo();
calmound authored
16
17
18
19
20
21
}

export const layout = () => {
  return {
    menu: {
      locale: false,
calmound authored
22
23
24
25
      // header: true,
      headerRender: true,
      // rightContentRender: () => <RightContent />,
      // footerRender: () => <Footer />,
calmound authored
26
    },
zhongnanhuang authored
27
    // collapsed: true,
calmound authored
28
29
30
31
32
33
  };
};

export const request: RequestConfig = {
  // 错误处理
  errorHandler: (error) => {
sanmu authored
34
35
36
37
38
    console.log(
      '%c [ error ]-35',
      'font-size:13px; background:pink; color:#bf2c9f;',
      error,
    );
calmound authored
39
40
41
    const { response, data } = error;
    if (response && response.status) {
      // 401重定向
calmound authored
42
      if (response.status === 401) {
sanmu authored
43
        if (!location.pathname.includes('login')) {
calmound authored
44
          localStorage.removeItem('token');
sanmu authored
45
          localStorage.removeItem('userInfo');
calmound authored
46
          message.error('token失效,请重新登录!');
sanmu authored
47
          history.push('/login');
calmound authored
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
          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
67
68
69
      // const requestOption: AxiosRequestConfig = {
      //   method: options.method || 'get',
      // };
calmound authored
70
sanmu authored
71
72
73
      // if (options.header) {
      //   requestOption.headers = options.header;
      // }
calmound authored
74
sanmu authored
75
76
77
      // if (options.body) {
      //   options.data = options.body;
      // }
calmound authored
78
79
80
81
82
83

      if (options.query) {
        options.params = options.query;
      }

      return {
sanmu authored
84
        url: '/api' + url,
calmound authored
85
86
87
88
89
90
91
92
93
        options: {
          ...options,
          signal,
          interceptors: true,
          headers: authHeader,
        },
      };
    },
  ],
calmound authored
94
95
96
97
98
99
  responseInterceptors: [
    // 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
    [
      (response) => {
        // 不再需要异步处理读取返回体内容,可直接在data中读出,部分字段可在 config 中找到
        const { data = {} as any } = response;
zhongnanhuang authored
100
101
102
103
        if (
          data.result !== RESPONSE_CODE.SUCCESS &&
          data.result !== undefined
        ) {
zhongnanhuang authored
104
          message.error(data.message);
calmound authored
105
        }
sanmu authored
106
107
108
109
110
        if (data.result === 401) {
          history.push('/login');
        }
calmound authored
111
112
113
        // do something
        return response;
      },
sanmu authored
114
115
116
117
118
119
120
121
122
      (error: any) => {
        if (error?.response?.status === 401) {
          if (!location.pathname.includes('login')) {
            localStorage.removeItem('token');
            localStorage.removeItem('userInfo');
            message.error('token失效,请重新登录!');
            history.push('/login');
          }
        }
calmound authored
123
124
125
126
        return Promise.reject(error);
      },
    ],
  ],
calmound authored
127
};