helongyang
2025-06-03 166a45a9d44e03e63552c6afa975c9ef0cc902e5
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
// 这个时axios的配置
import axios from 'axios';
// import { config } from 'vue/types/umd';
axios.defaults.withCredentials=true;
axios.defaults.crossDomain=true;
axios.defaults.baseURL = 'http://10.30.4.92:7081';//'http://192.168.2.51:8099'; //'http://localhost:8099'; //'http://192.168.2.51:8099';//
axios.defaults.headers.post["Content-Type"]="application/json;charset=utf-8";
// 错误信息处理
const  errorHandle = (status, other) => {
  switch (status) {
    case 400:
      console.log('信息验证失败');
      break;
    case 401:
      console.log('认证失败');
      break;
    case 403:
      localStorage.removeItem("token");
      console.log('token校验失败');
      break;
    case 404:
      console.log('请求资源不存在');
      break;
    default :
      console.log(other);
      break;
  }
}
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  // console.log(config);
  if(localStorage.elementToken){
    config.headers.Authorization=localStorage.elementToken;
    config.headers.con;
  }
  // console.log(config);
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});
 
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  // console.log();
  // console.log(response.data.token);
  // response.headers['Authorization'] = response.data.token;
  return response.status=== 200 ? response.data: response.data;
}, function (error) {
  // 对响应错误做点什么
  const {response}=error;
  if(response){
    errorHandle(response.status,response.data.message)
    return Promise.reject(response.data);
  }else{
    
  }
});
export default axios;