| | |
| | | import axios from "axios" |
| | | import { Message } from "element-ui" |
| | | import Cookies from 'js-cookie' |
| | | |
| | | // 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", |
| | |
| | | prod:`//${window.location.hostname}:9080`, |
| | | test:`//192.168.20.119:9080`, |
| | | // 想跳到本地启动的登录页的话需要把dev改成你本地项目路径 |
| | | dev: `//192.168.20.119:9080` |
| | | dev: `//192.168.8.111:8081` |
| | | } |
| | | |
| | | if (location.href.includes('192.168.20.119')) { |
| | | return loginPathMap.test |
| | | } else if (location.href.includes('192.168') || location.href.includes('localhost')) { |
| | | return loginPathMap.dev |
| | | } else { |
| | | return loginPathMap.prod |
| | | } |
| | | } |
| | | |
| | | function getToken(){ |
| | | const localStorageToken = localStorage.getItem('token') |
| | | const cookieToken = Cookies.get("token") |
| | | if (localStorageToken){ |
| | | return localStorageToken |
| | | }else if ( cookieToken) { |
| | | localStorage.setItem('token',cookieToken) |
| | | return cookieToken |
| | | }else { |
| | | return '' |
| | | } |
| | | return loginPathMap[environmentType()] |
| | | } |
| | | |
| | | /* //POST传参序列化(添加请求拦截器) */ |
| | | Axios.interceptors.request.use( |
| | | (config) => { |
| | | // 若是有做鉴权token , 就给头部带上token |
| | | let token = getToken() |
| | | if (token) { |
| | | config.headers.Authorization = "Bearer " + token; |
| | | } |
| | | |
| | | return config |
| | | 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) |
| | |
| | | (res) => { |
| | | /* //对响应数据做些事 */ |
| | | if (res.data.code === 200) { |
| | | return res.data ? res.data : {} |
| | | if(res.config.IsHeader){ |
| | | return res; |
| | | }else{ |
| | | return res.data ? res.data : {} |
| | | } |
| | | } else if (res.data.code === 700001 || res.data.code === 700005) { |
| | | console.log("客户名称重复") |
| | | return res |
| | | } else if([2012,2013,2014,2015].includes(res.data.code)){ |
| | | if (isDev){ |
| | | alert("JWT失效") |
| | | // window.location = getApsPage()+'/' |
| | | }else { |
| | | // JWT鉴权失效 跳转到登录页 |
| | | window.location = getApsPage()+'/login' |
| | | return Promise.reject(res.data) |
| | | } |
| | | return Promise.reject(res.data) |
| | | } else { |
| | | Message({ |
| | | message: res.data.msg, |
| | |
| | | } |
| | | }, |
| | | (error) => { |
| | | if (error.response.status === 401){ |
| | | if (isDev){ |
| | | alert("JWT失效") |
| | | // window.location = getApsPage()+'/' |
| | | }else { |
| | | // JWT鉴权失效 跳转到登录页 |
| | | window.location = getApsPage()+'/login' |
| | | } |
| | | } |
| | | |
| | | let { message } = error |
| | | if (message === "Network Error") { |
| | | message = "后端接口连接异常" |