m-update-dialog.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { Component, ChangeDetectionStrategy, inject, computed, signal, OnInit } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { AppUpdateService } from '../../../core/app-update.service';
  4. import { AppInstallService } from '../../../core/app-install.service';
  5. /**
  6. * 更新提示弹窗(2026-11-23 新建)
  7. *
  8. * 三种形态:
  9. * 1. **强制更新**:遮罩不可点、无关闭按钮,只有一个「立即更新」
  10. * 2. **普通更新**:「稍后再说」+「立即更新」
  11. * 3. **下载中**:进度条 + 字节数,可后台继续(关掉弹窗不影响下载)
  12. *
  13. * 下载走 AppInstallService:App 内直接下、挂通知、下完拉安装器,
  14. * 不跳浏览器。这是用户明确要求的体验。
  15. */
  16. @Component({
  17. selector: 'app-update-dialog',
  18. standalone: true,
  19. imports: [CommonModule],
  20. changeDetection: ChangeDetectionStrategy.OnPush,
  21. template: `
  22. @if (visible() && info(); as u) {
  23. <div class="m-update-mask" [class.is-force]="u.isForce" (click)="onMaskClick(u.isForce)">
  24. <div class="m-update-card" role="dialog" aria-modal="true" aria-labelledby="upd-title">
  25. @if (install.state().status === 'downloading') {
  26. <!-- ───────── 下载中 ───────── -->
  27. <div class="m-update-head">
  28. <h3 id="upd-title">正在下载更新</h3>
  29. <p class="m-update-ver">{{ u.version }}</p>
  30. </div>
  31. <div class="m-progress-wrap">
  32. <div class="m-progress-bar">
  33. <div class="m-progress-fill"
  34. [style.width.%]="install.state().progress >= 0 ? install.state().progress : 0">
  35. </div>
  36. </div>
  37. <div class="m-progress-meta">
  38. <span>{{ progressText() }}</span>
  39. <span>{{ percentText() }}</span>
  40. </div>
  41. </div>
  42. <p class="m-update-hint">
  43. 下载会在后台继续,可以先把 App 放到一边。下载完成后会自动弹出安装。
  44. </p>
  45. @if (!u.isForce) {
  46. <button type="button" class="m-btn m-btn-ghost" (click)="close()">
  47. 后台继续,先不看
  48. </button>
  49. }
  50. } @else {
  51. <!-- ───────── 更新说明 ───────── -->
  52. <div class="m-update-head">
  53. <div class="m-update-badge" [class.force]="u.isForce">
  54. {{ u.isForce ? '必须更新' : '有新版本' }}
  55. </div>
  56. <h3 id="upd-title">飞马SI {{ u.version }}</h3>
  57. <p class="m-update-ver">
  58. 当前版本 {{ localVersion() }}
  59. @if (u.size) { · {{ sizeText(u.size) }} }
  60. </p>
  61. </div>
  62. @if (u.content) {
  63. <div class="m-update-log">
  64. @for (line of contentLines(u.content); track $index) {
  65. <p>{{ line }}</p>
  66. }
  67. </div>
  68. }
  69. @if (install.state().status === 'error') {
  70. <p class="m-update-err">{{ install.state().message }}</p>
  71. }
  72. <div class="m-update-actions">
  73. <button type="button" class="m-btn m-btn-primary"
  74. (click)="startUpdate(u.url, u.version)">
  75. 立即更新
  76. </button>
  77. @if (!u.isForce) {
  78. <button type="button" class="m-btn m-btn-ghost" (click)="close()">
  79. 稍后再说
  80. </button>
  81. }
  82. </div>
  83. }
  84. </div>
  85. </div>
  86. }
  87. `,
  88. styles: [`
  89. .m-update-mask {
  90. position: fixed;
  91. inset: 0;
  92. z-index: 9999;
  93. display: flex;
  94. align-items: center;
  95. justify-content: center;
  96. padding: 24px;
  97. background: rgba(8, 13, 24, 0.72);
  98. backdrop-filter: blur(6px);
  99. animation: fadeIn 0.2s ease;
  100. }
  101. .m-update-mask.is-force {
  102. /* 强制更新时遮罩更实,视觉上明确「这个不能跳过」 */
  103. background: rgba(8, 13, 24, 0.9);
  104. }
  105. @keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
  106. .m-update-card {
  107. width: 100%;
  108. max-width: 380px;
  109. background: #fff;
  110. border-radius: 20px;
  111. padding: 26px 24px 22px;
  112. box-shadow: 0 24px 64px rgba(0, 0, 0, 0.4);
  113. animation: slideUp 0.26s cubic-bezier(0.16, 1, 0.3, 1);
  114. /* 内容多时可滚动,避免小屏溢出 */
  115. max-height: 82vh;
  116. overflow-y: auto;
  117. }
  118. @keyframes slideUp {
  119. from { opacity: 0; transform: translateY(16px) scale(0.98) }
  120. to { opacity: 1; transform: translateY(0) scale(1) }
  121. }
  122. .m-update-head { margin-bottom: 18px; }
  123. .m-update-badge {
  124. display: inline-block;
  125. padding: 4px 11px;
  126. border-radius: 999px;
  127. font-size: 12px;
  128. font-weight: 600;
  129. background: #EFF6FF;
  130. color: #2563EB;
  131. margin-bottom: 12px;
  132. }
  133. .m-update-badge.force {
  134. background: #FEF2F2;
  135. color: #DC2626;
  136. }
  137. .m-update-head h3 {
  138. font-size: 20px;
  139. font-weight: 700;
  140. color: #0F172A;
  141. margin: 0 0 6px;
  142. letter-spacing: -0.01em;
  143. }
  144. .m-update-ver {
  145. font-size: 13px;
  146. color: #64748B;
  147. margin: 0;
  148. }
  149. .m-update-log {
  150. background: #F8FAFC;
  151. border-radius: 12px;
  152. padding: 14px 16px;
  153. margin-bottom: 20px;
  154. max-height: 220px;
  155. overflow-y: auto;
  156. }
  157. .m-update-log p {
  158. font-size: 13.5px;
  159. line-height: 1.7;
  160. color: #334155;
  161. margin: 0 0 6px;
  162. }
  163. .m-update-log p:last-child { margin-bottom: 0; }
  164. .m-progress-wrap { margin-bottom: 18px; }
  165. .m-progress-bar {
  166. height: 8px;
  167. border-radius: 999px;
  168. background: #E2E8F0;
  169. overflow: hidden;
  170. margin-bottom: 8px;
  171. }
  172. .m-progress-fill {
  173. height: 100%;
  174. border-radius: 999px;
  175. background: linear-gradient(90deg, #6D28D9, #06B6D4);
  176. transition: width 0.3s ease;
  177. }
  178. .m-progress-meta {
  179. display: flex;
  180. justify-content: space-between;
  181. font-size: 12.5px;
  182. color: #64748B;
  183. font-variant-numeric: tabular-nums;
  184. }
  185. .m-update-hint {
  186. font-size: 12.5px;
  187. line-height: 1.7;
  188. color: #64748B;
  189. margin: 0 0 18px;
  190. }
  191. .m-update-err {
  192. font-size: 13px;
  193. color: #DC2626;
  194. margin: 0 0 14px;
  195. }
  196. .m-update-actions {
  197. display: flex;
  198. flex-direction: column;
  199. gap: 9px;
  200. }
  201. .m-btn {
  202. width: 100%;
  203. padding: 14px;
  204. border-radius: 13px;
  205. border: none;
  206. font-size: 15.5px;
  207. font-weight: 600;
  208. cursor: pointer;
  209. transition: transform 0.12s;
  210. }
  211. .m-btn:active { transform: scale(0.98); }
  212. .m-btn-primary {
  213. background: linear-gradient(135deg, #6D28D9, #06B6D4);
  214. color: #fff;
  215. box-shadow: 0 8px 24px rgba(109, 40, 217, 0.32);
  216. }
  217. .m-btn-ghost {
  218. background: transparent;
  219. color: #64748B;
  220. font-weight: 500;
  221. }
  222. `],
  223. })
  224. export class UpdateDialogComponent implements OnInit {
  225. private readonly update = inject(AppUpdateService);
  226. readonly install = inject(AppInstallService);
  227. /** 用户主动关掉后不再自动弹(非强制更新时) */
  228. private readonly dismissed = signal(false);
  229. readonly visible = computed(() => {
  230. if (this.dismissed()) return false;
  231. const s = this.install.state().status;
  232. /* 下载中或安装中必须继续显示,即使 hasUpdate 已经被清 */
  233. if (s === 'downloading' || s === 'installing' || s === 'error') return true;
  234. return this.update.hasUpdate();
  235. });
  236. readonly info = computed(() => this.update.remote());
  237. readonly localVersion = computed(() => this.update.local()?.versionName ?? '');
  238. async ngOnInit(): Promise<void> {
  239. /*
  240. * 组件挂载即检查更新。
  241. * 放在这里而不是 App 启动钩子,是因为弹窗组件本身就代表「该检查了」,
  242. * 而且懒加载 —— 用户没进到含弹窗的页面就不会发请求。
  243. */
  244. await this.update.check();
  245. }
  246. /** 下载中显示:已下载 / 总量 */
  247. progressText(): string {
  248. const s = this.install.state();
  249. if (s.total <= 0) return `已下载 ${this.sizeText(s.bytes)}`;
  250. return `${this.sizeText(s.bytes)} / ${this.sizeText(s.total)}`;
  251. }
  252. percentText(): string {
  253. const p = this.install.state().progress;
  254. return p >= 0 ? `${p}%` : '计算中…';
  255. }
  256. sizeText(bytes: number): string {
  257. if (!bytes || bytes <= 0) return '0 MB';
  258. const mb = bytes / 1024 / 1024;
  259. return mb >= 1 ? `${mb.toFixed(1)} MB` : `${(bytes / 1024).toFixed(0)} KB`;
  260. }
  261. /** 更新说明按行拆开渲染,保留原有的空行分段 */
  262. contentLines(content: string): string[] {
  263. return content
  264. .split(/\r?\n/)
  265. .map((l) => l.trim())
  266. .filter((l) => l.length > 0);
  267. }
  268. async startUpdate(url: string, version: string): Promise<void> {
  269. await this.install.downloadAndInstall(url, version);
  270. }
  271. close(): void {
  272. this.dismissed.set(true);
  273. /*
  274. * 只关弹窗,**不中断下载** —— 下载在系统 DownloadManager 里,
  275. * App 关了也会继续。用户下次回来看「我的」页还能看到进度。
  276. */
  277. if (this.install.state().status === 'idle') {
  278. this.install.reset();
  279. }
  280. }
  281. /** 点遮罩关闭。强制更新时点遮罩无效 */
  282. onMaskClick(isForce: boolean): void {
  283. if (isForce) return;
  284. this.close();
  285. }
  286. }