|
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
import qs from "qs";
|
|
|
|
|
|
import router from "../router/router";
|
|
|
|
|
|
import {ElMessage} from "element-plus";
|
|
|
|
|
|
import {getUrlKey} from "@/api/api.js";
|
|
|
|
|
|
|
|
|
|
|
|
axios.defaults.staticBaseURL = '../' //静态文件加载路径
|
|
|
|
|
|
// axios.defaults.baseURL = 'http://' + window.location.hostname + ':9998/NMGJTManager' //测试
|
|
|
|
|
|
axios.defaults.baseURL = 'http://'+window.location.host+'/NMGJTManager' //正式
|
|
|
|
|
|
|
|
|
|
|
|
//post请求头
|
|
|
|
|
|
axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
|
|
|
|
|
|
|
|
|
|
// 表示跨域请求时是否需要使用凭证 允许跨域携带cookie信息
|
|
|
|
|
|
axios.defaults.withCredentials = true;
|
|
|
|
|
|
axios.defaults.changeOrigin = true;
|
|
|
|
|
|
|
|
|
|
|
|
// 允许跨域
|
|
|
|
|
|
axios.defaults.headers.post["Access-Control-Allow-Origin-Type"] = "*";
|
|
|
|
|
|
//设置超时
|
|
|
|
|
|
axios.defaults.timeout = 150000;
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
|
|
|
|
|
|
|
|
axios.interceptors.request.use(
|
|
|
|
|
|
config => {
|
|
|
|
|
|
// token && (config.headers.Authorization = token)
|
|
|
|
|
|
let token = localStorage.getItem('token');
|
|
|
|
|
|
token && (config.headers.token = token)
|
|
|
|
|
|
return config
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
return Promise.error(error)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
axios.interceptors.response.use(
|
|
|
|
|
|
response => {
|
|
|
|
|
|
if (response.status == 200) {
|
|
|
|
|
|
if (response.headers.token != null && response.headers.token !== '') {
|
|
|
|
|
|
localStorage.setItem('token', response.headers.token)
|
|
|
|
|
|
}
|
|
|
|
|
|
return Promise.resolve(response);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return Promise.reject(response);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
// status =-2 为后台判断ssession失效是token无法获取时的错误码
|
|
|
|
|
|
if (error.response.data.status == -2) {
|
|
|
|
|
|
// ElMessage.error('登陆超时请重新登录!')
|
|
|
|
|
|
window.parent.toLogin()
|
|
|
|
|
|
} else if (error.response.data.status == -1) {
|
|
|
|
|
|
ElMessage.error('请登陆!')
|
|
|
|
|
|
window.parent.toLogin()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
alert(JSON.stringify(error), '请求异常', {
|
|
|
|
|
|
confirmButtonText: '确定',
|
|
|
|
|
|
callback: (action) => {
|
|
|
|
|
|
console.log(action)
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
export default {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param {String} url
|
|
|
|
|
|
* @param {Object} data
|
|
|
|
|
|
* @returns Promise
|
|
|
|
|
|
*/
|
|
|
|
|
|
post(url, data) {
|
|
|
|
|
|
return new Promise((resolve, reject, responseType) => {
|
|
|
|
|
|
axios({
|
|
|
|
|
|
method: 'post',
|
|
|
|
|
|
url,
|
|
|
|
|
|
data: data,
|
|
|
|
|
|
crossDomain: true,
|
|
|
|
|
|
// headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
|
|
|
|
|
|
//默认json格式,如果是下载文件,需要传 responseType:'blob'
|
|
|
|
|
|
responseType: (responseType == null || responseType == '') ? 'json' : responseType
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(res => {
|
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(err => {
|
|
|
|
|
|
reject(err)
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
get(url, data, inResponseType) {
|
|
|
|
|
|
return new Promise((resolve, reject, responseType) => {
|
|
|
|
|
|
axios({
|
|
|
|
|
|
method: 'get',
|
|
|
|
|
|
url,
|
|
|
|
|
|
params: data,
|
|
|
|
|
|
crossDomain: true,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8',
|
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
//默认json格式,如果是下载文件,需要传 responseType:'blob'
|
|
|
|
|
|
responseType: (inResponseType == null || inResponseType == '') ? 'json' : inResponseType
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(res => {
|
|
|
|
|
|
resolve(res)
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(err => {
|
|
|
|
|
|
reject(err)
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|