| | |
| | | pendingRouteLocation = null |
| | | return queuedRoute |
| | | } |
| | | function isSameRouteLocation(firstRoute, secondRoute) { |
| | | if (!firstRoute || !secondRoute) { |
| | | return false |
| | | } |
| | | return ( |
| | | firstRoute.path === secondRoute.path && |
| | | JSON.stringify(firstRoute.query || {}) === JSON.stringify(secondRoute.query || {}) && |
| | | (firstRoute.hash || '') === (secondRoute.hash || '') |
| | | ) |
| | | } |
| | | function schedulePendingRouteNavigation(router, routeLocation) { |
| | | if (!routeLocation) { |
| | | return |
| | | } |
| | | setTimeout(() => { |
| | | const currentRoute = router.currentRoute?.value |
| | | if (isSameRouteLocation(currentRoute, routeLocation)) { |
| | | return |
| | | } |
| | | void router.push({ |
| | | path: routeLocation.path, |
| | | query: routeLocation.query, |
| | | hash: routeLocation.hash |
| | | }) |
| | | }, 0) |
| | | } |
| | | function setupBeforeEachGuard(router) { |
| | | routeRegistry = new RouteRegistry(router) |
| | | router.beforeEach(async (to, from, next) => { |
| | |
| | | menuStore.addRemoveRouteFns(routeRegistry?.getRemoveRouteFns() || []) |
| | | IframeRouteManager.getInstance().save() |
| | | useWorktabStore().validateWorktabs(router) |
| | | const targetLocation = consumePendingRoute() || createRouteLocation(to) |
| | | if (isStaticRoute(targetLocation.path)) { |
| | | const initialTargetLocation = createRouteLocation(to) |
| | | const queuedTargetLocation = consumePendingRoute() |
| | | if (isStaticRoute(initialTargetLocation.path)) { |
| | | routeInitInProgress = false |
| | | next(targetLocation) |
| | | next(initialTargetLocation) |
| | | if (queuedTargetLocation) { |
| | | schedulePendingRouteNavigation(router, queuedTargetLocation) |
| | | } |
| | | return |
| | | } |
| | | const { homePath } = useCommon() |
| | | const { path: validatedPath, hasPermission } = RoutePermissionValidator.validatePath( |
| | | targetLocation.path, |
| | | const initialValidation = RoutePermissionValidator.validatePath( |
| | | initialTargetLocation.path, |
| | | menuList, |
| | | homePath.value || '/' |
| | | ) |
| | | const queuedValidation = queuedTargetLocation |
| | | ? RoutePermissionValidator.validatePath( |
| | | queuedTargetLocation.path, |
| | | menuList, |
| | | homePath.value || '/' |
| | | ) |
| | | : null |
| | | routeInitInProgress = false |
| | | if (!hasPermission) { |
| | | if (!initialValidation.hasPermission) { |
| | | closeLoading() |
| | | console.warn(`[RouteGuard] 用户无权限访问路径: ${targetLocation.path},已跳转到首页`) |
| | | console.warn(`[RouteGuard] 用户无权限访问路径: ${initialTargetLocation.path},已跳转到首页`) |
| | | next({ |
| | | path: validatedPath, |
| | | path: initialValidation.path, |
| | | replace: true |
| | | }) |
| | | } else { |
| | | next({ |
| | | ...targetLocation, |
| | | path: validatedPath, |
| | | ...initialTargetLocation, |
| | | path: initialValidation.path, |
| | | replace: true |
| | | }) |
| | | } |
| | | if (queuedValidation) { |
| | | if (!queuedValidation.hasPermission) { |
| | | console.warn(`[RouteGuard] 用户无权限访问路径: ${queuedTargetLocation.path},已跳转到首页`) |
| | | } |
| | | schedulePendingRouteNavigation(router, { |
| | | ...queuedTargetLocation, |
| | | path: queuedValidation.path |
| | | }) |
| | | } |
| | | } catch (error) { |
| | | console.error('[RouteGuard] 动态路由注册失败:', error) |
| | | closeLoading() |