feat: 路由守卫调整/

This commit is contained in:
renxiaodong
2025-06-23 03:16:55 -04:00
parent f2ce7a8539
commit 9d7f06ad0b
8 changed files with 60 additions and 62 deletions

View File

@ -1,8 +1,8 @@
/*
* @Author: 田鑫
* @Date: 2023-03-05 18:14:17
* @LastEditors: 田鑫
* @LastEditTime: 2023-03-05 19:21:15
* @LastEditors: Please set LastEditors
* @LastEditTime: 2025-06-23 02:08:22
* @Description:
*/
import type { Router } from 'vue-router';

View File

@ -1,36 +1,27 @@
/*
* @Author: 田鑫
* @Date: 2023-03-05 14:46:43
* @LastEditors: 田鑫
* @LastEditTime: 2023-03-05 15:59:25
* @Description: 路由登录状态守卫
* @Author: RenXiaoDong
* @Date: 2025-06-22 22:59:16
*/
import type { Router, LocationQueryRaw } from 'vue-router';
import NProgress from 'nprogress'; // progress bar
import type { Router } from 'vue-router';
import NProgress from 'nprogress';
import { goUserLogin } from '@/utils/user';
import { isLogin, clearAllLocalStorage } from '@/utils/auth';
import { useUserStore } from '@/stores/modules/user';
export default function setupUserLoginInfoGuard(router: Router) {
router.beforeEach(async (to, from, next) => {
console.log('access login info router guard');
NProgress.start();
if (to.name === 'auth') {
next();
}
const userStore = useUserStore();
//* 判断用户是否登录,若登录则放过,进入下一步
//* 若无,则清空所有缓存并弹回登录鉴权页
if (isLogin()) {
if (userStore.role) {
next();
} else {
userStore.getUserInfo();
next();
}
} else {
clearAllLocalStorage();
next({ name: 'UserLogin' }); // 添加缺失的next调用
const requireLogin = to?.meta?.requireLogin || 0;
const isLogin = !!userStore.isLogin;
if (requireLogin === 1 && !isLogin) {
goUserLogin();
next();
return;
}
next();
});
}