app.routes.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { ActivatedRouteSnapshot, Route, Routes } from '@angular/router';
  2. import { authGuard } from './core/auth.guard';
  3. import { containerGuard } from './core/container.guard';
  4. import { detectUiMode } from './core/device.service';
  5. import { orgGuard } from './core/org.guard';
  6. /**
  7. * 顶层路由表(ADR-004:双端双套路由树)
  8. *
  9. * 结构:
  10. * '' 入口分流页(UA/视口一次性判定 → replaceUrl 跳端)
  11. * login 登录(两端共用同一组件,形态自适应)
  12. * desktop/** 桌面端(懒加载,DesktopShell 包裹)
  13. * mobile/** 移动端(懒加载,MobileShell 包裹)
  14. * 旧路径 按当前端前缀重定向(FR-16;判定逻辑与 DeviceService.normalize 同源)
  15. * ** 兜底 → 入口页
  16. *
  17. * ⚠️ `redirectTo` 使用 Angular 的 `RedirectFunction`(形参是 route snapshot)。
  18. * 刻意不用「仅挂 canActivate 的 redirectTo 路由」写法 —— 存量为此踩过 NG04014。
  19. */
  20. type RedirectData = Pick<ActivatedRouteSnapshot, 'url'>;
  21. type RedirectFn = (data: RedirectData) => string;
  22. const LEGACY_SIMPLE = [
  23. 'messages',
  24. 'tabs',
  25. 'workbench',
  26. 'deliverables',
  27. 'mine',
  28. 'contacts',
  29. 'projects',
  30. 'events',
  31. ] as const;
  32. /** snapshot.url 是「相对当前路由的剩余段」;顶层路由下即为完整路径 */
  33. const legacyRedirect: RedirectFn = (d) => normalize(`/${segmentsOf(d).join('/')}`);
  34. export const routes: Routes = [
  35. {
  36. path: '',
  37. pathMatch: 'full',
  38. loadComponent: () => import('./pages/entry/entry').then((m) => m.EntryComponent),
  39. },
  40. {
  41. // 登录页分端(2026-11-03):移动端已按 Demo 重建独立形态;
  42. // 桌面端仍用原共用组件。两端登录逻辑共用 FmodeApi / SessionService,仅版式不同。
  43. path: 'login',
  44. loadComponent: () =>
  45. detectMode() === 'mobile'
  46. ? import('./mobile/pages/login/login').then((m) => m.MbLoginPage)
  47. : import('./pages/login/login').then((m) => m.LoginComponent),
  48. },
  49. {
  50. /*
  51. * 等待组织邀请(2026-11-10)。
  52. *
  53. * 🔴 只挂 `authGuard`,**不挂 `orgGuard`** —— 本页正是无组织用户的落点,
  54. * 再挂组织守卫会自我重定向死循环。
  55. */
  56. path: 'pending',
  57. canActivate: [authGuard],
  58. loadComponent: () =>
  59. detectMode() === 'mobile'
  60. ? import('./mobile/pages/pending/pending').then((m) => m.MbPendingPage)
  61. : import('./pages/pending/pending').then((m) => m.PendingPage),
  62. },
  63. {
  64. /*
  65. * 套餐页 / 开通等待页(2026-11-27 提到顶层)
  66. *
  67. * 🔴 为什么必须在 shell 之外:
  68. *
  69. * ① **门禁例外**:无容器的用户只能待在这两个页面,
  70. * 挂在 shell 里会被 `containerGuard` 一并拦掉 → 死循环。
  71. * ② **全屏锁定**:开通等待页不允许出现 TabBar / 抽屉 / 侧边栏,
  72. * 不挂 shell 就等于没有这些东西(比一个个去禁用干净)。
  73. * ③ **缩放不跳转**:shell 的 resize 监听不会触发,
  74. * 用户拖窗口不会把正在开通的页面跳走。
  75. *
  76. * 只挂 `authGuard`(要登录),**不挂 `orgGuard` / `containerGuard`**。
  77. */
  78. path: 'plans',
  79. canActivate: [authGuard],
  80. loadComponent: () =>
  81. detectMode() === 'mobile'
  82. ? import('./mobile/pages/plans/plans').then((m) => m.MbPlansPage)
  83. : import('./desktop/pages/plans/plans').then((m) => m.DkPlansPage),
  84. },
  85. {
  86. /** 开通等待页:全屏锁定,成功/失败后才解锁 */
  87. path: 'provision',
  88. canActivate: [authGuard],
  89. loadComponent: () =>
  90. detectMode() === 'mobile'
  91. ? import('./mobile/pages/provision/provision').then((m) => m.MbProvisionPage)
  92. : import('./desktop/pages/provision/provision').then((m) => m.DkProvisionPage),
  93. },
  94. {
  95. path: 'desktop',
  96. canActivate: [authGuard, orgGuard, containerGuard],
  97. loadChildren: () => import('./desktop/desktop.routes').then((m) => m.desktopRoutes),
  98. },
  99. {
  100. path: 'mobile',
  101. canActivate: [authGuard, orgGuard, containerGuard],
  102. loadChildren: () => import('./mobile/mobile.routes').then((m) => m.mobileRoutes),
  103. },
  104. // ---- 旧路径重定向(FR-16 AC-16.2)----
  105. ...LEGACY_SIMPLE.map((p): Route => ({ path: p, redirectTo: legacyRedirect })),
  106. { path: 'chat/:id', redirectTo: legacyRedirect },
  107. { path: 'chat', redirectTo: (): string => normalize('/chat') },
  108. { path: 'agent/:id', redirectTo: legacyRedirect },
  109. {
  110. // FR-11:语音通话暂不实现 → 落数字生命详情页,由详情页提示「暂未开放」
  111. path: 'call/:id',
  112. /*
  113. * 通话不做(FR-11)→ 落**容器设置页**(2026-11-04 恢复)。
  114. * 用户要从「某容器的来电」跳到该容器的设置,而不是笼统的会话列表。
  115. */
  116. redirectTo: (d): string => normalize(`/agent/${segmentsOf(d).pop() ?? ''}`),
  117. },
  118. { path: '**', redirectTo: (): string => normalize('') },
  119. ];
  120. function segmentsOf(d: RedirectData): string[] {
  121. return d.url.map((s) => s.path);
  122. }
  123. /**
  124. * 旧路径 → 当前端路径。
  125. * 独立成函数(而非注入 DeviceService)是为了让 `redirectTo` 保持**函数式**——
  126. * 存量为此踩过 NG04014 的坑,此处刻意规避。
  127. */
  128. function normalize(url: string): string {
  129. // 纯自动识别(2026-11-10 去掉手动偏好)
  130. const mode = detectMode();
  131. const clean = url.split('?')[0];
  132. const [head, id] = clean.split('/').filter(Boolean);
  133. switch (head) {
  134. case 'messages':
  135. case 'tabs':
  136. return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat';
  137. case 'chat':
  138. return id ? `/${mode}/chat/${id}` : `/${mode}/chat`;
  139. case 'agent':
  140. return `/${mode}/agent/${id ?? ''}`.replace(/\/$/, '');
  141. case 'call':
  142. // 通话暂不实现 → 落详情页
  143. return id ? `/${mode}/agent/${id}` : `/${mode}/chat`;
  144. // 首页已下线(2026-11-02):旧 /home 落到会话入口
  145. case 'home':
  146. return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat';
  147. case 'workbench':
  148. case 'deliverables':
  149. case 'mine':
  150. case 'contacts':
  151. case 'projects':
  152. case 'events':
  153. return `/${mode}/${head}`;
  154. default:
  155. return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat';
  156. }
  157. }
  158. /**
  159. * 登录页 / 等待页该加载哪个组件。
  160. *
  161. * 🔴 2026-11-10:改为复用 `DeviceService` 的纯函数 —— 这里原本是一份
  162. * 逐字复制的 UA 判定,两处一旦不同步就会出现「登录页判移动、入口判桌面」。
  163. *
  164. * 合并口径(2026-11-11):`detectMode` 的**判定内容**取本项目侧的
  165. * 「视口宽度优先 + 去手动偏好」,**写法**取 master 侧的单函数复用。
  166. */
  167. const detectMode = detectUiMode;