二手回收后台,师傅端,用户端
刘帅
2026-09-11 9631ee1b81be30f68b9cd3878dc64bb9c414a1ff
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';
 
// 错误处理方案: 错误类型
enum ErrorShowType {
  SILENT = 0,
  WARN_MESSAGE = 1,
  ERROR_MESSAGE = 2,
  NOTIFICATION = 3,
  REDIRECT = 9,
}
// 与后端约定的响应数据格式
interface ResponseStructure {
  success: boolean;
  data: any;
  errorCode?: number;
  errorMessage?: string;
  showType?: ErrorShowType;
}
 
/**
 * @name 错误处理
 * pro 自带的错误处理, 可以在这里做自己的改动
 * @doc https://umijs.org/docs/max/request#配置
 */
export const errorConfig: RequestConfig = {
  // baseURL: 'http://192.168.110.34:9101',
  baseURL: BASE_URL,
 
  // 请求拦截器
  requestInterceptors: [
    (config: RequestOptions) => {
      const o = config;
      const Authorization = localStorage.getItem('token') || '';
      const isFormData = typeof FormData !== 'undefined' && config.data instanceof FormData;
      o.headers = {
        ...config.headers,
        Authorization: Authorization,
        ...(isFormData ? {} : { 'Content-Type': 'application/json' }),
      };
      let url = ''
      // 拦截请求配置,进行个性化处理。
      if (['/auth/login'].includes(config?.url) ) {
        url = config?.url;
      } else {
        url = '/admin' + config?.url;
      }
      // const url = config?.url;
 
      return { ...config, url };
    },
  ],
 
  // 响应拦截器
  responseInterceptors: [
    (response: any) => {
      // 拦截响应数据,进行个性化处理
      const { data } = response as unknown as ResponseStructure;
 
      if (data?.code == 103 || data?.code == 401 || data?.code == 510) {
        history.replace('/login');
        return response
      } else if (data?.code == 500) {
        message.error(data.msg || '服务器错误');
        return { data: { code: 500, data: null } };
      } else if (data?.code && data?.code != 200) {
        message.error(data.msg || '系统错误');
      } else {
        return response;
      }
    },
  ],
};