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) {
} `, 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 { /* * 组件挂载即检查更新。 * 放在这里而不是 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 { 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(); } }