charles
2024-07-18 059f2a762ac88bb7ec20454a455b4ed2d43877b3
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
import axios from "axios"
import { Message } from "element-ui"
import {getApsPage} from './index.js';
// import router from '@/router'
// 开发环境下将自己的token复制到这里, 也可以在浏览器中手动添加token到cookie中,cookie中的token优先
const DEV_TOKEN =  ''
function environmentType(){
  let type
  if (location.href.includes('192.168.20.119')) {
      type = 'test'
  } else if (location.href.includes('192.168') || location.href.includes('localhost')) {
      type = 'dev'
  } else {
      type = 'prod'
  }
 
  return type
}
 
const isDev = environmentType() === 'dev'
 
const Axios = axios.create({
  responseType: "json",
  withCredentials: true // 是否允许带cookie这些
})
/*const getApsPage = () => {
  // 首页部署在各个环境的端口
  const loginPathMap = {
      prod:`//${window.location.hostname}:9080`,
      test:`//192.168.20.119:9080`,
      dev: `//192.168.8.117:8080`
  }
  return loginPathMap[environmentType()]
}*/
 
/* //POST传参序列化(添加请求拦截器) */
Axios.interceptors.request.use(
  (config) => {
    // // 若是有做鉴权token , 就给头部带上token
    // // let token = util.cookies.get("token");
    // // if (token != undefined) {
    // //   config.headers.Authorization = "Bearer " + token;
    // // }
    let token = document.cookie.replace(
      /(?:(?:^|.*;\s*)token\s*=\s*([^;]*).*$)|^.*$/,
      "$1",
    );
    if (isDev){
      token = token || DEV_TOKEN
    }
    if (token) {
      config.headers.Authorization = "Bearer " + token;
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
 
/* //返回状态判断(添加响应拦截器) */
Axios.interceptors.response.use(
  (res) => {
    /* //对响应数据做些事 */
    if (res.data.code === 200) {
      return res.data ? res.data : {}
    } else if([2012,2013,2014,2015].includes(res.data.code)){
      if (isDev){
        alert("JWT失效,即将跳转至登录页..")
        window.location = getApsPage()+'/login'
      }else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/login'
      }
      // Message({
      //   message: res.data.msg,
      //   type: "error",
      //   duration: 5 * 1000
      // })
      return Promise.reject(res.data)
    }else if([2036].includes(res.data.code)){
      if (isDev){
        window.location = getApsPage()+'/commonWeb?resetPwd=true'
      }else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/commonWeb?resetPwd=true'
      }
      return Promise.reject(res.data)
 
      // if(window.location.pathname && window.location.pathname !== '/login'){
      //   window.location = window.location.origin+'/login'
      // }
    }
  },
  (error) => {
    if(error.response.status === 401){
      if (isDev){
        alert("JWT失效,即将跳转至登录页..")
        window.location = getApsPage()+'/login'
      }else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/login'
      }
    }
    let { message } = error
    if (message === "Network Error") {
      message = "后端接口连接异常"
    } else if (message.includes("timeout")) {
      message = "系统接口请求超时"
    } else if (message.includes("Request failed with status code")) {
      message = "系统接口" + message.substr(message.length - 3) + "异常"
    }
    Message({
      message: message,
      type: "error",
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)
 
export default Axios