|
@@ -1,23 +1,39 @@
|
|
|
import router from './index';
|
|
import router from './index';
|
|
|
import { kuky_Authorization } from '../base/storage';
|
|
import { kuky_Authorization } from '../base/storage';
|
|
|
-import { accessFlatRoutes } from '../utils/permission';
|
|
|
|
|
|
|
+import { initializeRoutes } from '../utils/permission';
|
|
|
|
|
|
|
|
/** 免验证白名单 */
|
|
/** 免验证白名单 */
|
|
|
const whiteList = ['/login', '/reg', '/noPer'];
|
|
const whiteList = ['/login', '/reg', '/noPer'];
|
|
|
|
|
|
|
|
-// 导入所需的模块
|
|
|
|
|
-router.beforeEach((to, from, next) => {
|
|
|
|
|
|
|
+/** 是否已经生成过路由表 */
|
|
|
|
|
+let hasRouteFlag = false;
|
|
|
|
|
+export const resetHasRouteFlag = () => {
|
|
|
|
|
+ hasRouteFlag = false;
|
|
|
|
|
+};
|
|
|
|
|
+// 扁平路由表
|
|
|
|
|
+let flatRoutes = [];
|
|
|
|
|
+
|
|
|
|
|
+router.beforeEach(async (to, from, next) => {
|
|
|
const isAuthorized = kuky_Authorization.get(); // 获取用户授权状态
|
|
const isAuthorized = kuky_Authorization.get(); // 获取用户授权状态
|
|
|
const isLoginPage = to.path === '/login'; // 判断目标路径是否为登录页
|
|
const isLoginPage = to.path === '/login'; // 判断目标路径是否为登录页
|
|
|
const isInWhiteList = whiteList.includes(to.path); // 检查目标路径是否在白名单中
|
|
const isInWhiteList = whiteList.includes(to.path); // 检查目标路径是否在白名单中
|
|
|
- const hasAccess = accessFlatRoutes.some((route) => route.path == to.path);
|
|
|
|
|
|
|
+
|
|
|
// 用户已授权的情况
|
|
// 用户已授权的情况
|
|
|
if (isAuthorized) {
|
|
if (isAuthorized) {
|
|
|
- if (isLoginPage) {
|
|
|
|
|
|
|
+ if (!hasRouteFlag) {
|
|
|
|
|
+ const { formattedRoutes, accessFlatRoutes } = await initializeRoutes();
|
|
|
|
|
+ formattedRoutes.forEach((route) => {
|
|
|
|
|
+ if (route.path && route.component) {
|
|
|
|
|
+ router.addRoute(route); // 动态添加可访问路由表
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ flatRoutes = accessFlatRoutes;
|
|
|
|
|
+ hasRouteFlag = true;
|
|
|
|
|
+ next({ ...to, replace: true });
|
|
|
|
|
+ } else if (isLoginPage) {
|
|
|
next('/');
|
|
next('/');
|
|
|
- } else if (!hasAccess && !isInWhiteList) {
|
|
|
|
|
- // 如果用户无访问权限且不在白名单中
|
|
|
|
|
- next('/noPer'); // 重定向到无权限页面
|
|
|
|
|
|
|
+ } else if (!flatRoutes.some((route) => route.path == to.path) && !isInWhiteList) {
|
|
|
|
|
+ next('/noPer'); // 如果用户无访问权限且不在白名单中,重定向到无权限页面
|
|
|
} else {
|
|
} else {
|
|
|
next(); // 其他情况,正常访问
|
|
next(); // 其他情况,正常访问
|
|
|
}
|
|
}
|