yangfeng
2024-03-23 12cf68ea971e5c39e884474734ef51cbc52bec8d
src/common/untils/request.js
@@ -3,21 +3,21 @@
// import router from '@/router'
// 开发环境下将自己的token复制到这里, 也可以在浏览器中手动添加token到cookie中,cookie中的token优先
const DEV_TOKEN =  ''
function environmentType(){
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'
  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'
    type = "prod"
  }
  return type
}
const isDev = environmentType() === 'dev'
const isDev = environmentType() === "dev"
const Axios = axios.create({
  responseType: "json",
@@ -26,9 +26,9 @@
const getApsPage = () => {
  // 首页部署在各个环境的端口
  const loginPathMap = {
      prod:`//${window.location.hostname}:9080`,
      test:`//192.168.20.119:9080`,
      dev: `//192.168.8.107:8080`
    prod: `//${window.location.hostname}:9080`,
    test: `//192.168.20.119:9080`,
    dev: `//localhost:8081`
  }
  return loginPathMap[environmentType()]
}
@@ -41,15 +41,13 @@
    // // if (token != undefined) {
    // //   config.headers.Authorization = "Bearer " + token;
    // // }
    let token = document.cookie.replace(
      /(?:(?:^|.*;\s*)token\s*=\s*([^;]*).*$)|^.*$/,
      "$1",
    );
    if (isDev){
    let token = localStorage.getItem("token")
    // let token = document.cookie.replace(/(?:(?:^|.*;\s*)token\s*=\s*([^;]*).*$)|^.*$/, "$1")
    if (isDev) {
      token = token || DEV_TOKEN
    }
    if (token) {
      config.headers.Authorization = "Bearer " + token;
      config.headers.Authorization = "Bearer " + token
    }
    return config
  },
@@ -61,16 +59,17 @@
/* //返回状态判断(添加响应拦截器) */
Axios.interceptors.response.use(
  (res) => {
    console.log("99999", res)
    /* //对响应数据做些事 */
    if (res.data.code === 200) {
      return res.data ? res.data : {}
    } else if([2012,2013,2014,2015].includes(res.data.code)){
      if (isDev){
    } else if ([2012, 2013, 2014, 2015].includes(res.data.code)) {
      if (isDev) {
        alert("JWT失效,即将跳转至登录页..")
        window.location = getApsPage()+'/login'
      }else {
        window.location = getApsPage() + "/login"
      } else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/login'
        window.location = getApsPage() + "/login"
      }
      // Message({
      //   message: res.data.msg,
@@ -78,28 +77,42 @@
      //   duration: 5 * 1000
      // })
      return Promise.reject(res.data)
    }else if([2036].includes(res.data.code)){
      if (isDev){
        window.location = getApsPage()+'/commonWeb?resetPwd=true'
      }else {
    } else if ([2036].includes(res.data.code)) {
      if (isDev) {
        window.location = getApsPage() + "/commonWeb?resetPwd=true"
      } else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/commonWeb?resetPwd=true'
        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'
      // }
    } else if ([3003].includes(res.data.code)) {
      // 此状态下不弹出error message
      console.log("3003")
      return Promise.resolve(res.data)
    } else {
      if (!res.config.loadingFlag) {
        Message({
          // message: res.data.msg==res.data.data?res.data.data:res.data.msg+','+res.data.data,
          message: res.data.msg,
          type: "error",
          duration: 5 * 1000
        })
      }
      return res.data ? res.data : {}
    }
  },
  (error) => {
    if(error.response.status === 401){
      if (isDev){
    if (error.response.status === 401) {
      if (isDev) {
        alert("JWT失效,即将跳转至登录页..")
        window.location = getApsPage()+'/login'
      }else {
        window.location = getApsPage() + "/login"
      } else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/login'
        window.location = getApsPage() + "/login"
      }
    }
    let { message } = error