zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
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
import type { RequestOptions } from '@@/plugin-request/request';
import type { RequestConfig } from '@umijs/max';
import { message } from 'antd';
import { history } from '@umijs/max';
import { decryptData } from '@/utils/utils';
 
const runtimeBaseUrl = typeof BASE_URL === 'undefined' ? '' : BASE_URL;
 
/**
 * @name 错误处理
 * pro 自带的错误处理, 可以在这里做自己的改动
 * @doc https://umijs.org/docs/max/request#配置
 */
export const errorConfig: RequestConfig = {
  baseURL: runtimeBaseUrl,
 
  // 请求拦截器
  requestInterceptors: [
    (config: RequestOptions) => {
      const authorization = decryptData(localStorage.getItem('token')) || '';
      const headers = {
        ...config.headers,
        Authorization: authorization,
      };
      const url = config?.url;
      return {
        ...config,
        url,
        headers,
      };
    },
  ],
 
  // 响应拦截器
  responseInterceptors: [
    (response: any) => {
      // 拦截响应数据,进行个性化处理
      const { data } = response as { data?: { code?: number; msg?: string } };
      if (data?.code === 103 || data?.code === 401) {
        localStorage.clear()
        history.replace('/login')
        return Promise.resolve(response)
      }
      if (typeof data?.code === 'number' && data.code !== 0) {
        message.error(data.msg);
        return Promise.reject(response)
      }
      return Promise.resolve(response)
    },
  ],
};