|
|
|
|
import axios from 'axios';
|
|
|
|
|
import { baseUrl } from "../config/env";
|
|
|
|
|
import { $globalStore } from "../common/globalStore"
|
|
|
|
|
import { $globalRouter } from "../common/globalRouter"
|
|
|
|
|
// import {cache} from '@/store/store';
|
|
|
|
|
// axios.defaults.withCredentials = true
|
|
|
|
|
//const source = axios.CancelToken.source()
|
|
|
|
|
const http = axios.create({
|
|
|
|
|
baseURL: baseUrl,
|
|
|
|
|
traditional: true,
|
|
|
|
|
contentType : "application/json",
|
|
|
|
|
timeout: 1000 * 500000, // 1 秒
|
|
|
|
|
});
|
|
|
|
|
const router = $globalRouter;
|
|
|
|
|
//请求拦截器添加head
|
|
|
|
|
http.interceptors.request.use(
|
|
|
|
|
config => {
|
|
|
|
|
// FormData 必须由浏览器/axios 自动设置 multipart boundary
|
|
|
|
|
if (config.data instanceof FormData) {
|
|
|
|
|
delete config.headers['Content-Type'];
|
|
|
|
|
} else if(!config.headers['Content-Type']){
|
|
|
|
|
config.headers['Content-Type'] = 'application/json'
|
|
|
|
|
}
|
|
|
|
|
const token = $globalStore.useCommon.token;
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers['Authorization'] = 'Bearer ' + token;
|
|
|
|
|
}
|
|
|
|
|
config.headers['clientid'] = 'f0535c2a32ab4d0496ab9a699b2a7d36';
|
|
|
|
|
return config
|
|
|
|
|
},
|
|
|
|
|
err => {
|
|
|
|
|
return Promise.reject(err)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
//返回拦截器
|
|
|
|
|
http.interceptors.response.use(
|
|
|
|
|
response => {
|
|
|
|
|
if (response.data.code === 500) {
|
|
|
|
|
// let show = cache.getMemoryCache("isShow");
|
|
|
|
|
// if(show){
|
|
|
|
|
// return response;
|
|
|
|
|
// }
|
|
|
|
|
if(response.data.message){
|
|
|
|
|
// Message.error(response.data.message);
|
|
|
|
|
}
|
|
|
|
|
if(response.data.msg){
|
|
|
|
|
// Message.error(response.data.msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (response.data.code === 401) {
|
|
|
|
|
// Message.error('没有操作该功能权限,请联系管理员添加权限');
|
|
|
|
|
}
|
|
|
|
|
if (response.data.msg === '未登录或登录超时。请重新登录') {
|
|
|
|
|
// let show = cache.getMemoryCache("isShow");
|
|
|
|
|
// if(show){
|
|
|
|
|
// return response;
|
|
|
|
|
// }
|
|
|
|
|
// cache.addMemoryCache("isShow",true);
|
|
|
|
|
// Message.error('未登录或登录超时。请重新登录');
|
|
|
|
|
// let target = window.location.hash;
|
|
|
|
|
// cache.addMemoryCache("target",target);
|
|
|
|
|
router.push({path:"/login"});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return response
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
if (error.response.status === 401) {
|
|
|
|
|
// Message.error('没有操作该功能权限,请联系管理员添加权限');
|
|
|
|
|
}
|
|
|
|
|
if (error.response.status === 403) {
|
|
|
|
|
// let show = cache.getMemoryCache("isShow");
|
|
|
|
|
// if(show){
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// cache.addMemoryCache("isShow",true);
|
|
|
|
|
if('#/login'!=window.location.hash){
|
|
|
|
|
// Message.error('登录过期,请先登录');
|
|
|
|
|
let target = window.location.hash;
|
|
|
|
|
// cache.addMemoryCache("target",target);
|
|
|
|
|
router.push({path:"/login",params:{target:encodeURIComponent(target)}});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return error
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export default http
|