import axios from 'axios'
import {Message, Loading} from 'element-ui'
const getCookieItem = (key)=>{
var arr = document.cookie.split("; ");
for(var i = 0; i<arr.length; i++) {
var res = arr[i].split("=");
if (res[0] == key) {
return res[1];
}
}
};
let loadingInstance;
const HttpRequest = axios.create({
timeout: 60000,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'x-requested-with': 'XMLHttpRequest',
'version': 2
},
responseType: 'json',
})
HttpRequest.interceptors.request.use(config => {
let localToken = decodeURIComponent(getCookieItem('microServiceToken'));
if (localToken) {
config.headers['token'] = localToken
}
if(config.url.indexOf('/pass-formData') !== -1){
config.url = config.url.substring(0,config.url.length-14);
config.headers['Content-Type'] = 'multipart/form-data; charset=UTF-8';
}else {
config.headers['Content-Type'] = 'application/json; charset=UTF-8';
}
// loadingInstance = Loading.service({fullscreen: true, background: "rgba(0, 0, 0, 0.7)"});
let data = config.data;
if(data != undefined){
let keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
let value = data[keys[i]];
let tmpKey = keys[i];
if (typeof(value) === 'string') {
if(!value){
delete data[tmpKey];
}
}
}
config.data = data;
}
return config
}, error => {
Promise.reject(error)
})
HttpRequest.interceptors.response.use(
response => {
// loadingInstance.close();
return response.data;
},
error => {
// loadingInstance.close();
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
})
export default HttpRequest