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 HttpRequestData = axios.create({
timeout: 60000,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'x-requested-with': 'XMLHttpRequest',
'version': 2
},
responseType: 'json',
})
HttpRequestData.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)"});
return config
}, error => {
Promise.reject(error)
})
HttpRequestData.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 HttpRequestData