hookehuyr

fix(axios): 添加401响应处理以跳转登录页

在axios拦截器中添加对401响应的处理,清除用户登录信息并跳转到登录页,同时携带当前路由信息以便登录后重定向
......@@ -2,12 +2,12 @@
* @Author: hookehuyr hookehuyr@gmail.com
* @Date: 2022-05-28 10:17:40
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-03-25 15:03:55
* @LastEditTime: 2025-05-08 17:45:44
* @FilePath: /mlaj/src/utils/axios.js
* @Description:
*/
import axios from 'axios';
// import router from '@/router';
import router from '@/router';
// import qs from 'Qs'
// import { strExist } from '@/utils/tools'
......@@ -43,9 +43,17 @@ axios.interceptors.request.use(
*/
axios.interceptors.response.use(
response => {
if (response.data && response.data.code === 401) {
// 清除用户登录信息
localStorage.removeItem('currentUser');
// 跳转到登录页面,并携带当前路由信息
const currentPath = router.currentRoute.value.fullPath;
router.push(`/login?redirect=${encodeURIComponent(currentPath)}`);
}
return response;
},
error => {
// 响应错误处理
return Promise.reject(error);
});
......