import { ActivatedRouteSnapshot, Route, Routes } from '@angular/router'; import { authGuard } from './core/auth.guard'; import { containerGuard } from './core/container.guard'; import { detectUiMode } from './core/device.service'; import { orgGuard } from './core/org.guard'; /** * 顶层路由表(ADR-004:双端双套路由树) * * 结构: * '' 入口分流页(UA/视口一次性判定 → replaceUrl 跳端) * login 登录(两端共用同一组件,形态自适应) * desktop/** 桌面端(懒加载,DesktopShell 包裹) * mobile/** 移动端(懒加载,MobileShell 包裹) * 旧路径 按当前端前缀重定向(FR-16;判定逻辑与 DeviceService.normalize 同源) * ** 兜底 → 入口页 * * ⚠️ `redirectTo` 使用 Angular 的 `RedirectFunction`(形参是 route snapshot)。 * 刻意不用「仅挂 canActivate 的 redirectTo 路由」写法 —— 存量为此踩过 NG04014。 */ type RedirectData = Pick; type RedirectFn = (data: RedirectData) => string; const LEGACY_SIMPLE = [ 'messages', 'tabs', 'workbench', 'deliverables', 'mine', 'contacts', 'projects', 'events', ] as const; /** snapshot.url 是「相对当前路由的剩余段」;顶层路由下即为完整路径 */ const legacyRedirect: RedirectFn = (d) => normalize(`/${segmentsOf(d).join('/')}`); export const routes: Routes = [ { path: '', pathMatch: 'full', loadComponent: () => import('./pages/entry/entry').then((m) => m.EntryComponent), }, { // 登录页分端(2026-11-03):移动端已按 Demo 重建独立形态; // 桌面端仍用原共用组件。两端登录逻辑共用 FmodeApi / SessionService,仅版式不同。 path: 'login', loadComponent: () => detectMode() === 'mobile' ? import('./mobile/pages/login/login').then((m) => m.MbLoginPage) : import('./pages/login/login').then((m) => m.LoginComponent), }, { /* * 等待组织邀请(2026-11-10)。 * * 🔴 只挂 `authGuard`,**不挂 `orgGuard`** —— 本页正是无组织用户的落点, * 再挂组织守卫会自我重定向死循环。 */ path: 'pending', canActivate: [authGuard], loadComponent: () => detectMode() === 'mobile' ? import('./mobile/pages/pending/pending').then((m) => m.MbPendingPage) : import('./pages/pending/pending').then((m) => m.PendingPage), }, { /* * 套餐页 / 开通等待页(2026-11-27 提到顶层) * * 🔴 为什么必须在 shell 之外: * * ① **门禁例外**:无容器的用户只能待在这两个页面, * 挂在 shell 里会被 `containerGuard` 一并拦掉 → 死循环。 * ② **全屏锁定**:开通等待页不允许出现 TabBar / 抽屉 / 侧边栏, * 不挂 shell 就等于没有这些东西(比一个个去禁用干净)。 * ③ **缩放不跳转**:shell 的 resize 监听不会触发, * 用户拖窗口不会把正在开通的页面跳走。 * * 只挂 `authGuard`(要登录),**不挂 `orgGuard` / `containerGuard`**。 */ path: 'plans', canActivate: [authGuard], loadComponent: () => detectMode() === 'mobile' ? import('./mobile/pages/plans/plans').then((m) => m.MbPlansPage) : import('./desktop/pages/plans/plans').then((m) => m.DkPlansPage), }, { /** 开通等待页:全屏锁定,成功/失败后才解锁 */ path: 'provision', canActivate: [authGuard], loadComponent: () => detectMode() === 'mobile' ? import('./mobile/pages/provision/provision').then((m) => m.MbProvisionPage) : import('./desktop/pages/provision/provision').then((m) => m.DkProvisionPage), }, { path: 'desktop', canActivate: [authGuard, orgGuard, containerGuard], loadChildren: () => import('./desktop/desktop.routes').then((m) => m.desktopRoutes), }, { path: 'mobile', canActivate: [authGuard, orgGuard, containerGuard], loadChildren: () => import('./mobile/mobile.routes').then((m) => m.mobileRoutes), }, // ---- 旧路径重定向(FR-16 AC-16.2)---- ...LEGACY_SIMPLE.map((p): Route => ({ path: p, redirectTo: legacyRedirect })), { path: 'chat/:id', redirectTo: legacyRedirect }, { path: 'chat', redirectTo: (): string => normalize('/chat') }, { path: 'agent/:id', redirectTo: legacyRedirect }, { // FR-11:语音通话暂不实现 → 落数字生命详情页,由详情页提示「暂未开放」 path: 'call/:id', /* * 通话不做(FR-11)→ 落**容器设置页**(2026-11-04 恢复)。 * 用户要从「某容器的来电」跳到该容器的设置,而不是笼统的会话列表。 */ redirectTo: (d): string => normalize(`/agent/${segmentsOf(d).pop() ?? ''}`), }, { path: '**', redirectTo: (): string => normalize('') }, ]; function segmentsOf(d: RedirectData): string[] { return d.url.map((s) => s.path); } /** * 旧路径 → 当前端路径。 * 独立成函数(而非注入 DeviceService)是为了让 `redirectTo` 保持**函数式**—— * 存量为此踩过 NG04014 的坑,此处刻意规避。 */ function normalize(url: string): string { // 纯自动识别(2026-11-10 去掉手动偏好) const mode = detectMode(); const clean = url.split('?')[0]; const [head, id] = clean.split('/').filter(Boolean); switch (head) { case 'messages': case 'tabs': return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat'; case 'chat': return id ? `/${mode}/chat/${id}` : `/${mode}/chat`; case 'agent': return `/${mode}/agent/${id ?? ''}`.replace(/\/$/, ''); case 'call': // 通话暂不实现 → 落详情页 return id ? `/${mode}/agent/${id}` : `/${mode}/chat`; // 首页已下线(2026-11-02):旧 /home 落到会话入口 case 'home': return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat'; case 'workbench': case 'deliverables': case 'mine': case 'contacts': case 'projects': case 'events': return `/${mode}/${head}`; default: return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat'; } } /** * 登录页 / 等待页该加载哪个组件。 * * 🔴 2026-11-10:改为复用 `DeviceService` 的纯函数 —— 这里原本是一份 * 逐字复制的 UA 判定,两处一旦不同步就会出现「登录页判移动、入口判桌面」。 * * 合并口径(2026-11-11):`detectMode` 的**判定内容**取本项目侧的 * 「视口宽度优先 + 去手动偏好」,**写法**取 master 侧的单函数复用。 */ const detectMode = detectUiMode;