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

View File

@@ -16,16 +16,12 @@ export const constantRoutes: RouteRecordRaw[] = [
} }
] ]
}, },
{ {
path: '/login', path: '/login',
component: () => import('@/views/login/index.vue'), component: () => import('@/views/login/index.vue'),
meta: { hidden: true } meta: { hidden: true }
}, },
{
path: '/404',
component: () => import('@/views/error-page/404.vue'),
meta: { hidden: true }
},
{ {
path: '/', path: '/',
@@ -44,7 +40,12 @@ export const constantRoutes: RouteRecordRaw[] = [
meta: { hidden: true } meta: { hidden: true }
}, },
{ {
path: 'apidoc', path: '/404',
component: () => import('@/views/error-page/404.vue'),
meta: { hidden: true }
},
{
path: '/apidoc',
component: () => import('@/views/demo/apidoc.vue'), component: () => import('@/views/demo/apidoc.vue'),
meta: { hidden: true } meta: { hidden: true }
} }
@@ -104,7 +105,9 @@ export const constantRoutes: RouteRecordRaw[] = [
}*/ }*/
]; ];
// 创建路由 /**
* 创建路由
*/
const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
routes: constantRoutes as RouteRecordRaw[], routes: constantRoutes as RouteRecordRaw[],
@@ -112,15 +115,12 @@ const router = createRouter({
scrollBehavior: () => ({ left: 0, top: 0 }) scrollBehavior: () => ({ left: 0, top: 0 })
}); });
// 重置路由 /**
* 重置路由
*/
export function resetRouter() { export function resetRouter() {
const permissionStore = usePermissionStoreHook(); router.replace({ path: '/login' });
permissionStore.routes.forEach(route => { location.reload();
const name = route.name;
if (name && router.hasRoute(name)) {
router.removeRoute(name);
}
});
} }
export default router; export default router;