fix: 使用不同账号登录路由权限问题修复 Closes I6A2VR

Former-commit-id: 13bd62c8bb13523af8b47cd2052ff78fe92433a7
This commit is contained in:
haoxr
2023-02-08 00:55:16 +08:00
parent 9c347f7be8
commit 0965f32082
2 changed files with 26 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
import router from '@/router';
import { RouteRecordRaw } from 'vue-router';
import { getToken } from '@/utils/auth';
import { useUserStoreHook } from '@/store/modules/user';
import { usePermissionStoreHook } from '@/store/modules/permission';
@@ -15,25 +15,26 @@ const whiteList = ['/login'];
router.beforeEach(async (to, from, next) => {
NProgress.start();
const userStore = useUserStoreHook();
if (userStore.token) {
// 登录成功,跳转到首页
const hasToken = getToken();
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' });
NProgress.done();
} else {
const hasGetUserInfo = userStore.roles.length > 0;
if (hasGetUserInfo) {
const hasRoles = userStore.roles && userStore.roles.length > 0;
if (hasRoles) {
// 路由未匹配跳转404
if (to.matched.length === 0) {
from.name ? next({ name: from.name as any }) : next('/401');
from.name ? next({ name: from.name }) : next('/404');
} else {
next();
}
} else {
try {
const { roles } = await userStore.getInfo();
const accessRoutes: RouteRecordRaw[] =
await permissionStore.generateRoutes(roles);
accessRoutes.forEach((route: any) => {
const accessRoutes = await permissionStore.generateRoutes(roles);
accessRoutes.forEach(route => {
router.addRoute(route);
});
next({ ...to, replace: true });