| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- import { ChangeDetectionStrategy, Component, OnDestroy, computed, inject, signal } from '@angular/core';
- import { NavigationEnd, Router, RouterOutlet } from '@angular/router';
- import { filter } from 'rxjs';
- import { LiveService } from '../../core/live.service';
- import { MatrixSessionService } from '../../core/matrix-session.service';
- import { PaletteService } from '../../core/palette.service';
- import { DeviceService } from '../../core/device.service';
- import { UnreadService } from '../../core/unread.service';
- import { SessionService } from '../../core/session.service';
- import { FmodeApi } from '../../core/api/fmode-api';
- import { AppNotificationService } from '../../core/app-notification.service';
- import { PermissionGuideComponent } from '../components/permission-guide/permission-guide';
- import { PromptModalComponent } from '../../components/domain/prompt-modal/prompt-modal';
- interface TabDef {
- path: string;
- label: string;
- icon: string;
- }
- /**
- * 移动外壳 —— 底部 4 Tab + 安全区(视觉基线 `prototypes/fmode-mobile-20261102`)
- *
- * 与 Demo 对齐的四点:
- * · 4 Tab:会话 / 通讯录 / 工作台 / 我的(Demo `TAB_PAGES`)
- * · 底栏是**半透明毛玻璃 + 顶部细边**,内容可透出(Demo `.tabbar`)
- * · 会话 Tab 带未读角标(红色胶囊,不是小圆点)
- * · 内容区为底栏预留 `--mob-tabbar-h + 安全区` 高度,不遮挡(rules/18 §11)
- *
- * 顶栏由各页面自行渲染(Demo 的圆形浮钮顶栏是页面级组件 `app-m-app-bar`)。
- */
- @Component({
- selector: 'app-mobile-shell',
- changeDetection: ChangeDetectionStrategy.OnPush,
- imports: [RouterOutlet, PromptModalComponent, PermissionGuideComponent],
- template: `
- <main class="m-main" id="main">
- <router-outlet />
- </main>
- <!--
- 登录后一次性通知权限引导(2026-11-15,仅移动端)。
- 🔴 挂在 shell 而不是登录页:登录会从多条路径回到这里
- (密码 / 验证码 / Token / redirect 回跳),挂在 shell 才能「只问一次」且不漏。
- 是否该弹由 AppNotificationService 判断(问过就不再自动弹)。
- -->
- <app-permission-guide [open]="permGuideOpen()" (dismiss)="onPermDismiss()" />
- <nav class="m-tabbar" aria-label="主导航">
- @for (tab of tabs; track tab.path; let i = $index) {
- <button
- type="button"
- class="m-tab"
- [class.on]="activeIndex() === i"
- [attr.aria-current]="activeIndex() === i ? 'page' : null"
- (click)="go(tab.path)"
- >
- <span class="m-tab-icon">
- <i [class]="tab.icon" aria-hidden="true"></i>
- @if (tab.path === '/messages' && totalUnread() > 0) {
- <span class="m-tab-badge">{{ totalUnread() > 99 ? '99+' : totalUnread() }}</span>
- }
- </span>
- <span class="m-tab-label">{{ tab.label }}</span>
- </button>
- }
- </nav>
- <!-- 全局输入弹窗(新建/重命名工作区等):与桌面同一服务,移动端此前未挂载 -->
- <app-prompt-modal />
- @if (wideViewport()) {
- <!--
- 窗口变宽时的过渡态(2026-11-10 改为自动跳转)。
- 用户裁决:去掉手动切换,自动识别屏幕大小切换。
- 不再给按钮,组件会自动导航到桌面端,这里只显示「正在切换」。
- -->
- <div class="wide-mobile" role="status" aria-label="正在切换到桌面端布局">
- <div class="wide-card">
- <span class="wide-icon" aria-hidden="true"><i class="fa-solid fa-display"></i></span>
- <h2>正在切换到桌面端布局</h2>
- <p>窗口宽度已达到 768px,已自动适配桌面端。</p>
- </div>
- </div>
- }
- `,
- styles: [
- `
- :host {
- display: block;
- height: 100dvh;
- background: var(--canvas);
- color: var(--ink);
- }
- /*
- 内容区滚动,并为底栏预留空间 —— 底栏是 fixed,
- 不预留就会被盖住最后一段内容(rules/18 §11 防遮挡底线)。
- */
- .m-main {
- height: 100dvh;
- overflow-y: auto;
- -webkit-overflow-scrolling: touch;
- overscroll-behavior-y: contain;
- padding-bottom: calc(var(--mob-tabbar-h) + var(--mob-safe-bottom) + var(--sp-4));
- box-sizing: border-box;
- }
- /*
- 页面自己管滚动(会话列表等三态固定结构)时不叠加外壳滚动。
- 但**必须**把底栏预留传下去:.m-page--fixed 高度是 100dvh,
- 若只留外壳 padding,内部滚动区仍会被 fixed 底栏压住(实测遮挡)。
- */
- /*
- * 页面自己管滚动时,外壳**不再滚动**(2026-11-04 统一)。
- * 覆盖两类页面:
- * · .m-page--fixed —— 三态固定结构(会话列表、通讯录等)
- * · .m-page--scroll —— 固定头部 + 内容滚动(工作台、我的等)
- * 🔴 原来的问题:工作台同时有 m-main 和内部 m-body 两个滚动容器,
- * 手指滑动时内外层互相抢,出现「滚一半突然跳到另一层」的错乱手感。
- * 现在只有页面内部的滚动区生效。
- */
- .m-main:has(.m-page--fixed),
- .m-main:has(.m-page--scroll) {
- overflow: hidden;
- padding-bottom: 0;
- }
- /*
- 底栏:毛玻璃 + 顶部细边(Demo .tabbar)
- 🔴 层级必须**低于抽屉**(--z-float 950 < --z-modal 1100):
- 抽屉滑出时要盖住整个屏幕(含底栏),否则视觉上像被切了一刀。
- 上一版误把底栏提到 1110,导致抽屉被压在下面(用户实测指出)。
- 抽屉内部已为底栏预留空间(见 m-drawer 的 padding-bottom),
- 所以底栏被盖住也不会让内容被切。
- */
- .m-tabbar {
- position: fixed;
- left: 0;
- right: 0;
- bottom: 0;
- z-index: var(--z-float);
- display: flex;
- align-items: stretch;
- height: calc(var(--mob-tabbar-h) + var(--mob-safe-bottom));
- padding-bottom: var(--mob-safe-bottom);
- background: color-mix(in srgb, var(--canvas) 92%, transparent);
- -webkit-backdrop-filter: blur(16px);
- backdrop-filter: blur(16px);
- border-top: 1px solid var(--edge);
- }
- .m-tab {
- flex: 1;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- gap: var(--sp-1);
- border: 0;
- background: none;
- color: var(--muted);
- font: inherit;
- font-size: var(--fs-12);
- cursor: pointer;
- transition: color var(--motion-fast) var(--ease-standard);
- }
- .m-tab.on {
- color: var(--primary);
- font-weight: 600;
- }
- .m-tab-icon {
- position: relative;
- display: inline-grid;
- place-items: center;
- font-size: var(--fs-20);
- line-height: 1;
- }
- .m-tab-badge {
- position: absolute;
- top: -6px;
- left: 11px;
- min-width: 16px;
- height: 16px;
- padding: 0 4px;
- border-radius: var(--r-tag);
- background: var(--danger);
- color: var(--on-danger);
- font-size: var(--fs-11);
- font-weight: 600;
- line-height: 16px;
- text-align: center;
- }
- .wide-mobile {
- position: fixed;
- inset: 0;
- z-index: var(--z-mask);
- display: grid;
- place-items: center;
- padding: var(--sp-6);
- background: var(--canvas);
- }
- .wide-card {
- display: grid;
- justify-items: center;
- gap: var(--sp-2);
- max-width: 360px;
- text-align: center;
- }
- .wide-icon {
- display: grid;
- place-items: center;
- width: 56px;
- height: 56px;
- border-radius: var(--r-lg);
- background: var(--primary-subtle);
- color: var(--primary);
- font-size: var(--fs-20);
- }
- .wide-card h2 {
- margin: 0;
- color: var(--ink);
- font-size: var(--fs-20);
- }
- .wide-card p {
- margin: 0;
- color: var(--muted);
- font-size: var(--fs-13);
- line-height: 1.7;
- }
- /* 两个动作并排:主操作在右(拇指顺位),次要操作在左 */
- .wide-actions {
- display: flex;
- gap: var(--sp-2);
- margin-top: var(--sp-3);
- }
- .wide-btn {
- flex: 1;
- min-height: var(--touch-min);
- padding: 0 var(--sp-3);
- border: 0;
- border-radius: var(--r-sm);
- background: var(--primary);
- color: var(--primary-content);
- font-size: var(--fs-13);
- cursor: pointer;
- /* 并排时宽度有限,别让「切换到桌面端」折成两行 */
- white-space: nowrap;
- }
- .wide-btn.ghost {
- background: transparent;
- color: var(--primary);
- border: 1px solid var(--edge);
- }
- `,
- ],
- })
- export class MobileShellComponent implements OnDestroy {
- /** 登录后的通知权限引导(2026-11-15,仅移动端) */
- private readonly notifySvc = inject(AppNotificationService);
- protected readonly permGuideOpen = signal(false);
- /**
- * 是否需要弹通知权限引导。
- *
- * 延后 800ms:刚登录进来要加载会话列表、建 Matrix 连接,
- * 弹层立刻盖上来会显得很急躁,也让首屏看起来像卡住了。
- */
- private async maybeShowPermGuide(): Promise<void> {
- if (!this.notifySvc.shouldPromptPermission()) return;
- /* 已授权的就没必要问 —— 直接标记,不再打扰 */
- if ((await this.notifySvc.isGranted()) === true) {
- this.notifySvc.markPrompted();
- return;
- }
- await new Promise((r) => setTimeout(r, 800));
- this.permGuideOpen.set(true);
- }
- /** 用户关掉引导(同意或拒绝都算),记下「问过了」 */
- protected onPermDismiss(): void {
- this.permGuideOpen.set(false);
- this.notifySvc.markPrompted();
- }
- private readonly live = inject(LiveService);
- private readonly matrix = inject(MatrixSessionService);
- private readonly palette = inject(PaletteService);
- private readonly unread = inject(UnreadService);
- private readonly session = inject(SessionService);
- private readonly api = inject(FmodeApi);
- private readonly router = inject(Router);
- readonly device = inject(DeviceService);
- /** 4 个 Tab(顺序即底栏顺序;与 Demo TAB_PAGES 一致) */
- protected readonly tabs: TabDef[] = [
- { path: '/messages', label: '会话', icon: 'fa-solid fa-comment' },
- { path: '/contacts', label: '通讯录', icon: 'fa-solid fa-user-group' },
- { path: '/workbench', label: '工作台', icon: 'fa-solid fa-table-cells-large' },
- { path: '/mine', label: '我的', icon: 'fa-solid fa-user' },
- ];
- /** 底栏会话 Tab 未读总数(UnreadService 唯一来源) */
- protected readonly totalUnread = computed(() =>
- Object.values(this.unread.counts()).reduce((n, v) => n + v, 0),
- );
- protected readonly activeIndex = signal(0);
- protected readonly wideViewport = signal(this.isWideViewport());
- constructor() {
- // 🔴 唯一轮询器的启停点
- this.live.start();
- // 🔴 Matrix 会话级连接(常驻,未读计数依赖它)
- this.matrix.bootstrap();
- this.palette.init();
- /* 登录后一次性通知权限引导(2026-11-15) */
- void this.maybeShowPermGuide();
- /*
- * 启动时刷新身份(2026-11-05):管理员角色可能在后台被调整过,
- * 只登录一次、之后一直用本地副本会导致权限变化不生效。
- * 失败静默,不打断使用。
- */
- if (this.session.token()) {
- /* 本地 session 已过期 → 登出并回登录页(2026-11-12,与桌面端同口径) */
- if (this.session.isExpired()) {
- this.session.logout();
- void this.router.navigateByUrl('/login');
- } else {
- void this.session.refreshMe((t) => this.api.fetchMe(t));
- }
- }
- if (typeof window !== 'undefined') {
- window.addEventListener('resize', () => this.onResize(), { passive: true });
- // 首次进入也检查一次(可能直接访问了 /mobile/... 但窗口是宽的)
- setTimeout(() => this.onResize(), 300);
- }
- this.syncFromUrl(this.router.url);
- this.router.events
- .pipe(filter((e): e is NavigationEnd => e instanceof NavigationEnd))
- .subscribe((e) => this.syncFromUrl(e.urlAfterRedirects));
- }
- protected go(path: string): void {
- void this.router.navigateByUrl(`/mobile${path}`);
- }
- /**
- * 窗口尺寸变化 → 自动切换到桌面端(2026-11-10 用户裁决)。
- *
- * 与桌面外壳对称:防抖 400ms,避免拖窗口时反复导航。
- * 只在「够宽」时跳,窄屏不主动跳回移动端(那边自己负责)。
- */
- private resizeTimer: ReturnType<typeof setTimeout> | null = null;
- private onResize(): void {
- this.wideViewport.set(this.isWideViewport());
- if (this.resizeTimer !== null) clearTimeout(this.resizeTimer);
- this.resizeTimer = setTimeout(() => {
- this.resizeTimer = null;
- /*
- * 与桌面侧同一修正:判据用「目标端」,不用 `changed`。
- * 用宽窗口直接打开 `/mobile/...` 时 `mode` 本来就是 desktop,
- * `changed` 恒为 false → 永远不跳(用户截图实测)。
- */
- const { mode } = this.device.recheck();
- if (mode !== 'desktop') return;
- void this.router.navigateByUrl('/desktop/chat', { replaceUrl: true });
- }, 400);
- }
- /** 卸载时清掉未触发的防抖定时器,避免外壳销毁后仍导航 */
- ngOnDestroy(): void {
- if (this.resizeTimer !== null) clearTimeout(this.resizeTimer);
- this.resizeTimer = null;
- }
- private isWideViewport(): boolean {
- return typeof window !== 'undefined' && window.innerWidth >= 768;
- }
- private syncFromUrl(url: string): void {
- const path = url.split('?')[0].replace(/^\/mobile/, '') || '/messages';
- const idx = this.tabs.findIndex((t) => path === t.path || path.startsWith(t.path + '/'));
- this.activeIndex.set(idx >= 0 ? idx : 0);
- }
- }
|