mobile-shell.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import { ChangeDetectionStrategy, Component, OnDestroy, computed, inject, signal } from '@angular/core';
  2. import { NavigationEnd, Router, RouterOutlet } from '@angular/router';
  3. import { filter } from 'rxjs';
  4. import { LiveService } from '../../core/live.service';
  5. import { MatrixSessionService } from '../../core/matrix-session.service';
  6. import { PaletteService } from '../../core/palette.service';
  7. import { DeviceService } from '../../core/device.service';
  8. import { UnreadService } from '../../core/unread.service';
  9. import { SessionService } from '../../core/session.service';
  10. import { FmodeApi } from '../../core/api/fmode-api';
  11. import { AppNotificationService } from '../../core/app-notification.service';
  12. import { PermissionGuideComponent } from '../components/permission-guide/permission-guide';
  13. import { PromptModalComponent } from '../../components/domain/prompt-modal/prompt-modal';
  14. interface TabDef {
  15. path: string;
  16. label: string;
  17. icon: string;
  18. }
  19. /**
  20. * 移动外壳 —— 底部 4 Tab + 安全区(视觉基线 `prototypes/fmode-mobile-20261102`)
  21. *
  22. * 与 Demo 对齐的四点:
  23. * · 4 Tab:会话 / 通讯录 / 工作台 / 我的(Demo `TAB_PAGES`)
  24. * · 底栏是**半透明毛玻璃 + 顶部细边**,内容可透出(Demo `.tabbar`)
  25. * · 会话 Tab 带未读角标(红色胶囊,不是小圆点)
  26. * · 内容区为底栏预留 `--mob-tabbar-h + 安全区` 高度,不遮挡(rules/18 §11)
  27. *
  28. * 顶栏由各页面自行渲染(Demo 的圆形浮钮顶栏是页面级组件 `app-m-app-bar`)。
  29. */
  30. @Component({
  31. selector: 'app-mobile-shell',
  32. changeDetection: ChangeDetectionStrategy.OnPush,
  33. imports: [RouterOutlet, PromptModalComponent, PermissionGuideComponent],
  34. template: `
  35. <main class="m-main" id="main">
  36. <router-outlet />
  37. </main>
  38. <!--
  39. 登录后一次性通知权限引导(2026-11-15,仅移动端)。
  40. 🔴 挂在 shell 而不是登录页:登录会从多条路径回到这里
  41. (密码 / 验证码 / Token / redirect 回跳),挂在 shell 才能「只问一次」且不漏。
  42. 是否该弹由 AppNotificationService 判断(问过就不再自动弹)。
  43. -->
  44. <app-permission-guide [open]="permGuideOpen()" (dismiss)="onPermDismiss()" />
  45. <nav class="m-tabbar" aria-label="主导航">
  46. @for (tab of tabs; track tab.path; let i = $index) {
  47. <button
  48. type="button"
  49. class="m-tab"
  50. [class.on]="activeIndex() === i"
  51. [attr.aria-current]="activeIndex() === i ? 'page' : null"
  52. (click)="go(tab.path)"
  53. >
  54. <span class="m-tab-icon">
  55. <i [class]="tab.icon" aria-hidden="true"></i>
  56. @if (tab.path === '/messages' && totalUnread() > 0) {
  57. <span class="m-tab-badge">{{ totalUnread() > 99 ? '99+' : totalUnread() }}</span>
  58. }
  59. </span>
  60. <span class="m-tab-label">{{ tab.label }}</span>
  61. </button>
  62. }
  63. </nav>
  64. <!-- 全局输入弹窗(新建/重命名工作区等):与桌面同一服务,移动端此前未挂载 -->
  65. <app-prompt-modal />
  66. @if (wideViewport()) {
  67. <!--
  68. 窗口变宽时的过渡态(2026-11-10 改为自动跳转)。
  69. 用户裁决:去掉手动切换,自动识别屏幕大小切换。
  70. 不再给按钮,组件会自动导航到桌面端,这里只显示「正在切换」。
  71. -->
  72. <div class="wide-mobile" role="status" aria-label="正在切换到桌面端布局">
  73. <div class="wide-card">
  74. <span class="wide-icon" aria-hidden="true"><i class="fa-solid fa-display"></i></span>
  75. <h2>正在切换到桌面端布局</h2>
  76. <p>窗口宽度已达到 768px,已自动适配桌面端。</p>
  77. </div>
  78. </div>
  79. }
  80. `,
  81. styles: [
  82. `
  83. :host {
  84. display: block;
  85. height: 100dvh;
  86. background: var(--canvas);
  87. color: var(--ink);
  88. }
  89. /*
  90. 内容区滚动,并为底栏预留空间 —— 底栏是 fixed,
  91. 不预留就会被盖住最后一段内容(rules/18 §11 防遮挡底线)。
  92. */
  93. .m-main {
  94. height: 100dvh;
  95. overflow-y: auto;
  96. -webkit-overflow-scrolling: touch;
  97. overscroll-behavior-y: contain;
  98. padding-bottom: calc(var(--mob-tabbar-h) + var(--mob-safe-bottom) + var(--sp-4));
  99. box-sizing: border-box;
  100. }
  101. /*
  102. 页面自己管滚动(会话列表等三态固定结构)时不叠加外壳滚动。
  103. 但**必须**把底栏预留传下去:.m-page--fixed 高度是 100dvh,
  104. 若只留外壳 padding,内部滚动区仍会被 fixed 底栏压住(实测遮挡)。
  105. */
  106. /*
  107. * 页面自己管滚动时,外壳**不再滚动**(2026-11-04 统一)。
  108. * 覆盖两类页面:
  109. * · .m-page--fixed —— 三态固定结构(会话列表、通讯录等)
  110. * · .m-page--scroll —— 固定头部 + 内容滚动(工作台、我的等)
  111. * 🔴 原来的问题:工作台同时有 m-main 和内部 m-body 两个滚动容器,
  112. * 手指滑动时内外层互相抢,出现「滚一半突然跳到另一层」的错乱手感。
  113. * 现在只有页面内部的滚动区生效。
  114. */
  115. .m-main:has(.m-page--fixed),
  116. .m-main:has(.m-page--scroll) {
  117. overflow: hidden;
  118. padding-bottom: 0;
  119. }
  120. /*
  121. 底栏:毛玻璃 + 顶部细边(Demo .tabbar)
  122. 🔴 层级必须**低于抽屉**(--z-float 950 < --z-modal 1100):
  123. 抽屉滑出时要盖住整个屏幕(含底栏),否则视觉上像被切了一刀。
  124. 上一版误把底栏提到 1110,导致抽屉被压在下面(用户实测指出)。
  125. 抽屉内部已为底栏预留空间(见 m-drawer 的 padding-bottom),
  126. 所以底栏被盖住也不会让内容被切。
  127. */
  128. .m-tabbar {
  129. position: fixed;
  130. left: 0;
  131. right: 0;
  132. bottom: 0;
  133. z-index: var(--z-float);
  134. display: flex;
  135. align-items: stretch;
  136. height: calc(var(--mob-tabbar-h) + var(--mob-safe-bottom));
  137. padding-bottom: var(--mob-safe-bottom);
  138. background: color-mix(in srgb, var(--canvas) 92%, transparent);
  139. -webkit-backdrop-filter: blur(16px);
  140. backdrop-filter: blur(16px);
  141. border-top: 1px solid var(--edge);
  142. }
  143. .m-tab {
  144. flex: 1;
  145. display: flex;
  146. flex-direction: column;
  147. align-items: center;
  148. justify-content: center;
  149. gap: var(--sp-1);
  150. border: 0;
  151. background: none;
  152. color: var(--muted);
  153. font: inherit;
  154. font-size: var(--fs-12);
  155. cursor: pointer;
  156. transition: color var(--motion-fast) var(--ease-standard);
  157. }
  158. .m-tab.on {
  159. color: var(--primary);
  160. font-weight: 600;
  161. }
  162. .m-tab-icon {
  163. position: relative;
  164. display: inline-grid;
  165. place-items: center;
  166. font-size: var(--fs-20);
  167. line-height: 1;
  168. }
  169. .m-tab-badge {
  170. position: absolute;
  171. top: -6px;
  172. left: 11px;
  173. min-width: 16px;
  174. height: 16px;
  175. padding: 0 4px;
  176. border-radius: var(--r-tag);
  177. background: var(--danger);
  178. color: var(--on-danger);
  179. font-size: var(--fs-11);
  180. font-weight: 600;
  181. line-height: 16px;
  182. text-align: center;
  183. }
  184. .wide-mobile {
  185. position: fixed;
  186. inset: 0;
  187. z-index: var(--z-mask);
  188. display: grid;
  189. place-items: center;
  190. padding: var(--sp-6);
  191. background: var(--canvas);
  192. }
  193. .wide-card {
  194. display: grid;
  195. justify-items: center;
  196. gap: var(--sp-2);
  197. max-width: 360px;
  198. text-align: center;
  199. }
  200. .wide-icon {
  201. display: grid;
  202. place-items: center;
  203. width: 56px;
  204. height: 56px;
  205. border-radius: var(--r-lg);
  206. background: var(--primary-subtle);
  207. color: var(--primary);
  208. font-size: var(--fs-20);
  209. }
  210. .wide-card h2 {
  211. margin: 0;
  212. color: var(--ink);
  213. font-size: var(--fs-20);
  214. }
  215. .wide-card p {
  216. margin: 0;
  217. color: var(--muted);
  218. font-size: var(--fs-13);
  219. line-height: 1.7;
  220. }
  221. /* 两个动作并排:主操作在右(拇指顺位),次要操作在左 */
  222. .wide-actions {
  223. display: flex;
  224. gap: var(--sp-2);
  225. margin-top: var(--sp-3);
  226. }
  227. .wide-btn {
  228. flex: 1;
  229. min-height: var(--touch-min);
  230. padding: 0 var(--sp-3);
  231. border: 0;
  232. border-radius: var(--r-sm);
  233. background: var(--primary);
  234. color: var(--primary-content);
  235. font-size: var(--fs-13);
  236. cursor: pointer;
  237. /* 并排时宽度有限,别让「切换到桌面端」折成两行 */
  238. white-space: nowrap;
  239. }
  240. .wide-btn.ghost {
  241. background: transparent;
  242. color: var(--primary);
  243. border: 1px solid var(--edge);
  244. }
  245. `,
  246. ],
  247. })
  248. export class MobileShellComponent implements OnDestroy {
  249. /** 登录后的通知权限引导(2026-11-15,仅移动端) */
  250. private readonly notifySvc = inject(AppNotificationService);
  251. protected readonly permGuideOpen = signal(false);
  252. /**
  253. * 是否需要弹通知权限引导。
  254. *
  255. * 延后 800ms:刚登录进来要加载会话列表、建 Matrix 连接,
  256. * 弹层立刻盖上来会显得很急躁,也让首屏看起来像卡住了。
  257. */
  258. private async maybeShowPermGuide(): Promise<void> {
  259. if (!this.notifySvc.shouldPromptPermission()) return;
  260. /* 已授权的就没必要问 —— 直接标记,不再打扰 */
  261. if ((await this.notifySvc.isGranted()) === true) {
  262. this.notifySvc.markPrompted();
  263. return;
  264. }
  265. await new Promise((r) => setTimeout(r, 800));
  266. this.permGuideOpen.set(true);
  267. }
  268. /** 用户关掉引导(同意或拒绝都算),记下「问过了」 */
  269. protected onPermDismiss(): void {
  270. this.permGuideOpen.set(false);
  271. this.notifySvc.markPrompted();
  272. }
  273. private readonly live = inject(LiveService);
  274. private readonly matrix = inject(MatrixSessionService);
  275. private readonly palette = inject(PaletteService);
  276. private readonly unread = inject(UnreadService);
  277. private readonly session = inject(SessionService);
  278. private readonly api = inject(FmodeApi);
  279. private readonly router = inject(Router);
  280. readonly device = inject(DeviceService);
  281. /** 4 个 Tab(顺序即底栏顺序;与 Demo TAB_PAGES 一致) */
  282. protected readonly tabs: TabDef[] = [
  283. { path: '/messages', label: '会话', icon: 'fa-solid fa-comment' },
  284. { path: '/contacts', label: '通讯录', icon: 'fa-solid fa-user-group' },
  285. { path: '/workbench', label: '工作台', icon: 'fa-solid fa-table-cells-large' },
  286. { path: '/mine', label: '我的', icon: 'fa-solid fa-user' },
  287. ];
  288. /** 底栏会话 Tab 未读总数(UnreadService 唯一来源) */
  289. protected readonly totalUnread = computed(() =>
  290. Object.values(this.unread.counts()).reduce((n, v) => n + v, 0),
  291. );
  292. protected readonly activeIndex = signal(0);
  293. protected readonly wideViewport = signal(this.isWideViewport());
  294. constructor() {
  295. // 🔴 唯一轮询器的启停点
  296. this.live.start();
  297. // 🔴 Matrix 会话级连接(常驻,未读计数依赖它)
  298. this.matrix.bootstrap();
  299. this.palette.init();
  300. /* 登录后一次性通知权限引导(2026-11-15) */
  301. void this.maybeShowPermGuide();
  302. /*
  303. * 启动时刷新身份(2026-11-05):管理员角色可能在后台被调整过,
  304. * 只登录一次、之后一直用本地副本会导致权限变化不生效。
  305. * 失败静默,不打断使用。
  306. */
  307. if (this.session.token()) {
  308. /* 本地 session 已过期 → 登出并回登录页(2026-11-12,与桌面端同口径) */
  309. if (this.session.isExpired()) {
  310. this.session.logout();
  311. void this.router.navigateByUrl('/login');
  312. } else {
  313. void this.session.refreshMe((t) => this.api.fetchMe(t));
  314. }
  315. }
  316. if (typeof window !== 'undefined') {
  317. window.addEventListener('resize', () => this.onResize(), { passive: true });
  318. // 首次进入也检查一次(可能直接访问了 /mobile/... 但窗口是宽的)
  319. setTimeout(() => this.onResize(), 300);
  320. }
  321. this.syncFromUrl(this.router.url);
  322. this.router.events
  323. .pipe(filter((e): e is NavigationEnd => e instanceof NavigationEnd))
  324. .subscribe((e) => this.syncFromUrl(e.urlAfterRedirects));
  325. }
  326. protected go(path: string): void {
  327. void this.router.navigateByUrl(`/mobile${path}`);
  328. }
  329. /**
  330. * 窗口尺寸变化 → 自动切换到桌面端(2026-11-10 用户裁决)。
  331. *
  332. * 与桌面外壳对称:防抖 400ms,避免拖窗口时反复导航。
  333. * 只在「够宽」时跳,窄屏不主动跳回移动端(那边自己负责)。
  334. */
  335. private resizeTimer: ReturnType<typeof setTimeout> | null = null;
  336. private onResize(): void {
  337. this.wideViewport.set(this.isWideViewport());
  338. if (this.resizeTimer !== null) clearTimeout(this.resizeTimer);
  339. this.resizeTimer = setTimeout(() => {
  340. this.resizeTimer = null;
  341. /*
  342. * 与桌面侧同一修正:判据用「目标端」,不用 `changed`。
  343. * 用宽窗口直接打开 `/mobile/...` 时 `mode` 本来就是 desktop,
  344. * `changed` 恒为 false → 永远不跳(用户截图实测)。
  345. */
  346. const { mode } = this.device.recheck();
  347. if (mode !== 'desktop') return;
  348. void this.router.navigateByUrl('/desktop/chat', { replaceUrl: true });
  349. }, 400);
  350. }
  351. /** 卸载时清掉未触发的防抖定时器,避免外壳销毁后仍导航 */
  352. ngOnDestroy(): void {
  353. if (this.resizeTimer !== null) clearTimeout(this.resizeTimer);
  354. this.resizeTimer = null;
  355. }
  356. private isWideViewport(): boolean {
  357. return typeof window !== 'undefined' && window.innerWidth >= 768;
  358. }
  359. private syncFromUrl(url: string): void {
  360. const path = url.split('?')[0].replace(/^\/mobile/, '') || '/messages';
  361. const idx = this.tabs.findIndex((t) => path === t.path || path.startsWith(t.path + '/'));
  362. this.activeIndex.set(idx >= 0 ? idx : 0);
  363. }
  364. }