// POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
var token = sessionStorage.getItem('token');
if(token){
// 判断是否存在token,如果存在的话,则每个http header都加上token
axios.default.headers.Authorization = 'token ${store.state.token}';
}
// 在发送请求之前序列化数据
if (config.method === 'post') {
config.data = qs.stringify(config.data);
}else if(config.method === 'get'){
config.params = {
redirect_url: window.location.href.split('#')[1],
...config.params
}
}
return config;
}, (error) => {
_.toast('错误的传参', 'fail');
return Promise.reject(error);
});
// 返回状态判断(添加响应拦截器)
axios.interceptors.response.use(response => {
if (response.data.code == 1107) { //相应状态码
if (response.data.data.type === 1) {
//判断返回类型是1跳转登录页面
router.push({path: '/px-login', query: {errorInfo: 'login'}});
} else if (response.data.data.type === 2) {
window.location.href = response.data.data.url;
}
} else {
//router.push({path: '/errorDev', query: {errMsg: response.data.errMsg}});
}
return response;
}, error => {
return Promise.reject(error);
});