wujingjing
2024-12-31 88f7847db37cf7c4822f398117016b25829c969e
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {qsparse, toast} from 'amis';
import axios, {CancelToken} from 'axios';
 
import qs from 'qs';
 
const MAIN_URL =
  (window?.parent as any)?.globalConfig?.WebApiUrl?.MainUrl ??
  'http://wi.cpolar.top';
//#region ====================== 后端 res.err_code ======================
export const enum ErrorCode {
  /** @description 权限验证失败 */
  Message = 'MESSAGE',
  /** @description 内部错误 */
  Exception = 'EXCEPTION',
  /** @description 无权使用 */
  Auth = 'AUTH'
}
//#endregion
// 配置新建一个 axios 实例
const createAxiosInstance = () => {
  return axios.create({
    baseURL: MAIN_URL,
    timeout: 50000,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  });
};
 
export type ExtraConfigData = {
  cancelToken?: CancelToken | undefined;
};
// export const accessSessionKey = 'access-session';
const request = createAxiosInstance();
 
const handleNoAuth = () => {
  (window.parent as any)?.handleNoAuth?.();
};
request.interceptors.request.use(
  config => {
    const paramsStr = location.href.split('?')[1];
    const paramsSession =
      paramsStr && (qs.parse(paramsStr).session as any)?.split('#')?.[0];
    const accessSession = paramsSession;
    if (accessSession) {
      config.headers['hswatersession'] = accessSession;
    } else {
      handleNoAuth();
    }
 
    if (config.data) {
      config.data = qs.stringify(config.data);
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);
 
request.interceptors.response.use(res => {
  // 获取状态码和返回数据
  const status = res.status;
  const serveData = res.data;
 
  if (!serveData) {
    throw new Error('请求失败');
  }
 
  // 处理 401
  if (status === 401) {
    // clearAccessTokens();
  }
 
  // 响应拦截及自定义处理
  if (!serveData.json_ok) {
    switch (serveData?.err_code) {
      case ErrorCode.Auth:
        handleNoAuth();
        throw '权限验证失败';
      case ErrorCode.Exception:
        // ElMessage.error('内部错误!');
        toast.error('内部错误');
        throw '内部错误';
    }
    // 非 message error,且 handleFail 为 true
    // message error 不需要处理
    if (serveData?.err_code !== ErrorCode.Message) {
      const errorText = serveData?.json_msg || '响应失败!';
      toast.error(errorText);
      throw errorText;
    }
  }
  return res.data;
});
 
export const getPageId = () => {
  const str = window.location.href.split('?')[2];
  let currentId: any;
  if (str) {
    const strObj = qsparse(str);
    currentId = strObj?.id;
  } else if ((window?.parent as any)?.currentPage?.id) {
    currentId = (window?.parent as any)?.currentPage?.id;
  } else {
    const getId = localStorage.getItem('currentId');
    currentId = getId;
  }
  localStorage.setItem('currentId', currentId);
  return currentId;
};
 
export default request;