app.routes.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * 开通等待页始终使用同一套响应式布局,避免桌面端切窄后仍留在桌面路由,
  88. * 或不同端切换时丢失当前任务。成功/失败后才解锁。
  89. *
  90. * 🔴 按端挂组件(2026-12-02 修):此前两端都挂桌面组件,
  91. * 移动端看到桌面版布局(用户实测「观感混乱」);
  92. * MbProvisionPage 早已存在但未挂载。
  93. */
  94. path: 'provision',
  95. canActivate: [authGuard],
  96. loadComponent: () =>
  97. detectMode() === 'mobile'
  98. ? import('./mobile/pages/provision/provision').then((m) => m.MbProvisionPage)
  99. : import('./desktop/pages/provision/provision').then((m) => m.DkProvisionPage),
  100. },
  101. {
  102. path: 'desktop',
  103. canActivate: [authGuard, orgGuard, containerGuard],
  104. loadChildren: () => import('./desktop/desktop.routes').then((m) => m.desktopRoutes),
  105. },
  106. {
  107. path: 'mobile',
  108. canActivate: [authGuard, orgGuard, containerGuard],
  109. loadChildren: () => import('./mobile/mobile.routes').then((m) => m.mobileRoutes),
  110. },
  111. // ---- 旧路径重定向(FR-16 AC-16.2)----
  112. ...LEGACY_SIMPLE.map((p): Route => ({ path: p, redirectTo: legacyRedirect })),
  113. { path: 'chat/:id', redirectTo: legacyRedirect },
  114. { path: 'chat', redirectTo: (): string => normalize('/chat') },
  115. { path: 'agent/:id', redirectTo: legacyRedirect },
  116. {
  117. // FR-11:语音通话暂不实现 → 落数字生命详情页,由详情页提示「暂未开放」
  118. path: 'call/:id',
  119. /*
  120. * 通话不做(FR-11)→ 落**容器设置页**(2026-11-04 恢复)。
  121. * 用户要从「某容器的来电」跳到该容器的设置,而不是笼统的会话列表。
  122. */
  123. redirectTo: (d): string => normalize(`/agent/${segmentsOf(d).pop() ?? ''}`),
  124. },
  125. { path: '**', redirectTo: (): string => normalize('') },
  126. ];
  127. function segmentsOf(d: RedirectData): string[] {
  128. return d.url.map((s) => s.path);
  129. }
  130. /**
  131. * 旧路径 → 当前端路径。
  132. * 独立成函数(而非注入 DeviceService)是为了让 `redirectTo` 保持**函数式**——
  133. * 存量为此踩过 NG04014 的坑,此处刻意规避。
  134. */
  135. function normalize(url: string): string {
  136. // 纯自动识别(2026-11-10 去掉手动偏好)
  137. const mode = detectMode();
  138. const clean = url.split('?')[0];
  139. const [head, id] = clean.split('/').filter(Boolean);
  140. switch (head) {
  141. case 'messages':
  142. case 'tabs':
  143. return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat';
  144. case 'chat':
  145. return id ? `/${mode}/chat/${id}` : `/${mode}/chat`;
  146. case 'agent':
  147. return `/${mode}/agent/${id ?? ''}`.replace(/\/$/, '');
  148. case 'call':
  149. // 通话暂不实现 → 落详情页
  150. return id ? `/${mode}/agent/${id}` : `/${mode}/chat`;
  151. // 首页已下线(2026-11-02):旧 /home 落到会话入口
  152. case 'home':
  153. return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat';
  154. case 'workbench':
  155. case 'deliverables':
  156. case 'mine':
  157. case 'contacts':
  158. case 'projects':
  159. case 'events':
  160. return `/${mode}/${head}`;
  161. default:
  162. return mode === 'mobile' ? '/mobile/messages' : '/desktop/chat';
  163. }
  164. }
  165. /**
  166. * 登录页 / 等待页该加载哪个组件。
  167. *
  168. * 🔴 2026-11-10:改为复用 `DeviceService` 的纯函数 —— 这里原本是一份
  169. * 逐字复制的 UA 判定,两处一旦不同步就会出现「登录页判移动、入口判桌面」。
  170. *
  171. * 合并口径(2026-11-11):`detectMode` 的**判定内容**取本项目侧的
  172. * 「视口宽度优先 + 去手动偏好」,**写法**取 master 侧的单函数复用。
  173. */
  174. const detectMode = detectUiMode;