“dzb”
2022-10-11 387e8b0743240de72425e5b5c5709d629ace9aa4
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
import axios from 'axios';
import router from '@/router'
// 引入elementUI
import tip from 'element-ui';
const api = axios.create({
  baseURL: 'http://42.193.1.25:8082/', // 请求的公共地址部分
  timeout: 15000
})
// 拦截器
// 请求
api.interceptors.request.use((config) => {
  const token = sessionStorage.getItem('token');
  const tokenHead = sessionStorage.getItem('tokenHead');
  if (token && tokenHead) {
    config.headers.Authorization = tokenHead + token;
  }
  return config;
}, (err) => {
  console.log(err);
}
)
// 响应
api.interceptors.response.use(
  function (response) {
    if (response.data.code === 401) {
      tip.Message({
        type: 'warning',
        message: '登录身份已过期',
      })
      router.push('/login');
    }
    return response.data;
  },
  function (error) {
    console.log(error);
    if(error.code === "ECONNABORTED"){
      tip.Message({
        type:'error',
        message:'网络请求超市',
        duration: 3 * 1000,
      })
      return;
    }
    const {code,status} = error.response.data;
    if (error.code === 'ERR_NETWORK') {
      console.log(1);
      tip.Message({
        type: 'error',
        message: '服务器故障'
      })
      return;
    }
    if (code === 401) {
      console.log(2);
      tip.Message({
        type: 'warning',
        message: '登录身份已过期',
      })
      router.push('/login');
      return;
    }
    if (status === 500) {
      console.log(3);
      tip.Message({
        type: 'error',
        message: '网络请求出错'
      })
      return;
    }
  }
);
export default api;