/** * 权限控制 */ import router, { findFirstValidRoute } from './router' import { INDEX_ROUTE, INDEX_ROUTE_NAME } from './router/routes' import useUserStore from './stores/modules/user' import { PageEnum } from '@/enums/pageEnum' import { MenuEnum } from "@/enums/appEnums"; import { getToken } from '@/utils/token' import NProgress from 'nprogress' import 'nprogress/nprogress.css' // NProgress配置 NProgress.configure({ showSpinner: false }) // 免登录白名单 const whiteList: string[] = [PageEnum.LOGIN, '/support', '/401', '/work/verifySubmit'] router.beforeEach(async (to, from, next) => { // 开始 Progress Bar NProgress.start() if (whiteList.includes(to.path)) { // 在免登录白名单,直接进入 next() return } if (!getToken()) { next(PageEnum.LOGIN) return } const userStore = useUserStore() if (!Object.keys(userStore.user).length) { await userStore.getUserInfo() const routes = userStore.routes if (routes.length === 0) { next('/401') return } const routeName = findFirstValidRoute(routes) INDEX_ROUTE.redirect = { name: routeName } router.addRoute(INDEX_ROUTE) routes.forEach((route: any) => { // 动态添加可访问路由表 if (!route.children) { if (route.meta.type === MenuEnum.CATALOGUE) { return } router.addRoute(INDEX_ROUTE_NAME, route) return } router.addRoute(route) }) next({ ...to, replace: true }) return } next() }) router.afterEach(() => { NProgress.done() })