charles
2024-08-06 5ecb7958c96d3f0b6d47b79aff7eb306c2cf690f
src/common/untils/request.js
@@ -1,12 +1,10 @@
import axios from "axios"
import { Message } from "element-ui"
// import router from '@/router'
import {getApsPage} from './index.js';
// 开发环境下将自己的token复制到这里, 也可以在浏览器中手动添加token到cookie中,cookie中的token优先
const DEV_TOKEN =  ''
const DEV_TOKEN =  '';
function environmentType(){
  let type
  let type;
  if (location.href.includes('192.168.20.119')) {
    type = 'test'
  } else if (location.href.includes('192.168') || location.href.includes('localhost')) {
@@ -14,28 +12,14 @@
  } else {
    type = 'prod'
  }
  return type
}
const isDev = environmentType() === 'dev'
const getApsPage = () => {
  // 首页部署在各个环境的端口
  const loginPathMap = {
    prod:`//${window.location.hostname}:9080`,
    test:`//192.168.20.119:9080`,
    // 想跳到本地启动的登录页的话需要把dev改成你本地项目路径
    dev: `//192.168.8.112:8080`
  }
  return loginPathMap[environmentType()]
}
const isDev = environmentType() === 'dev';
const Axios = axios.create({
  responseType: "json",
  withCredentials: true // 是否允许带cookie这些
})
});
/* //POST传参序列化(添加请求拦截器) */
Axios.interceptors.request.use(
@@ -44,6 +28,9 @@
        /(?:(?:^|.*;\s*)token\s*=\s*([^;]*).*$)|^.*$/,
        "$1",
    );
    if (config.methodType == "down") {
      config.responseType = "blob";
    }
    if (isDev){
      token = token || DEV_TOKEN
    }
@@ -61,25 +48,28 @@
/* //返回状态判断(添加响应拦截器) */
Axios.interceptors.response.use(
  (res) => {
    /* //对响应数据做些事 */
    if (res.data.code === 200) {
    if (res.config.responseType === 'blob') {
      return res.data
    }else 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'
        //alert("JWT失效,即将跳转至登录页..")
        window.location = 'http://'+getApsPage()+'/login'
      }else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/login'
        window.location ='http://'+ getApsPage()+'/login'
      }
      return Promise.reject(res.data)
    }else if([2036].includes(res.data.code)){
      if (isDev){
        window.location = getApsPage()+'/commonWeb?resetPwd=true'
        window.location ='http://'+ getApsPage()+'/commonWeb?resetPwd=true'
      }else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/commonWeb?resetPwd=true'
        window.location ='http://'+ getApsPage()+'/commonWeb?resetPwd=true'
      }
      return Promise.reject(res.data)
      // if(window.location.pathname && window.location.pathname !== '/login'){
      //   window.location = window.location.origin+'/login'
@@ -97,14 +87,13 @@
    if (error.response.status === 401){
      if (isDev){
        alert("JWT失效,即将跳转至登录页..")
        window.location = getApsPage()+'/login'
        window.location ='http://'+ getApsPage()+'/login'
        
      }else {
        //   JWT鉴权失效 跳转到登录页
        window.location = getApsPage()+'/login'
        window.location ='http://'+ getApsPage()+'/login'
      }
    }
    let { message } = error
    if (message === "Network Error") {
      message = "后端接口连接异常"
@@ -117,9 +106,8 @@
      message: message,
      type: "error",
      duration: 5 * 1000
    })
    });
    return Promise.reject(error)
  }
)
);
export default Axios