解决重复跳转login的bug
This commit is contained in:
+24
-1
@@ -1,5 +1,27 @@
|
||||
import axios from 'axios';
|
||||
import store from '../store';
|
||||
import router from '../router';
|
||||
|
||||
let redirectingToLogin = false;
|
||||
|
||||
function redirectToLogin() {
|
||||
if (redirectingToLogin || router.currentRoute.path === '/login') {
|
||||
return;
|
||||
}
|
||||
|
||||
redirectingToLogin = true;
|
||||
router.replace('/login').then(
|
||||
() => {
|
||||
redirectingToLogin = false;
|
||||
},
|
||||
(navigationError) => {
|
||||
redirectingToLogin = false;
|
||||
if (navigationError.name !== 'NavigationDuplicated') {
|
||||
console.error(navigationError);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 配置请求
|
||||
const request = axios.create({
|
||||
@@ -29,9 +51,10 @@ request.interceptors.response.use(
|
||||
},
|
||||
(error) => {
|
||||
if (error.response && error.response.status === 401) {
|
||||
// 如果返回401,说明token过期或无效,清除token
|
||||
// 如果返回401,说明token过期或无效,清除token并跳转到登录页
|
||||
store.commit('SET_TOKEN', '');
|
||||
store.commit('SET_USERNAME', '');
|
||||
redirectToLogin();
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user