mine.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. import { AppNotificationService } from '../../../core/app-notification.service';
  2. import { NotificationSetupService } from '../../../core/notification-setup.service';
  3. import { isNativeApp } from '../../../core/api/platform-proxy.service';
  4. import { ChatService } from '../../../core/chat.service';
  5. import { ChangeDetectionStrategy, Component, computed, inject, signal } from '@angular/core';
  6. import { Router } from '@angular/router';
  7. import { CHAT_FONT_SIZES, ChatFontService, UI_SCALES, UI_SCALE_LABEL } from '../../../core/chat-font.service';
  8. import { DEFAULT_PALETTE, PALETTES, PaletteService } from '../../../core/palette.service';
  9. import { PaletteId } from '../../../core/api/models';
  10. import { SessionService } from '../../../core/session.service';
  11. import { ChatSessionService } from '../../../core/chat-session.service';
  12. import { ThemeService } from '../../../ui/theme/theme.service';
  13. import { MAppBarComponent } from '../../components/m-app-bar/m-app-bar';
  14. import { MAvatarComponent } from '../../components/m-avatar/m-avatar';
  15. import { MDrawerComponent } from '../../components/m-drawer/m-drawer';
  16. import { MConfirmComponent } from '../../components/m-confirm/m-confirm';
  17. import { AppUpdateService } from '../../../core/app-update.service';
  18. import { UpdateDialogComponent } from '../../components/m-update-dialog/m-update-dialog';
  19. /**
  20. * 我的 / 设置(Demo 16)
  21. *
  22. * 顺序:个人资料 / 通知(原生端)/ 外观 / 账号与 Token / 退出。
  23. *
  24. * 配色与深色直接复用既有服务(`PaletteService` / 底座 `ThemeService`),
  25. * 字号走 `ChatFontService`(写 `--ui-scale`),与桌面设置项**同一真源**,
  26. * 不另建一套偏好存储。
  27. */
  28. @Component({
  29. selector: 'app-mb-mine-page',
  30. changeDetection: ChangeDetectionStrategy.OnPush,
  31. imports: [MAppBarComponent, MAvatarComponent, MDrawerComponent, MConfirmComponent, UpdateDialogComponent],
  32. template: `
  33. <div class="m-page--scroll">
  34. <app-m-app-bar title="我的" [menu]="true" (menuTap)="drawerOpen.set(true)" />
  35. <div class="m-body">
  36. <section class="m-section">
  37. <div class="m-card m-profile">
  38. <app-m-avatar [name]="me()?.name || '未登录'" size="lg" tone="person" />
  39. <div class="m-profile-body">
  40. <div class="m-profile-name">{{ me()?.name || '未登录' }}</div>
  41. <div class="m-profile-sub">{{ me()?.mobile || '未设置手机号' }}@if (me()?.companyName) { · {{ me()?.companyName }} }</div>
  42. </div>
  43. </div>
  44. </section>
  45. <!--
  46. 通知(2026-11-15):把「开不开系统通知」这件事摆到明面上。
  47. 🔴 为什么要有这一项:Android 13+ 首次拒绝通知权限后,系统**不再弹窗**,
  48. 用户会一直以为「这个 App 就是不推送」,也不知道去哪改。
  49. 这里给一个明确入口 + 状态显示,点一下就去系统设置。
  50. -->
  51. @if (isNative()) {
  52. <section class="m-section">
  53. <h2 class="m-section-title">通知</h2>
  54. <div class="m-card">
  55. <button type="button" class="m-row" (click)="onNotificationRow()">
  56. <span class="m-row-body">
  57. <span class="m-row-t1">消息与任务通知</span>
  58. <span class="m-row-sub">{{ notifyStateText() }}</span>
  59. </span>
  60. <i class="fa-solid fa-chevron-right m-chev" aria-hidden="true"></i>
  61. </button>
  62. </div>
  63. </section>
  64. }
  65. <section class="m-section">
  66. <h2 class="m-section-title">外观</h2>
  67. <div class="m-card">
  68. <p class="m-label">整体配色</p>
  69. <div class="m-palette-grid">
  70. @for (p of palettes; track p.id) {
  71. <button
  72. type="button"
  73. class="m-palette"
  74. [class.on]="palette.palette() === p.id"
  75. [attr.aria-pressed]="palette.palette() === p.id"
  76. (click)="setPalette(p.id)"
  77. >
  78. <span class="m-swatch" [attr.data-preview-palette]="p.id" aria-hidden="true">
  79. <i></i><i></i><i></i>
  80. </span>
  81. {{ p.name }}
  82. </button>
  83. }
  84. </div>
  85. <div class="m-row m-row--plain">
  86. <span class="m-row-body">
  87. <span class="m-row-t1">深色模式</span>
  88. </span>
  89. <button
  90. type="button"
  91. class="m-switch"
  92. role="switch"
  93. [attr.aria-checked]="theme.isDark()"
  94. aria-label="深色模式"
  95. (click)="toggleDark()"
  96. ></button>
  97. </div>
  98. <!--
  99. 字号(2026-11-21 用户裁决):拆成「正文大小」和「界面大小」两项。
  100. 2026-11-23 重排:说明文字从控件之间挪到底部一句脚注,
  101. 预览去掉背景框 —— 用户反馈卡片中间松垮、间隔不舒服。
  102. -->
  103. <div class="m-font">
  104. <div class="m-font-head">
  105. <span class="m-row-t1">正文大小</span>
  106. <span class="m-font-val">{{ font.size() }}px</span>
  107. </div>
  108. <input
  109. class="m-slider"
  110. type="range"
  111. [min]="fontMin"
  112. [max]="fontMax"
  113. step="1"
  114. [value]="font.size()"
  115. aria-label="正文大小"
  116. aria-describedby="global-font-hint"
  117. (input)="onFont($event)"
  118. />
  119. <!-- 轻量预览:不加背景框,所见即所得 -->
  120. <p class="m-font-preview" [style.font-size.px]="font.size()" aria-hidden="true">
  121. 预览:对话正文效果
  122. </p>
  123. <div class="m-font-head m-font-head--ui">
  124. <span class="m-row-t1">界面大小</span>
  125. <span class="m-font-val">{{ uiScaleLabel() }}</span>
  126. </div>
  127. <div class="m-ui-scale" role="radiogroup" aria-label="界面大小">
  128. @for (opt of uiScaleOptions; track opt.value) {
  129. <button
  130. type="button"
  131. role="radio"
  132. [attr.aria-checked]="font.ui() === opt.value"
  133. [class.on]="font.ui() === opt.value"
  134. (click)="font.setUi(opt.value)"
  135. >
  136. {{ opt.label }}
  137. </button>
  138. }
  139. </div>
  140. <p id="global-font-hint" class="m-font-hint">
  141. 正文大小影响对话内容;界面大小影响导航与图标。
  142. </p>
  143. </div>
  144. </div>
  145. </section>
  146. <section class="m-section">
  147. <h2 class="m-section-title">账号</h2>
  148. <ul class="m-list">
  149. <!--
  150. 访问 Token(2026-11-04 对齐 PC):可显示/隐藏 + 复制。
  151. 默认脱敏,避免误拍屏泄漏;显示后可直接复制用于排障或换设备登录。
  152. -->
  153. <li class="m-token-row">
  154. <span class="m-row-body">
  155. <span class="m-row-t1">访问 Token</span>
  156. <span class="m-row-t2 m-token-value" [class.m-token--masked]="!tokenShown()">
  157. {{ tokenShown() ? tokenText() : maskToken() }}
  158. </span>
  159. </span>
  160. <button
  161. type="button"
  162. class="m-token-btn"
  163. [attr.aria-label]="tokenShown() ? '隐藏 Token' : '显示 Token'"
  164. (click)="tokenShown.set(!tokenShown())"
  165. >
  166. <i [class]="tokenShown() ? 'fa-solid fa-eye-slash' : 'fa-solid fa-eye'" aria-hidden="true"></i>
  167. </button>
  168. <button
  169. type="button"
  170. class="m-token-btn"
  171. aria-label="复制 Token"
  172. [disabled]="!tokenShown()"
  173. (click)="copyToken()"
  174. >
  175. <i [class]="copied() ? 'fa-solid fa-check' : 'fa-solid fa-copy'" aria-hidden="true"></i>
  176. </button>
  177. </li>
  178. <!--
  179. 关于与版本(2026-11-23 新增)
  180. 用户要求在「我的」页能看到版本号,并能点开查更新 ——
  181. 以前这里完全没有版本概念,装了什么版本自己都不知道。
  182. -->
  183. <li>
  184. <button type="button" class="m-row" (click)="checkUpdateNow()" [disabled]="updating()">
  185. <span class="m-row-body">
  186. <span class="m-row-t1">关于与版本</span>
  187. <span class="m-row-t2">
  188. @if (updating()) {
  189. 正在检查更新…
  190. } @else if (updateTip()) {
  191. {{ updateTip() }}
  192. } @else if (update.hasUpdate()) {
  193. 有新版本 {{ update.remote()?.version }} 可更新
  194. } @else {
  195. {{ versionText() }}
  196. }
  197. </span>
  198. </span>
  199. @if (update.hasUpdate()) {
  200. <span class="m-update-dot" aria-label="有新版本"></span>
  201. } @else {
  202. <i class="fa-solid fa-chevron-right m-chev" aria-hidden="true"></i>
  203. }
  204. </button>
  205. </li>
  206. <li>
  207. <button type="button" class="m-row" (click)="logoutOpen.set(true)">
  208. <span class="m-row-body">
  209. <span class="m-row-t1 m-danger">退出登录</span>
  210. </span>
  211. <i class="fa-solid fa-chevron-right m-chev" aria-hidden="true"></i>
  212. </button>
  213. </li>
  214. </ul>
  215. </section>
  216. <!-- 退出登录:独立成卡、居中红字(2026-11-23,微信/美团惯例) -->
  217. <section class="m-section">
  218. <button type="button" class="m-logout" (click)="logoutOpen.set(true)">退出登录</button>
  219. </section>
  220. </div>
  221. </div>
  222. <!-- 更新弹窗:有新版时自动弹出,也可由「关于与版本」手动唤起 -->
  223. <app-update-dialog />
  224. <app-m-drawer [activeSessionId]="activeSessionId()"
  225. [open]="drawerOpen()" (close)="drawerOpen.set(false)" (newChat)="newChat()" (newChatWithWs)="onNewChatIn($event)" />
  226. <!-- 确认弹窗换成业务组件:48px 按钮 + 危险红(2026-11-23) -->
  227. <app-m-confirm
  228. [(open)]="logoutOpen"
  229. title="退出登录"
  230. icon="fa-solid fa-arrow-right-from-bracket"
  231. [danger]="true"
  232. confirmText="退出登录"
  233. (confirm)="logout()"
  234. />
  235. `,
  236. styles: `
  237. :host {
  238. display: block;
  239. min-height: 100dvh;
  240. background: var(--canvas);
  241. }
  242. /* 固定外壳 + 内部滚动(2026-11-04):外层不能滚,只有内容区滚 */
  243. .m-page--scroll {
  244. display: flex;
  245. flex-direction: column;
  246. height: 100dvh;
  247. box-sizing: border-box;
  248. padding-bottom: calc(var(--mob-tabbar-h) + var(--mob-safe-bottom));
  249. }
  250. .m-body {
  251. flex: 1;
  252. min-height: 0;
  253. overflow-y: auto;
  254. -webkit-overflow-scrolling: touch;
  255. overscroll-behavior: contain;
  256. padding-bottom: var(--sp-6);
  257. }
  258. .m-section {
  259. padding: 0 var(--mob-gutter);
  260. }
  261. .m-section + .m-section {
  262. margin-top: var(--sp-6);
  263. }
  264. .m-section:first-of-type {
  265. margin-top: var(--sp-3);
  266. }
  267. .m-section-title {
  268. margin: 0 0 var(--sp-3);
  269. color: var(--muted);
  270. font-size: var(--fs-13);
  271. font-weight: 400;
  272. }
  273. .m-card {
  274. padding: var(--sp-4);
  275. border: 1px solid var(--edge);
  276. border-radius: var(--r-lg);
  277. background: var(--surface);
  278. box-shadow: none;
  279. }
  280. .m-profile {
  281. display: flex;
  282. align-items: center;
  283. gap: var(--sp-3);
  284. }
  285. .m-profile-body {
  286. min-width: 0;
  287. }
  288. .m-profile-name {
  289. color: var(--ink);
  290. font-size: var(--fs-17);
  291. font-weight: 600;
  292. }
  293. .m-profile-sub {
  294. overflow: hidden;
  295. text-overflow: ellipsis;
  296. white-space: nowrap;
  297. color: var(--muted);
  298. font-size: var(--fs-13);
  299. }
  300. .m-list {
  301. margin: 0;
  302. padding: 0;
  303. list-style: none;
  304. border: 1px solid var(--edge);
  305. border-radius: var(--r-lg);
  306. background: var(--surface);
  307. overflow: hidden;
  308. }
  309. .m-list > li + li {
  310. border-top: 1px solid var(--edge);
  311. }
  312. .m-row {
  313. display: flex;
  314. align-items: center;
  315. gap: var(--sp-3);
  316. width: 100%;
  317. min-height: 56px;
  318. padding: var(--sp-3) var(--sp-4);
  319. box-sizing: border-box;
  320. border: 0;
  321. background: var(--surface);
  322. color: var(--ink);
  323. font: inherit;
  324. text-align: left;
  325. cursor: pointer;
  326. }
  327. .m-row--plain {
  328. min-height: 56px;
  329. padding: var(--sp-3) 0;
  330. margin-top: var(--sp-2);
  331. border-top: 1px solid var(--edge);
  332. cursor: default;
  333. }
  334. .m-row-body {
  335. flex: 1;
  336. min-width: 0;
  337. }
  338. .m-row-t1 {
  339. display: block;
  340. font-size: var(--fs-15);
  341. }
  342. .m-row-t2 {
  343. display: block;
  344. overflow: hidden;
  345. text-overflow: ellipsis;
  346. white-space: nowrap;
  347. color: var(--muted);
  348. font-size: var(--fs-13);
  349. }
  350. .m-row-sub {
  351. display: block;
  352. margin-top: var(--sp-1);
  353. color: var(--muted);
  354. font-size: var(--fs-12);
  355. line-height: 1.5;
  356. overflow-wrap: anywhere;
  357. }
  358. .m-token-row {
  359. display: flex;
  360. align-items: center;
  361. gap: var(--sp-2);
  362. min-height: 56px;
  363. padding: var(--sp-3) var(--sp-4);
  364. background: var(--surface);
  365. }
  366. .m-token-value {
  367. overflow: hidden;
  368. text-overflow: ellipsis;
  369. white-space: nowrap;
  370. }
  371. .m-token-btn {
  372. display: grid;
  373. place-items: center;
  374. flex: none;
  375. width: var(--touch-min);
  376. height: var(--touch-min);
  377. border: 0;
  378. border-radius: var(--r-sm);
  379. background: none;
  380. color: var(--muted);
  381. font-size: var(--fs-13);
  382. cursor: pointer;
  383. }
  384. .m-token-btn:active {
  385. background: var(--surface-hover);
  386. }
  387. .m-token-btn:disabled {
  388. opacity: 0.35;
  389. cursor: not-allowed;
  390. }
  391. .m-logout {
  392. width: 100%;
  393. min-height: 52px;
  394. border: 1px solid var(--edge);
  395. border-radius: var(--r-lg);
  396. background: var(--surface);
  397. color: var(--danger);
  398. font: inherit;
  399. font-size: var(--fs-15);
  400. font-weight: 600;
  401. cursor: pointer;
  402. }
  403. .m-logout:active {
  404. background: var(--surface-hover);
  405. }
  406. /*
  407. * 有新版本时的红点(2026-11-23)
  408. * 取代 chevron 的位置 —— 有红点时无需再提示「可进入」,
  409. * 点进去就是要更新,语义更聚焦。
  410. */
  411. .m-update-dot {
  412. flex: none;
  413. width: 8px;
  414. height: 8px;
  415. border-radius: 50%;
  416. background: #EF4444;
  417. box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.16);
  418. }
  419. .m-chev {
  420. color: var(--muted);
  421. font-size: var(--fs-12);
  422. }
  423. .m-label {
  424. margin: 0 0 var(--sp-3);
  425. color: var(--ink-2);
  426. font-size: var(--fs-13);
  427. }
  428. .m-palette-grid {
  429. display: grid;
  430. grid-template-columns: repeat(4, 1fr);
  431. gap: var(--sp-2);
  432. }
  433. .m-palette {
  434. min-width: 0;
  435. display: flex;
  436. flex-direction: column;
  437. align-items: center;
  438. gap: var(--sp-1);
  439. padding: var(--sp-2) var(--sp-1);
  440. border: 1.5px solid var(--edge);
  441. border-radius: var(--r-md);
  442. background: var(--surface);
  443. color: var(--ink-2);
  444. font: inherit;
  445. font-size: var(--fs-11);
  446. white-space: nowrap;
  447. cursor: pointer;
  448. }
  449. .m-palette.on {
  450. border-color: var(--primary);
  451. color: var(--primary);
  452. font-weight: 600;
  453. }
  454. .m-swatch {
  455. display: flex;
  456. width: 100%;
  457. height: 32px;
  458. border: 1px solid var(--edge);
  459. border-radius: var(--r-sm);
  460. overflow: hidden;
  461. }
  462. .m-swatch i {
  463. flex: 1;
  464. }
  465. .m-swatch i:nth-child(1) {
  466. background: var(--nav-surface);
  467. }
  468. .m-swatch i:nth-child(2) {
  469. background: var(--primary);
  470. }
  471. .m-swatch i:nth-child(3) {
  472. background: var(--surface-2);
  473. }
  474. /*
  475. * 配色缩略图:各自作用域内展示该套配色的导航 / 主色 / 表面色。
  476. *
  477. * 🔴 这里不再重复写色值 —— _palette.scss 早已定义
  478. * [data-preview-palette='xxx'] { --nav-surface / --primary / --surface-2 },
  479. * 本组件的 [attr.data-preview-palette] 直接命中那套规则。
  480. * 原先复制过来的 12 个色值是冗余副本,一旦调色就会与真源不一致。 */
  481. .m-switch {
  482. position: relative;
  483. flex: none;
  484. width: 48px;
  485. height: var(--touch-min);
  486. border: 0;
  487. border-radius: var(--r-tag);
  488. background: transparent;
  489. cursor: pointer;
  490. transition: background var(--motion-fast) var(--ease-standard);
  491. }
  492. .m-switch::before {
  493. content: '';
  494. position: absolute;
  495. inset: 8px 0;
  496. border-radius: var(--r-tag);
  497. background: var(--surface-active);
  498. }
  499. .m-switch::after {
  500. content: '';
  501. position: absolute;
  502. top: 11px;
  503. left: 3px;
  504. width: 22px;
  505. height: 22px;
  506. border-radius: var(--r-tag);
  507. background: var(--surface);
  508. box-shadow: var(--shadow-soft);
  509. transition: transform var(--motion-fast) var(--ease-standard);
  510. }
  511. .m-switch[aria-checked='true']::before {
  512. background: var(--primary);
  513. }
  514. .m-switch[aria-checked='true']::after {
  515. transform: translateX(20px);
  516. background: var(--primary-content);
  517. }
  518. .m-font {
  519. margin-top: var(--sp-2);
  520. padding-top: var(--sp-3);
  521. border-top: 1px solid var(--edge);
  522. }
  523. .m-font-head {
  524. display: flex;
  525. align-items: baseline;
  526. justify-content: space-between;
  527. }
  528. .m-font-val {
  529. color: var(--muted);
  530. font-size: var(--fs-13);
  531. }
  532. .m-font-head--ui {
  533. margin-top: var(--sp-3);
  534. }
  535. /* 轻量预览:无背景框,直接用当前档位渲染一行样例 */
  536. .m-font-preview {
  537. margin: var(--sp-1) 0 0;
  538. color: var(--muted);
  539. line-height: 1.5;
  540. }
  541. .m-font-hint {
  542. margin: var(--sp-3) 0 0;
  543. color: var(--faint);
  544. font-size: var(--fs-12);
  545. line-height: 1.5;
  546. }
  547. /* 界面大小:三档分段(与正文滑块区分开,避免两套控制混在一起) */
  548. .m-ui-scale {
  549. display: grid;
  550. grid-template-columns: repeat(3, 1fr);
  551. gap: var(--sp-1);
  552. margin-top: var(--sp-2);
  553. padding: 3px;
  554. border-radius: var(--r-md);
  555. background: var(--surface-2);
  556. }
  557. .m-ui-scale button {
  558. min-height: var(--touch-min);
  559. border: 0;
  560. border-radius: var(--r-sm);
  561. background: transparent;
  562. color: var(--ink-2);
  563. font: inherit;
  564. font-size: var(--fs-14);
  565. cursor: pointer;
  566. }
  567. .m-ui-scale button.on {
  568. background: var(--surface);
  569. color: var(--primary);
  570. font-weight: 600;
  571. box-shadow: var(--shadow-soft);
  572. }
  573. .m-slider {
  574. width: 100%;
  575. margin-top: var(--sp-2);
  576. min-height: var(--touch-min);
  577. accent-color: var(--primary);
  578. }
  579. `
  580. })
  581. export class MbMinePage {
  582. private readonly chat = inject(ChatService);
  583. /** 当前打开的会话(抽屉高亮用) */
  584. protected readonly activeSessionId = computed(() => this.chat.sessionId());
  585. private readonly router = inject(Router);
  586. private readonly session = inject(SessionService);
  587. private readonly sessionsSvc = inject(ChatSessionService);
  588. protected readonly palette = inject(PaletteService);
  589. protected readonly theme = inject(ThemeService);
  590. protected readonly font = inject(ChatFontService);
  591. protected readonly drawerOpen = signal(false);
  592. protected readonly logoutOpen = signal(false);
  593. /**
  594. * 版本与更新(2026-11-23 新增)
  595. *
  596. * 服务是 root 级单例,和弹窗组件共享同一个实例 ——
  597. * 这里点了检查,弹窗那边立刻能感知到 hasUpdate 变化。
  598. */
  599. protected readonly update = inject(AppUpdateService);
  600. protected readonly updating = this.update.checking;
  601. /** 版本文案:本机版本 + 包名,方便反馈问题时一眼看出装的是哪个包 */
  602. protected versionText(): string {
  603. const v = this.update.local();
  604. if (!v) return '未获取到版本信息';
  605. return `v${v.versionName} (${v.versionCode})`;
  606. }
  607. /** 手动检查更新:查完有新版就由弹窗接管显示 */
  608. protected async checkUpdateNow(): Promise<void> {
  609. await this.update.check();
  610. if (!this.update.hasUpdate() && !this.update.error()) {
  611. /*
  612. * 已是最新 —— 给个轻提示。
  613. * 不用弹窗(太重),直接在行内文案上体现就够了,
  614. * 用户点完看到「已是最新」自然明白。
  615. */
  616. this.updateTip.set('已是最新版本');
  617. setTimeout(() => this.updateTip.set(null), 2000);
  618. }
  619. }
  620. /** 「已是最新」的临时提示 */
  621. protected readonly updateTip = signal<string | null>(null);
  622. protected readonly palettes = PALETTES;
  623. protected readonly fontMin = Math.min(...CHAT_FONT_SIZES);
  624. protected readonly fontMax = Math.max(...CHAT_FONT_SIZES);
  625. /** 界面大小三档(与正文大小分开设置) */
  626. protected readonly uiScaleOptions = UI_SCALES.map((value) => ({ value, label: UI_SCALE_LABEL[value] }));
  627. protected readonly uiScaleLabel = computed(() => UI_SCALE_LABEL[this.font.ui()]);
  628. protected readonly me = this.session.me;
  629. constructor() {
  630. // 字号偏好按用户存储,进入设置页时载入(幂等)
  631. this.font.load();
  632. this.loadNotificationState();
  633. }
  634. /** Token 只展示尾部 4 位 —— 完整令牌不应出现在屏幕上 */
  635. protected readonly tokenShown = signal(false);
  636. protected readonly copied = signal(false);
  637. /** 完整 Token(仅在用户主动点「显示」后渲染到 DOM) */
  638. protected readonly tokenText = computed(() => this.session.token() ?? '—');
  639. protected async copyToken(): Promise<void> {
  640. try {
  641. await navigator.clipboard.writeText(this.session.token() ?? '');
  642. this.copied.set(true);
  643. setTimeout(() => this.copied.set(false), 1500);
  644. } catch {
  645. /* 非安全上下文:静默失败,用户可手动选中复制 */
  646. }
  647. }
  648. protected readonly maskToken = computed(() => {
  649. const t = this.session.token();
  650. if (!t) return '—';
  651. const visible = t.slice(-4);
  652. return `${t.startsWith('r:') ? 'r:' : ''}${'•'.repeat(12)}${visible}`;
  653. });
  654. protected setPalette(id: PaletteId): void {
  655. this.palette.set(id || DEFAULT_PALETTE);
  656. }
  657. protected toggleDark(): void {
  658. this.theme.toggleMode();
  659. }
  660. // ---------- 通知权限(2026-11-15) ----------
  661. private readonly notify = inject(AppNotificationService);
  662. private readonly notificationSetup = inject(NotificationSetupService);
  663. /** 通知权限状态:null = 未知/查询中,true = 已开,false = 未开 */
  664. /**
  665. * 通知权限状态:直接订阅 service 的 signal(2026-11-15)。
  666. * 原来的本地 signal 只在构造函数查一次,从系统设置返回后不会更新。
  667. */
  668. protected readonly notifyGranted = computed(() => this.notify.grantedState());
  669. /** 是否原生壳(浏览器里不显示「通知」入口 —— 浏览器没有这个系统权限) */
  670. protected readonly isNative = signal(isNativeApp());
  671. /** 进页面时查一次通知权限状态(构造函数里调用,不另开第二个 constructor) */
  672. private loadNotificationState(): void {
  673. /* 进页面时强制刷新一次:用户可能刚从系统设置回来 */
  674. void this.notify.refreshGranted();
  675. }
  676. protected notifyStateText(): string {
  677. const g = this.notifyGranted();
  678. if (g === null) return '检查中…';
  679. if (!g) return '未开启 · 点此设置';
  680. // 已开通知时进一步告知还有几项后台设置没配好,用户不用点进去才知道
  681. const left = this.notificationSetup.remaining();
  682. return left > 0 ? `系统通知已开启 · ${left} 项后台设置待处理` : '系统通知已开启 · 全部就绪';
  683. }
  684. /**
  685. * 点「通知」行:
  686. * · 已开 → 去系统设置(可关闭)
  687. * · 未开 → 先申请;被系统拒绝(不再弹窗)则引导去设置页
  688. */
  689. protected async onNotificationRow(): Promise<void> {
  690. this.notificationSetup.show();
  691. }
  692. /** 字号滑块:吸附到最近的档位(与桌面设置同一组档位) */
  693. protected onFont(e: Event): void {
  694. const v = Number((e.target as HTMLInputElement).value);
  695. const nearest = CHAT_FONT_SIZES.reduce((a, b) => (Math.abs(b - v) < Math.abs(a - v) ? b : a));
  696. this.font.set(nearest);
  697. }
  698. protected logout(): void {
  699. this.logoutOpen.set(false);
  700. this.session.logout();
  701. this.sessionsSvc.reset();
  702. void this.router.navigateByUrl('/login', { replaceUrl: true });
  703. }
  704. protected newChat(): void {
  705. this.drawerOpen.set(false);
  706. void this.router.navigateByUrl('/mobile/messages');
  707. }
  708. /**
  709. * 在指定工作区中新建对话。
  710. *
  711. * 这几页没有新建对话弹层,统一跳到会话列表页并带上工作区参数,
  712. * 由那里打开选择容器的弹层(避免每个页面各做一套)。
  713. */
  714. protected onNewChatIn(workspaceId: string): void {
  715. void this.router.navigateByUrl(`/mobile/messages?newIn=${encodeURIComponent(workspaceId)}`);
  716. }
  717. }