import router from './index'; import { kuky_Authorization } from '../base/storage'; import { firstPage, accessFlatRoutes } from '../utils/permission'; /** 免验证白名单 */ const whiteList = ['/login', '/reg', '/noPer']; // 导入所需的模块 router.beforeEach((to, from, next) => { const isAuthorized = kuky_Authorization.get(); // 获取用户授权状态 const isLoginPage = to.path === '/login'; // 判断目标路径是否为登录页 const firstLogin = from.path === '/login'; // 判断当前路径是否为登录页 const isInWhiteList = whiteList.includes(to.path); // 检查目标路径是否在白名单中 const hasAccess = accessFlatRoutes.some((route) => route.path == to.path); // 用户已授权的情况 if (isAuthorized) { if (firstLogin || isLoginPage) { next(); // 如果是第一次登录,直接进入首页 } else if (!hasAccess && !isInWhiteList) { // 如果用户无访问权限且不在白名单中 next({ path: '/noPer' }); // 重定向到无权限页面 } else { next(); // 其他情况,正常访问 } } else { // 用户未授权的情况 if (isInWhiteList) { // 如果目标路径在免登录白名单中 next(); // 允许进入 } else { next('/login'); // 否则重定向到登录页面 } } });