yixie-saas-container
Version:
45 lines (38 loc) • 1.19 kB
JavaScript
/**
* http请求的封装
*/
import axios from 'axios'
import storage from 'store' //本地存储
import CONFIG from './saasconfig' //saas配置
const SUCCESS = "000000";
/**
* 初始化axios
*/
const http = axios.create({
timeout: 6000, // 请求超时时间
withCredentials: true, //跨域带上cookie
})
/**
* 异常处理 非2xx的http响应
*/
const errorHandler = (error) => {
if (error.response) {
// 未登录
if (error.response.status === 401) {
window.location.href = storage.get(CONFIG.REQUEST_PROTOCOL_KEY) + storage.get(CONFIG.SAAS_LOGIN_URL_STORAGE_KEY) + '?callback=' + storage.get(CONFIG.REQUEST_PROTOCOL_KEY) + storage.get(CONFIG.SAAS_INIT_URL_STORAGE_KEY) + '?callback=' + storage.get(CONFIG.REQUEST_PROTOCOL_KEY) + storage.get(CONFIG.SAAS_SYS_DOMAIN_KEY);
}
}
return Promise.reject(error)
}
/**
* 请求完成拦截器
*/
http.interceptors.response.use((response) => {
let resulet = response.data;
if(resulet.stateCode === SUCCESS){
return resulet.data;
} else{
console.log("error");
}
}, errorHandler)
export default http