diff --git a/web/apps/web-ele/src/router/guard.ts b/web/apps/web-ele/src/router/guard.ts index ff44c71..1bc000a 100644 --- a/web/apps/web-ele/src/router/guard.ts +++ b/web/apps/web-ele/src/router/guard.ts @@ -111,6 +111,10 @@ function setupCommonGuard(router: Router) { if (preferences.transition.progress) { stopProgress(); } + + if (useAccessStore().accessToken) { + prefetchAiPlatformPages(to.path); + } }); } @@ -198,7 +202,6 @@ function setupAccessGuard(router: Router) { accessStore.setAccessMenus(accessibleMenus); accessStore.setAccessRoutes(accessibleRoutes); accessStore.setIsAccessChecked(true); - prefetchAiPlatformPages(); // 子应用根路径重定向(/app/hr -> /app/hr/xxx) if (appContextStore.isSubApp) { diff --git a/web/apps/web-ele/src/router/prefetch.ts b/web/apps/web-ele/src/router/prefetch.ts index 73e286a..487e63f 100644 --- a/web/apps/web-ele/src/router/prefetch.ts +++ b/web/apps/web-ele/src/router/prefetch.ts @@ -18,25 +18,51 @@ type NavigatorWithConnection = Navigator & { }; }; -let prefetched = false; +const prefetchedGroups = new Set(); -const corePrefetchTasks = [ - () => import('#/views/_core/user/index.vue'), - () => import('#/views/_core/menu/index.vue'), - () => import('#/views/_core/role/index.vue'), +type PrefetchTask = { + key: string; + load: () => Promise; +}; + +const corePrefetchTasks: PrefetchTask[] = [ + { key: 'system-user', load: () => import('#/views/_core/user/index.vue') }, + { key: 'system-menu', load: () => import('#/views/_core/menu/index.vue') }, + { key: 'system-role', load: () => import('#/views/_core/role/index.vue') }, ]; -const aiInteractivePrefetchTasks = [ - () => import('#/views/_core/agent-chat/index.vue'), - () => import('#/components/ai-chat-panel/AiChatPanel.vue'), +const aiInteractivePrefetchTasks: PrefetchTask[] = [ + { + key: 'agent-chat', + load: () => import('#/views/_core/agent-chat/index.vue'), + }, + { + key: 'ai-chat-panel', + load: () => import('#/components/ai-chat-panel/AiChatPanel.vue'), + }, ]; -const aiManagementPrefetchTasks = [ - () => import('#/views/ai-platform/agent/index.vue'), - () => import('#/views/ai-platform/workflow/index.vue'), - () => import('#/views/ai-platform/model/index.vue'), - () => import('#/views/ai-platform/knowledge/index.vue'), - () => import('#/views/ai-platform/workflow-runs/index.vue'), +const aiManagementPrefetchTasks: PrefetchTask[] = [ + { + key: 'ai-agent', + load: () => import('#/views/ai-platform/agent/index.vue'), + }, + { + key: 'ai-workflow', + load: () => import('#/views/ai-platform/workflow/index.vue'), + }, + { + key: 'ai-model', + load: () => import('#/views/ai-platform/model/index.vue'), + }, + { + key: 'ai-knowledge', + load: () => import('#/views/ai-platform/knowledge/index.vue'), + }, + { + key: 'ai-workflow-runs', + load: () => import('#/views/ai-platform/workflow-runs/index.vue'), + }, ]; function scheduleIdle(callback: () => void) { @@ -63,38 +89,65 @@ function canPrefetch() { return !/(^|-)2g$/.test(connection?.effectiveType || ''); } -async function runTasksSequentially(tasks: Array<() => Promise>) { +async function runTasksSequentially(tasks: PrefetchTask[]) { for (const task of tasks) { if (!canPrefetch()) { return; } - await task().catch(() => undefined); + if (prefetchedGroups.has(task.key)) { + continue; + } + prefetchedGroups.add(task.key); + await task.load().catch(() => undefined); } } -function prefetchAiPlatformPages() { - if (prefetched || typeof window === 'undefined') { +function delayedPrefetch(tasks: PrefetchTask[], delay: number) { + if (!tasks.length || typeof window === 'undefined') { return; } - prefetched = true; window.setTimeout(() => { scheduleIdle(() => { - void runTasksSequentially(corePrefetchTasks); + void runTasksSequentially(tasks); }); - }, 800); + }, delay); +} - window.setTimeout(() => { - scheduleIdle(() => { - void runTasksSequentially(aiInteractivePrefetchTasks); - }); - }, 4000); +function isSystemRoute(path: string) { + return path.startsWith('/system/') || path.startsWith('/message/'); +} - window.setTimeout(() => { - scheduleIdle(() => { - void runTasksSequentially(aiManagementPrefetchTasks); - }); - }, 9000); +function isAiPlatformRoute(path: string) { + return path.startsWith('/ai-platform/'); +} + +function isAgentChatRoute(path: string) { + return path.startsWith('/agent-chat/'); +} + +function prefetchAiPlatformPages(currentPath = '') { + if (typeof window === 'undefined') { + return; + } + + if (isSystemRoute(currentPath)) { + delayedPrefetch(corePrefetchTasks, 6000); + return; + } + + if (isAgentChatRoute(currentPath)) { + delayedPrefetch(aiInteractivePrefetchTasks, 1500); + return; + } + + if (isAiPlatformRoute(currentPath)) { + delayedPrefetch(aiManagementPrefetchTasks, 2000); + return; + } + + delayedPrefetch(corePrefetchTasks, 7000); + delayedPrefetch(aiInteractivePrefetchTasks, 12_000); } export { prefetchAiPlatformPages }; diff --git a/web/apps/web-ele/src/views/dashboard/workspace/index.vue b/web/apps/web-ele/src/views/dashboard/workspace/index.vue index 7d3ae05..43e3fed 100644 --- a/web/apps/web-ele/src/views/dashboard/workspace/index.vue +++ b/web/apps/web-ele/src/views/dashboard/workspace/index.vue @@ -1,23 +1,18 @@