/** * @Description: 路由权限 * @Author: zenghua.wang * @Date: 2022-01-26 22:04:31 * @LastEditors: zenghua.wang * @LastEditTime: 2024-05-22 13:36:31 */ import NProgress from 'nprogress'; import 'nprogress/nprogress.css'; import router from '@/router'; import { useUserStore } from '@/store/modules/user'; import { usePermissionStore } from '@/store/modules/permission'; NProgress.configure({ showSpinner: false }); const { VITE_APP_NAME } = import.meta.env; const whiteList = ['/login']; router.beforeEach(async (to, from, next) => { // 解决三级菜单页面缓存问题 // if (to.matched && to.matched.length > 1) { // for (let i = 0; i < to.matched.length; i++) { // const element = to.matched[i]; // if (element.components.default.name === 'ViewBox') { // to.matched.splice(i, 1); // } // } // } NProgress.start(); if (typeof to.meta.title === 'string') { document.title = VITE_APP_NAME + ' | ' + to.meta.title; } const userStore = useUserStore(); const hasToken = true; //userStore.hasToken(); if (hasToken) { if (to.path === '/login') { next({ path: '/' }); } else { try { const PermissionStore = usePermissionStore(); if (!PermissionStore.routes.length) { const accessRoutes = await PermissionStore.generateRoutes(userStore.roles); accessRoutes.forEach((item) => router.addRoute(item)); next({ ...to, replace: true }); } else { next(); } } catch (error) { next(`/login?redirect=${to.path}`); } } } else { if (whiteList.indexOf(to.path) !== -1) { next(); } else { next(`/login?redirect=${to.path}`); } } // const PermissionStore = usePermissionStore(); // console.log('===', PermissionStore.routes); // if (!PermissionStore.routes.length) { // const accessRoutes = await PermissionStore.getRoutes(); // accessRoutes.forEach((item) => router.addRoute(item)); // next({ ...to, replace: true }); // } else { // next(); // } }); router.afterEach((to) => { // qiankun子应用跳转回主应用时判断#app是否还有渲染的子应用,如若没有则重新渲染主应用 // setTimeout(() => { // if (to.path === '/') { // if (window.wocwin_qiankun) { // window.wocwin_qiankun = null; // } // } // }, 300); NProgress.done(); });