main.js中添加axios请求前拦截器、接受拦截器
import axios from 'axios'
// 添加请求拦截器,在请求头中加token,Cookie
axios.interceptors.request.use(
config => {
if (localStorage.getItem('Authorization')) {
config.headers.Authorization = localStorage.getItem('Authorization');
}
if (localStorage.getItem('Cookies')) {
config.headers.Cookies = localStorage.getItem('Cookies');
}
return config;
}, error => {
return Promise.reject(error);
});
// 这里拦截401错误,并重新跳入登页重新获取token
axios.interceptors.response.use(
response => {
console.log("response.use" + response)
if (response.data.data && response.data.data.ecode == '401') {
localStorage.removeItem('Authorization');
localStorage.removeItem('Cookie');
this.$router.push('/login');
} else {
return response;
}
}, error => {
if (error.response) {
switch (error.response.status) {
case 401:
localStorage.removeItem('Authorization');
localStorage.removeItem('Cookie');
this.$router.push('/login');
}
}
return Promise.reject(error.response.data)
}
);