import { Injectable, signal } from '@angular/core'; /** * 全局异常提示(业务层,2026-11-10)。 * * ## 为什么不用底座 toast * * `src/app/ui/**` 是 ui-kit 官方底座,**项目规则禁止修改**。 * 而底座的 `UiMToastService` 是「固定自动消失」,无法表达 * 「用户必须手动关闭」这个语义。所以这里在**业务层**另起一个轻量服务, * 只承载「需要人工确认的异常」,与普通 toast 分工不同: * * | 类型 | 用什么 | 行为 | * |---|---|---| * | 普通轻提示(已保存/已复制) | 底座 `UiMToastService` | 2 秒自动消失 | * | **异常提示(请求超时等)** | 本服务 | **不自动消失,必须点关闭** | * * ## 为什么必须手动关闭 * * 用户裁决原话:「应该给出全局提示说请求异常,并且这个全局提示需要手动关闭 * 才会关闭,不会自动隐藏。」——容器超时意味着这条消息**根本没被处理**, * 一闪而过的提示会让用户以为已经正常了。 */ export interface AppAlert { id: number; /** 标题,一句话说清发生了什么 */ title: string; /** 补充说明(可选),告诉用户能做什么 */ detail?: string; /** 级别:异常用 error,提醒用 warning */ tone: 'error' | 'warning'; /** * 是否由「当前状态」驱动(2026-11-10)。 * * 状态型提示(如「容器未响应」):条件成立时存在,条件消失后自动撤掉。 * 用户手动关闭后**不会马上重弹**,要等条件先消失再出现才会重新提示 —— * 否则用户会觉得「关不掉」。 */ stateDriven?: boolean; /** * 提示条上的**操作按钮**(2026-11-11)。 * * 用途:容器卡住时不能让用户只看到一句「没响应」就完事 —— * 给出「重新发送」,把刚才那条消息放回输入框,用户点一下就能重发。 * * 只放**低风险、可撤销**的动作(回填草稿),不放会直接产生副作用的 * (比如「立即重发」会重复扣费)。 */ action?: { label: string }; } @Injectable({ providedIn: 'root' }) export class AlertService { readonly alerts = signal([]); private nextId = 1; /** * 用户关掉过的状态型标题**本轮**不再弹(2026-11-10)。 * * 🔴 用户裁决:超时提示**每次超时都要弹**。 * 但同一轮超时里,用户点「我知道了」之后不能每秒又冒出来 —— * 所以记一下,等「本轮超时结束」(条件变 false)时清除, * 下一次发生新的超时会**重新弹出**。 */ private readonly _dismissedThisRound = new Set(); /** * 弹出一条**不会自动消失**的提示。 * 同一 message 已存在时不重复弹(避免每秒 tick 刷屏)。 */ show(title: string, detail?: string, tone: 'error' | 'warning' = 'error'): void { if (this.alerts().some((a) => a.title === title)) { // 已存在:只更新说明(计数可能变化),不新增 this.alerts.update((list) => list.map((a) => (a.title === title && a.detail !== detail ? { ...a, detail } : a)), ); return; } this.alerts.update((list) => [...list, { id: this.nextId++, title, detail, tone }]); } /** * 状态型提示(2026-11-10):条件成立就确保在,条件消失就撤掉。 * * @param active 当前条件是否成立 * @param action 可选的**操作按钮**(2026-11-11)。注册时带一次即可; * 动作的执行由宿主通过 `onAction(title)` 订阅后自行分发。 */ syncState( title: string, active: boolean, detail?: string, action?: { label: string }, ): void { if (active) { /* * 用户刚关掉 → 本轮不再弹(否则每秒都会重新出现,关不掉)。 * 但「本轮」只指这次超时:条件一旦消失就清空,下次超时会重新提示。 */ if (this._dismissedThisRound.has(title)) return; const exists = this.alerts().some((a) => a.title === title); if (exists) { this.alerts.update((list) => list.map((a) => a.title === title && (a.detail !== detail || a.action?.label !== action?.label) ? { ...a, detail, ...(action ? { action } : {}) } : a, ), ); } else { this.alerts.update((list) => [ ...list, { id: this.nextId++, title, detail, tone: 'error', stateDriven: true, ...(action ? { action } : {}), }, ]); } return; } // 条件已消失:撤掉提示,并解除本轮锁定 —— 下次超时会重新提示 this._dismissedThisRound.delete(title); this.alerts.update((list) => list.filter((a) => a.title !== title)); } /** * 执行提示条上的操作按钮(2026-11-11)。 * * 实际动作由**宿主页面**注册(`onAction(handler)`),因为只有页面 * 知道「重新发送」该填哪条草稿。服务本身保持无业务依赖。 */ private actionHandler: ((alert: AppAlert) => void) | null = null; onAction(handler: (alert: AppAlert) => void): void { this.actionHandler = handler; } /** 由提示条组件在用户点按钮时调用 */ runAction(alert: AppAlert): void { if (!alert.action) return; this.actionHandler?.(alert); /* 点完就撤 —— 留着会让人以为还没执行 */ this.dismiss(alert.id); } /** 用户点关闭 */ dismiss(id: number): void { const hit = this.alerts().find((a) => a.id === id); if (hit?.stateDriven) this._dismissedThisRound.add(hit.title); this.alerts.update((list) => list.filter((a) => a.id !== id)); } /** 清空全部 */ clear(): void { this.alerts.set([]); this._dismissedThisRound.clear(); } /** * 解除「用户已关闭」的锁(2026-11-10)。 * * 用于「新的超时发生」时:用户上一次关掉了提示,但新的问题又出现了, * 必须重新提示 —— 用户裁决「每次超时都要弹」。 */ resetDismiss(title: string): void { this._dismissedThisRound.delete(title); } }