| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import { Component, ChangeDetectionStrategy, inject, computed, signal, OnInit } from '@angular/core';
- import { CommonModule } from '@angular/common';
- import { AppUpdateService } from '../../../core/app-update.service';
- import { AppInstallService } from '../../../core/app-install.service';
- /**
- * 更新提示弹窗(2026-11-23 新建)
- *
- * 三种形态:
- * 1. **强制更新**:遮罩不可点、无关闭按钮,只有一个「立即更新」
- * 2. **普通更新**:「稍后再说」+「立即更新」
- * 3. **下载中**:进度条 + 字节数,可后台继续(关掉弹窗不影响下载)
- *
- * 下载走 AppInstallService:App 内直接下、挂通知、下完拉安装器,
- * 不跳浏览器。这是用户明确要求的体验。
- */
- @Component({
- selector: 'app-update-dialog',
- standalone: true,
- imports: [CommonModule],
- changeDetection: ChangeDetectionStrategy.OnPush,
- template: `
- @if (visible() && info(); as u) {
- <div class="m-update-mask" [class.is-force]="u.isForce" (click)="onMaskClick(u.isForce)">
- <div class="m-update-card" role="dialog" aria-modal="true" aria-labelledby="upd-title">
- @if (install.state().status === 'downloading') {
- <!-- ───────── 下载中 ───────── -->
- <div class="m-update-head">
- <h3 id="upd-title">正在下载更新</h3>
- <p class="m-update-ver">{{ u.version }}</p>
- </div>
- <div class="m-progress-wrap">
- <div class="m-progress-bar">
- <div class="m-progress-fill"
- [style.width.%]="install.state().progress >= 0 ? install.state().progress : 0">
- </div>
- </div>
- <div class="m-progress-meta">
- <span>{{ progressText() }}</span>
- <span>{{ percentText() }}</span>
- </div>
- </div>
- <p class="m-update-hint">
- 下载会在后台继续,可以先把 App 放到一边。下载完成后会自动弹出安装。
- </p>
- @if (!u.isForce) {
- <button type="button" class="m-btn m-btn-ghost" (click)="close()">
- 后台继续,先不看
- </button>
- }
- } @else {
- <!-- ───────── 更新说明 ───────── -->
- <div class="m-update-head">
- <div class="m-update-badge" [class.force]="u.isForce">
- {{ u.isForce ? '必须更新' : '有新版本' }}
- </div>
- <h3 id="upd-title">飞马SI {{ u.version }}</h3>
- <p class="m-update-ver">
- 当前版本 {{ localVersion() }}
- @if (u.size) { · {{ sizeText(u.size) }} }
- </p>
- </div>
- @if (u.content) {
- <div class="m-update-log">
- @for (line of contentLines(u.content); track $index) {
- <p>{{ line }}</p>
- }
- </div>
- }
- @if (install.state().status === 'error') {
- <p class="m-update-err">{{ install.state().message }}</p>
- }
- <div class="m-update-actions">
- <button type="button" class="m-btn m-btn-primary"
- (click)="startUpdate(u.url, u.version)">
- 立即更新
- </button>
- @if (!u.isForce) {
- <button type="button" class="m-btn m-btn-ghost" (click)="close()">
- 稍后再说
- </button>
- }
- </div>
- }
- </div>
- </div>
- }
- `,
- styles: [`
- .m-update-mask {
- position: fixed;
- inset: 0;
- z-index: 9999;
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 24px;
- background: rgba(8, 13, 24, 0.72);
- backdrop-filter: blur(6px);
- animation: fadeIn 0.2s ease;
- }
- .m-update-mask.is-force {
- /* 强制更新时遮罩更实,视觉上明确「这个不能跳过」 */
- background: rgba(8, 13, 24, 0.9);
- }
- @keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
- .m-update-card {
- width: 100%;
- max-width: 380px;
- background: #fff;
- border-radius: 20px;
- padding: 26px 24px 22px;
- box-shadow: 0 24px 64px rgba(0, 0, 0, 0.4);
- animation: slideUp 0.26s cubic-bezier(0.16, 1, 0.3, 1);
- /* 内容多时可滚动,避免小屏溢出 */
- max-height: 82vh;
- overflow-y: auto;
- }
- @keyframes slideUp {
- from { opacity: 0; transform: translateY(16px) scale(0.98) }
- to { opacity: 1; transform: translateY(0) scale(1) }
- }
- .m-update-head { margin-bottom: 18px; }
- .m-update-badge {
- display: inline-block;
- padding: 4px 11px;
- border-radius: 999px;
- font-size: 12px;
- font-weight: 600;
- background: #EFF6FF;
- color: #2563EB;
- margin-bottom: 12px;
- }
- .m-update-badge.force {
- background: #FEF2F2;
- color: #DC2626;
- }
- .m-update-head h3 {
- font-size: 20px;
- font-weight: 700;
- color: #0F172A;
- margin: 0 0 6px;
- letter-spacing: -0.01em;
- }
- .m-update-ver {
- font-size: 13px;
- color: #64748B;
- margin: 0;
- }
- .m-update-log {
- background: #F8FAFC;
- border-radius: 12px;
- padding: 14px 16px;
- margin-bottom: 20px;
- max-height: 220px;
- overflow-y: auto;
- }
- .m-update-log p {
- font-size: 13.5px;
- line-height: 1.7;
- color: #334155;
- margin: 0 0 6px;
- }
- .m-update-log p:last-child { margin-bottom: 0; }
- .m-progress-wrap { margin-bottom: 18px; }
- .m-progress-bar {
- height: 8px;
- border-radius: 999px;
- background: #E2E8F0;
- overflow: hidden;
- margin-bottom: 8px;
- }
- .m-progress-fill {
- height: 100%;
- border-radius: 999px;
- background: linear-gradient(90deg, #6D28D9, #06B6D4);
- transition: width 0.3s ease;
- }
- .m-progress-meta {
- display: flex;
- justify-content: space-between;
- font-size: 12.5px;
- color: #64748B;
- font-variant-numeric: tabular-nums;
- }
- .m-update-hint {
- font-size: 12.5px;
- line-height: 1.7;
- color: #64748B;
- margin: 0 0 18px;
- }
- .m-update-err {
- font-size: 13px;
- color: #DC2626;
- margin: 0 0 14px;
- }
- .m-update-actions {
- display: flex;
- flex-direction: column;
- gap: 9px;
- }
- .m-btn {
- width: 100%;
- padding: 14px;
- border-radius: 13px;
- border: none;
- font-size: 15.5px;
- font-weight: 600;
- cursor: pointer;
- transition: transform 0.12s;
- }
- .m-btn:active { transform: scale(0.98); }
- .m-btn-primary {
- background: linear-gradient(135deg, #6D28D9, #06B6D4);
- color: #fff;
- box-shadow: 0 8px 24px rgba(109, 40, 217, 0.32);
- }
- .m-btn-ghost {
- background: transparent;
- color: #64748B;
- font-weight: 500;
- }
- `],
- })
- export class UpdateDialogComponent implements OnInit {
- private readonly update = inject(AppUpdateService);
- readonly install = inject(AppInstallService);
- /** 用户主动关掉后不再自动弹(非强制更新时) */
- private readonly dismissed = signal(false);
- readonly visible = computed(() => {
- if (this.dismissed()) return false;
- const s = this.install.state().status;
- /* 下载中或安装中必须继续显示,即使 hasUpdate 已经被清 */
- if (s === 'downloading' || s === 'installing' || s === 'error') return true;
- return this.update.hasUpdate();
- });
- readonly info = computed(() => this.update.remote());
- readonly localVersion = computed(() => this.update.local()?.versionName ?? '');
- async ngOnInit(): Promise<void> {
- /*
- * 组件挂载即检查更新。
- * 放在这里而不是 App 启动钩子,是因为弹窗组件本身就代表「该检查了」,
- * 而且懒加载 —— 用户没进到含弹窗的页面就不会发请求。
- */
- await this.update.check();
- }
- /** 下载中显示:已下载 / 总量 */
- progressText(): string {
- const s = this.install.state();
- if (s.total <= 0) return `已下载 ${this.sizeText(s.bytes)}`;
- return `${this.sizeText(s.bytes)} / ${this.sizeText(s.total)}`;
- }
- percentText(): string {
- const p = this.install.state().progress;
- return p >= 0 ? `${p}%` : '计算中…';
- }
- sizeText(bytes: number): string {
- if (!bytes || bytes <= 0) return '0 MB';
- const mb = bytes / 1024 / 1024;
- return mb >= 1 ? `${mb.toFixed(1)} MB` : `${(bytes / 1024).toFixed(0)} KB`;
- }
- /** 更新说明按行拆开渲染,保留原有的空行分段 */
- contentLines(content: string): string[] {
- return content
- .split(/\r?\n/)
- .map((l) => l.trim())
- .filter((l) => l.length > 0);
- }
- async startUpdate(url: string, version: string): Promise<void> {
- await this.install.downloadAndInstall(url, version);
- }
- close(): void {
- this.dismissed.set(true);
- /*
- * 只关弹窗,**不中断下载** —— 下载在系统 DownloadManager 里,
- * App 关了也会继续。用户下次回来看「我的」页还能看到进度。
- */
- if (this.install.state().status === 'idle') {
- this.install.reset();
- }
- }
- /** 点遮罩关闭。强制更新时点遮罩无效 */
- onMaskClick(isForce: boolean): void {
- if (isForce) return;
- this.close();
- }
- }
|