将 menu 从 appstore 提出.
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
import type { Router } from 'vue-router';
|
||||
import NProgress from 'nprogress';
|
||||
import { useAppStore } from '@/store';
|
||||
import { useMenuStore } from '@/store';
|
||||
import { NOT_FOUND_ROUTER_NAME, WHITE_ROUTER_LIST } from '../constants';
|
||||
import usePermission from '@/hooks/permission';
|
||||
|
||||
export default function setupPermissionGuard(router: Router) {
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const appStore = useAppStore();
|
||||
const menuStore = useMenuStore();
|
||||
// 未加载菜单 并且 不在白名单内 则加载菜单
|
||||
if (
|
||||
!appStore.menuFetched &&
|
||||
!menuStore.menuFetched &&
|
||||
!WHITE_ROUTER_LIST.find((el) => el.name === to.name)
|
||||
) {
|
||||
// 加载菜单
|
||||
await appStore.fetchMenuConfig();
|
||||
await menuStore.fetchMenu();
|
||||
}
|
||||
// 检测是否可以访问
|
||||
const permission = usePermission();
|
||||
const access = permission.accessRouter(to);
|
||||
// 刚进入页面时 重定向的 meta 是空的
|
||||
if (access && to.meta.locale === undefined && appStore.menuFetched) {
|
||||
if (access && to.meta.locale === undefined && menuStore.menuFetched) {
|
||||
to.meta = to.matched[to.matched.length - 1].meta;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user