axios请求超时后再调用接口处理方法
//设置全局的请求次数,请求的间隙
axios.defaults.retry = 2;
axios.defaults.retryDelay = 1000;
// http response 拦截器
axios.interceptors.response.use(response=>{
//console.log('请求响应后');
const res = response.data,
code = res.error_code,
msg = res.message;
if (code == 0||code == 1) { //成功
return res;
}else if(code==-4){
console.log(msg);
store.commit('updataLoginFlag',false);
}else{
console.log(msg);
}
}, (err)=> {// 返回状态码不为200时候的错误处理
const code=err.response.data.error_code;
const status=err.response.status;
switch (status){
//case 500:router.push({name:'500'});break;
case 403:router.push({name:'403'});break;
}
var config = err.config;
// 如果配置不存在,或者重试选项没有设置,则拒绝
if(!config || !config.retry) return Promise.reject(err);
// 设置用于跟踪重试计数的变量
config.__retryCount = config.__retryCount || 0;
if(config.__retryCount >= config.retry) {
return Promise.reject(err);
}
config.__retryCount += 1;
// 创造新的请求来处理回调
var backoff = new Promise(function(resolve) {
setTimeout(function() {
resolve();
}, config.retryDelay || 1);
});
// axios将重新请求
return backoff.then(function() {
config.baseURL='';
return axios(config);
});
});