axios拦截器,登录超时跳转到login页面
程序员文章站
2023-12-23 09:53:34
import Vue from 'vue';import axios from "axios";import auth from '../components/auth/auth.js'import {Message,Loading} from 'element-ui'import router from '../router/index'let config = { timeout: 20 * 1000, // Timeout withCredentials: true, // Che...
import Vue from 'vue';
import axios from "axios";
import auth from '../components/auth/auth.js'
import {Message,Loading} from 'element-ui'
import router from '../router/index'
let config = {
timeout: 20 * 1000, // Timeout
withCredentials: true, // Check cross-site Access-Control
};
const _axios = axios.create(config);
_axios.interceptors.request.use(
function(config) {
config.headers.common[''] = auth.getToken()== undefined? '' : auth.getToken().token;
return config;
},
function(error) {
// Do something with request error
return Promise.reject(error);//Promise
}
);
// Add a response interceptor
_axios.interceptors.response.use(
res=> {
Loading.service().close();
if (res.data.returnCode == 111111) {
return res;
} else{
Message.error({message: res.data.data});
}
return res;
}, err=> {
Loading.service().close();
if (err.response.status == 403) {
Message.error({message: '登录超时,请重新登录!'});
router.push('Login');
} else {
Message.error({message: '服务器被吃了⊙﹏⊙∥'});
}
return err;
}
);
本文地址:https://blog.csdn.net/weixin_44144211/article/details/110234094