riku
2025-03-14 5f44d21b3921abc88506a7ec46b3fe6f078664aa
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import axios from 'axios';
import { ElMessage } from 'element-plus';
 
const debug = false;
 
let ip1 = 'http://47.100.191.150:9029/';
// console.log(import.meta.env);
if (import.meta.env.VITE_DATA_MODE == 'jingan') {
  ip1 = 'http://114.215.109.124:8805/';
}
 
if (debug) {
  ip1 = 'http://192.168.0.138:8084/';
}
 
const $http = axios.create({
  baseURL: ip1,
  timeout: 30000
});
 
//添加拦截器
[$http].forEach((i) => {
  // 添加请求拦截器
  i.interceptors.request.use(
    function (config) {
      // 在发送请求之前做些什么
      if (import.meta.env.DEV) {
        console.log('==>请求开始');
        console.log(`${config.baseURL}${config.url}`);
        if (config.data) {
          console.log('==>请求数据', config.data);
        }
      }
      return config;
    },
    function (error) {
      // 对请求错误做些什么
      if (import.meta.env.DEV) {
        console.log('==>请求开始');
        console.log(error);
      }
      ElMessage({
        message: error,
        type: 'error'
      });
      return Promise.reject(error);
    }
  );
 
  // 添加响应拦截器
  i.interceptors.response.use(
    function (response) {
      // 2xx 范围内的状态码都会触发该函数。
      // 对响应数据做点什么
      if (import.meta.env.DEV) {
        console.log(response);
        console.log('==>请求结束');
      }
      if (response.status == 200) {
        if (
          response.data.success != undefined &&
          response.data.success != null
        ) {
          if (response.data.success == true) {
            return response;
          } else {
            ElMessage({
              message: response.data.message,
              type: 'error'
            });
            return Promise.reject(response.data.message);
          }
        } else {
          return response;
        }
      } else {
        return Promise.reject(response);
      }
    },
    function (error) {
      // 超出 2xx 范围的状态码都会触发该函数。
      // 对响应错误做点什么
      if (import.meta.env.DEV) {
        console.log(error);
        console.log('==>请求结束');
      }
      ElMessage({
        message: error,
        type: 'error'
      });
      return Promise.reject(error);
    }
  );
});
 
// const $http = {
//   get(url) {
//     const controller = new AbortController();
//     return {
//       con: controller,
//       resp: axiosInstance.get(url, { signal: controller.signal })
//     };
//   },
//   put(url, data) {
//     const controller = new AbortController();
//     return {
//       con: controller,
//       resp: axiosInstance.put(url, data, { signal: controller.signal })
//     };
//   },
//   post(url, data) {
//     const controller = new AbortController();
//     return {
//       con: controller,
//       resp: axiosInstance.post(url, data, { signal: controller.signal })
//     };
//   },
//   delete(url) {
//     const controller = new AbortController();
//     return {
//       con: controller,
//       resp: axiosInstance.delete(url, { signal: controller.signal })
//     };
//   }
// };
 
function resToData(res) {
  res.resp = res.resp.then((res) => res.data);
  return res;
}
 
export { $http, resToData };