import { createRouter, createWebHistory } from 'vue-router' import { defineComponent } from 'vue' import type { RouteLocationNormalized } from 'vue-router' import { useAuthStore } from '@/stores/auth' import { useDeploymentCapabilitiesStore } from '@/stores/deploymentCapabilities' import { autoSetup, userInfoFromApi } from '@/api/auth' import type { DeploymentCapabilityKey } from '@/config/deploymentCapabilities' import { MessagePlugin } from 'tdesign-vue-next' import i18n from '@/i18n' import { normalizeSettingsSection } from '@/config/settingsRoute' import { isToolboxSection, toolboxLocation } from '@/config/toolbox' /** Lite /桌面 WebView 硬刷新时可能只打开 `/`,用 session 记住上次页面以便恢复 */ const LITE_LAST_PATH_KEY = 'academy_lite_last_path' // views/platform/index.vue always mounts the settings modal and opens it when // the path is /platform/settings, so this route only has to own the URL. // Rendering Settings.vue here would mount a second, independent copy. const SettingsRouteOutlet = defineComponent({ name: 'SettingsRouteOutlet', render: () => null }) function isLiteEdition(authStore: ReturnType) { return authStore.isLiteMode || localStorage.getItem('academy_lite_mode') === 'true' } function isLiteSpaDefaultEntry(to: RouteLocationNormalized) { return ( to.path === '/' || to.path === '/platform' || to.path === '/platform/knowledge-bases' || to.path === '/platform/creatChat' || to.name === 'knowledgeBaseList' ) } function isSafeLiteRestoreTarget(path: string) { return path.startsWith('/platform/') && !path.startsWith('/platform/organizations') } function hasPendingOIDCCallback() { if (typeof window === 'undefined') return false const hash = window.location.hash || '' return hash.includes('oidc_result=') || hash.includes('oidc_error=') } const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: "/", // 登录用户默认打开 AI 对话页(首页口径,与登录成功跳转一致) redirect: "/platform/creatChat", }, { path: "/login", name: "login", component: () => import("../views/auth/Login.vue"), meta: { requiresAuth: false, requiresInit: false } }, { // 学员扫码签到:独立手机端 H5 页(2026-11-25 调整)。顶层路由,直接渲染在 // App.vue 的裸 RouterView —— 完全脱离平台壳布局(侧栏/桌面样式),扫码免 // 登录直达;页面内自行完成工号签到与自动登录。显式 requiresAuth:false + // requiresInit:false 放行路由守卫(meta 缺省 = undefined 时守卫仍按需 // 登录拦截,必须显式 false)。 path: "/m/training/checkin/:token", name: "mobileTrainingCheckin", component: () => import("../views/training/CheckinPage.vue"), meta: { requiresInit: false, requiresAuth: false } }, // Embed chat is a separate entry (embed.html + embed-main.ts), not this SPA. { path: "/register", name: "registerByInvite", // Share-link landing page reuses the Login form: the same Vue // component renders both modes and detects ?token=xxx on mount // to switch into invite-register flow. Avoids a parallel page // that would duplicate the OIDC / language-switch / styling // surface for one extra field. component: () => import("../views/auth/Login.vue"), meta: { requiresAuth: false, requiresInit: false } }, { path: "/onboarding/workspace", name: "workspaceOnboarding", component: () => import("../views/auth/WorkspaceOnboarding.vue"), meta: { requiresAuth: true, requiresInit: false, requiresTenant: false } }, { path: "/join", name: "joinOrganization", // 重定向到组织列表页,并将 code 参数转换为 invite_code redirect: (to) => { const code = to.query.code as string return { path: '/platform/organizations', query: code ? { invite_code: code } : {} } }, meta: { requiresInit: true, requiresAuth: true } }, { path: "/knowledgeBase", name: "home", component: () => import("../views/knowledge/KnowledgeBase.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "/platform", name: "Platform", redirect: "/platform/creatChat", component: () => import("../views/platform/index.vue"), meta: { requiresInit: true, requiresAuth: true }, children: [ { path: "overview", name: "overview", component: () => import("../views/overview/Overview.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "tenant", redirect: "/platform/settings" }, { path: "settings", name: "settings", component: SettingsRouteOutlet, meta: { requiresInit: true, requiresAuth: true } }, { path: "knowledge-bases", name: "knowledgeBaseList", component: () => import("../views/knowledge/KnowledgeBaseList.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "knowledge-bases/:kbId", name: "knowledgeBaseDetail", component: () => import("../views/knowledge/KnowledgeBase.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "knowledge-search", // 旧路径保留为重定向,打开全局命令面板(⌘K),带上可选的 q 参数 redirect: (to) => { const q = to.query.q return { path: '/platform/knowledge-bases', query: typeof q === 'string' ? { cmdk: q } : { cmdk: '' }, } }, }, { path: "artifacts", name: "artifactLibrary", component: () => import("../views/artifacts/ArtifactLibrary.vue"), meta: { requiresInit: true, requiresAuth: true, requiredCapability: 'settings.sandbox' } }, { path: "toolbox/:section?", name: "toolbox", component: () => import("../views/toolbox/Toolbox.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "automations/:automationId?", name: "automationList", component: () => import("../views/automation/AutomationList.vue"), meta: { requiresInit: true, requiresAuth: true } }, { // 企业培训:子页(工作台/培训计划/培训课程/培训签到/学习考试/配置中心) // 收纳为侧栏「企业培训」分组的子项,tab 取自路径参数,缺省重定向到工作台。 path: "training", name: "training", redirect: "/platform/training/dashboard" }, { // 职称角色页已并入「组织架构」页维护(员工/角色/企微账号一处维护), // 旧链接与书签兜底重定向。 path: "training/members", redirect: "/platform/org-center" }, { path: "training/:tab", name: "trainingTab", component: () => import("../views/training/Training.vue"), meta: { requiresInit: true, requiresAuth: true } }, { // 课程学习详情页:工作台课程卡片 / 学习任务「去学习」进入, // 课件在线预览(PDF/Word/PPT/图片/视频)+ 随堂考试。 path: "training/course/:courseId", name: "trainingCourseDetail", component: () => import("../views/training/CourseDetail.vue"), meta: { requiresInit: true, requiresAuth: true } }, { // 旧平台内签到路径兼容重定向(2026-11-25 签到页迁移为独立手机端 H5): // 已投屏/已分发的旧二维码短窗口内仍可扫;携 query 重定向,OAuth 回跳 // 参数(code/state)不丢。 path: "training/checkin/:token", redirect: (to) => ({ path: `/m/training/checkin/${to.params.token}`, query: to.query }) }, { // 组织架构:租户级共享组织架构(部门 + 员工档案), // 业务模块(企业培训等)按需引用,部门岗位只维护一份。 path: "org-center", name: "orgCenter", component: () => import("../views/org-center/OrgCenter.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "agents", name: "agentList", component: () => import("../views/agent/AgentList.vue"), meta: { requiresInit: true, requiresAuth: true, requiredCapability: 'agents' } }, { path: "integrations", redirect: (to) => { const tab = typeof to.query.tab === 'string' ? to.query.tab : undefined const incoming = typeof to.query.section === 'string' ? to.query.section : 'integrations' const rest = { ...to.query } delete rest.tab return { path: '/platform/settings', query: { ...rest, section: normalizeSettingsSection(incoming, tab), }, } }, meta: { requiresInit: true, requiresAuth: true } }, { path: "creatChat", name: "globalCreatChat", component: () => import("../views/creatChat/creatChat.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "knowledge-bases/:kbId/creatChat", name: "kbCreatChat", component: () => import("../views/creatChat/creatChat.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "chat/:chatid", name: "chat", component: () => import("../views/chat/index.vue"), meta: { requiresInit: true, requiresAuth: true } }, { path: "organizations", name: "organizationList", component: () => import("../views/organization/OrganizationList.vue"), meta: { requiresInit: true, requiresAuth: true, requiredCapability: 'organizations' } }, // Compatibility redirects for /platform/system/* URLs. System // administration surfaces live as dedicated sections inside the // standard Settings modal; keep stable URLs for bookmarks and // external links. { path: "system", redirect: { path: "/platform/settings", query: { section: "system-global" } }, meta: { requiresInit: true, requiresAuth: true, requiresSystemAdmin: true }, }, { path: "system/settings", name: "systemSettings", redirect: { path: "/platform/settings", query: { section: "system-global" } }, meta: { requiresInit: true, requiresAuth: true, requiresSystemAdmin: true }, }, { path: "system/admins", name: "systemAdmins", redirect: { path: "/platform/settings", query: { section: "system-global" } }, meta: { requiresInit: true, requiresAuth: true, requiresSystemAdmin: true }, }, { path: "system/queues", name: "systemQueues", redirect: { path: "/platform/settings", query: { section: "runtime-queues" } }, meta: { requiresInit: true, requiresAuth: true, requiresSystemAdmin: true }, }, ], }, // Dev-only markdown rendering test page ...(import.meta.env.DEV ? [{ path: '/platform/dev/markdown', name: 'markdownTest', component: () => import('../views/dev/MarkdownTestPage.vue'), meta: { requiresAuth: false, requiresInit: false } }] : []), ], }); // 持久化 auto-setup / login 返回的认证信息到 store function persistLoginResponse(authStore: ReturnType, response: any) { const activeTenant = response.active_tenant || response.tenant if (response.user && response.token) { const homeTenantId = response.user.tenant_id ?? activeTenant?.id ?? '' authStore.setUser(userInfoFromApi(response.user, homeTenantId)) authStore.setToken(response.token) if (response.refresh_token) { authStore.setRefreshToken(response.refresh_token) } if (activeTenant) { authStore.setTenant({ id: String(activeTenant.id) || '', name: activeTenant.name || '', owner_id: response.user.id || '', created_at: activeTenant.created_at || new Date().toISOString(), updated_at: activeTenant.updated_at || new Date().toISOString() }) } else { authStore.setTenant(null) } if (Array.isArray(response.memberships)) { authStore.setMemberships(response.memberships) } } } async function hydrateSessionFromToken(authStore: ReturnType) { const token = localStorage.getItem('academy_token') if (!token) return false if (!authStore.token) { authStore.setToken(token) } const storedRefreshToken = localStorage.getItem('academy_refresh_token') if (storedRefreshToken && !authStore.refreshToken) { authStore.setRefreshToken(storedRefreshToken) } // /auth/me 的落库逻辑只在 auth store 里维护一份(user / tenant / memberships / // capabilities),这里只负责先把 token 放进 store。请求本身与启动、侧栏共用去重。 return authStore.refreshFromAuthMe() } let autoSetupAttempted = false let liteDeepLinkRestoreDone = false // 路由守卫:检查认证状态和系统初始化状态 router.beforeEach(async (to, from, next) => { const authStore = useAuthStore() // OIDC 回跳登录结果依赖 App.vue 在挂载后消费 URL hash。 // 如果这里先按“未登录”拦截到 /login,会导致回调结果没有机会落盘。 if (hasPendingOIDCCallback()) { next() return } // Preserve bookmarks for tools that have moved out of Settings. if (to.path === '/platform/settings' && isToolboxSection(to.query.section)) { next({ ...toolboxLocation(to.query.section, typeof to.query.sandboxId === 'string' ? to.query.sandboxId : undefined), replace: true }) return } // Lite:硬刷新后若落在默认首页,恢复本次会话中最后访问的 /platform 子路径 if (!liteDeepLinkRestoreDone) { liteDeepLinkRestoreDone = true if (isLiteEdition(authStore)) { const saved = sessionStorage.getItem(LITE_LAST_PATH_KEY) if (saved && isSafeLiteRestoreTarget(saved) && isLiteSpaDefaultEntry(to)) { if (saved !== to.fullPath) { next(saved) return } } } } // Tenantless onboarding still requires a valid user token even though it // deliberately skips the normal tenant/system-initialization gates. if (to.path === '/onboarding/workspace') { if (!authStore.isLoggedIn) { const restored = await hydrateSessionFromToken(authStore) if (!restored) { next('/login') return } } if (authStore.hasValidTenant) { next('/platform/creatChat') } else { next() } return } // 如果访问的是登录页面或初始化页面,直接放行 if (to.meta.requiresAuth === false || to.meta.requiresInit === false) { // 如果已登录用户访问登录页面,重定向到首页(AI 对话) if (to.path === '/login' && authStore.isLoggedIn) { next(authStore.hasValidTenant ? '/platform/creatChat' : '/onboarding/workspace') return } next() return } // 检查用户认证状态 if (to.meta.requiresAuth !== false) { if (!authStore.isLoggedIn) { const restored = await hydrateSessionFromToken(authStore) if (restored) { next( !authStore.hasValidTenant && to.meta.requiresTenant !== false ? '/onboarding/workspace' : to.fullPath, ) return } if (!autoSetupAttempted) { autoSetupAttempted = true localStorage.removeItem('academy_auto_setup_failed') try { const response = await autoSetup() if (response.success) { persistLoginResponse(authStore, response) authStore.setLiteMode(true) next(to.fullPath) return } } catch { // Auto-setup may be unavailable outside the native Lite shell. } } next('/login') return } } if (to.meta.requiresTenant !== false && !authStore.hasValidTenant) { next('/onboarding/workspace') return } // 部署能力只描述“后端是否提供该功能”,不反映服务健康或是否已配置。 // 探测失败时 Store 会 fail-open,真正的权限和可用性仍由后端接口校验。 const deploymentCapabilities = useDeploymentCapabilitiesStore() await deploymentCapabilities.ensureLoaded() const requiredCapability = to.meta.requiredCapability as DeploymentCapabilityKey | undefined if (requiredCapability && !deploymentCapabilities.isSupported(requiredCapability)) { MessagePlugin.warning(i18n.global.t('settings.capabilityUnavailable')) next('/platform/creatChat') return } // SystemAdmin gate — checked AFTER auth so a non-admin who's logged // out gets redirected to /login first (consistent with how the rest // of the auth flow works), and only an authenticated non-admin sees // the bounce. This is UI-only; the server enforces the real check. if (to.meta.requiresSystemAdmin === true) { if (!authStore.isSystemAdmin) { next('/platform/creatChat') return } } next() }) router.afterEach((to) => { if (!isLiteEdition(useAuthStore())) return if (to.path === '/login') return if (!to.path.startsWith('/platform')) return sessionStorage.setItem(LITE_LAST_PATH_KEY, to.fullPath) }) export default router