alert.service.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { Injectable, signal } from '@angular/core';
  2. /**
  3. * 全局异常提示(业务层,2026-11-10)。
  4. *
  5. * ## 为什么不用底座 toast
  6. *
  7. * `src/app/ui/**` 是 ui-kit 官方底座,**项目规则禁止修改**。
  8. * 而底座的 `UiMToastService` 是「固定自动消失」,无法表达
  9. * 「用户必须手动关闭」这个语义。所以这里在**业务层**另起一个轻量服务,
  10. * 只承载「需要人工确认的异常」,与普通 toast 分工不同:
  11. *
  12. * | 类型 | 用什么 | 行为 |
  13. * |---|---|---|
  14. * | 普通轻提示(已保存/已复制) | 底座 `UiMToastService` | 2 秒自动消失 |
  15. * | **异常提示(请求超时等)** | 本服务 | **不自动消失,必须点关闭** |
  16. *
  17. * ## 为什么必须手动关闭
  18. *
  19. * 用户裁决原话:「应该给出全局提示说请求异常,并且这个全局提示需要手动关闭
  20. * 才会关闭,不会自动隐藏。」——容器超时意味着这条消息**根本没被处理**,
  21. * 一闪而过的提示会让用户以为已经正常了。
  22. */
  23. export interface AppAlert {
  24. id: number;
  25. /** 标题,一句话说清发生了什么 */
  26. title: string;
  27. /** 补充说明(可选),告诉用户能做什么 */
  28. detail?: string;
  29. /** 级别:异常用 error,提醒用 warning */
  30. tone: 'error' | 'warning';
  31. /**
  32. * 是否由「当前状态」驱动(2026-11-10)。
  33. *
  34. * 状态型提示(如「容器未响应」):条件成立时存在,条件消失后自动撤掉。
  35. * 用户手动关闭后**不会马上重弹**,要等条件先消失再出现才会重新提示 ——
  36. * 否则用户会觉得「关不掉」。
  37. */
  38. stateDriven?: boolean;
  39. /**
  40. * 提示条上的**操作按钮**(2026-11-11)。
  41. *
  42. * 用途:容器卡住时不能让用户只看到一句「没响应」就完事 ——
  43. * 给出「重新发送」,把刚才那条消息放回输入框,用户点一下就能重发。
  44. *
  45. * 只放**低风险、可撤销**的动作(回填草稿),不放会直接产生副作用的
  46. * (比如「立即重发」会重复扣费)。
  47. */
  48. action?: { label: string };
  49. }
  50. @Injectable({ providedIn: 'root' })
  51. export class AlertService {
  52. readonly alerts = signal<AppAlert[]>([]);
  53. private nextId = 1;
  54. /**
  55. * 用户关掉过的状态型标题**本轮**不再弹(2026-11-10)。
  56. *
  57. * 🔴 用户裁决:超时提示**每次超时都要弹**。
  58. * 但同一轮超时里,用户点「我知道了」之后不能每秒又冒出来 ——
  59. * 所以记一下,等「本轮超时结束」(条件变 false)时清除,
  60. * 下一次发生新的超时会**重新弹出**。
  61. */
  62. private readonly _dismissedThisRound = new Set<string>();
  63. /**
  64. * 弹出一条**不会自动消失**的提示。
  65. * 同一 message 已存在时不重复弹(避免每秒 tick 刷屏)。
  66. */
  67. show(title: string, detail?: string, tone: 'error' | 'warning' = 'error'): void {
  68. if (this.alerts().some((a) => a.title === title)) {
  69. // 已存在:只更新说明(计数可能变化),不新增
  70. this.alerts.update((list) =>
  71. list.map((a) => (a.title === title && a.detail !== detail ? { ...a, detail } : a)),
  72. );
  73. return;
  74. }
  75. this.alerts.update((list) => [...list, { id: this.nextId++, title, detail, tone }]);
  76. }
  77. /**
  78. * 状态型提示(2026-11-10):条件成立就确保在,条件消失就撤掉。
  79. *
  80. * @param active 当前条件是否成立
  81. * @param action 可选的**操作按钮**(2026-11-11)。注册时带一次即可;
  82. * 动作的执行由宿主通过 `onAction(title)` 订阅后自行分发。
  83. */
  84. syncState(
  85. title: string,
  86. active: boolean,
  87. detail?: string,
  88. action?: { label: string },
  89. ): void {
  90. if (active) {
  91. /*
  92. * 用户刚关掉 → 本轮不再弹(否则每秒都会重新出现,关不掉)。
  93. * 但「本轮」只指这次超时:条件一旦消失就清空,下次超时会重新提示。
  94. */
  95. if (this._dismissedThisRound.has(title)) return;
  96. const exists = this.alerts().some((a) => a.title === title);
  97. if (exists) {
  98. this.alerts.update((list) =>
  99. list.map((a) =>
  100. a.title === title && (a.detail !== detail || a.action?.label !== action?.label)
  101. ? { ...a, detail, ...(action ? { action } : {}) }
  102. : a,
  103. ),
  104. );
  105. } else {
  106. this.alerts.update((list) => [
  107. ...list,
  108. {
  109. id: this.nextId++,
  110. title,
  111. detail,
  112. tone: 'error',
  113. stateDriven: true,
  114. ...(action ? { action } : {}),
  115. },
  116. ]);
  117. }
  118. return;
  119. }
  120. // 条件已消失:撤掉提示,并解除本轮锁定 —— 下次超时会重新提示
  121. this._dismissedThisRound.delete(title);
  122. this.alerts.update((list) => list.filter((a) => a.title !== title));
  123. }
  124. /**
  125. * 执行提示条上的操作按钮(2026-11-11)。
  126. *
  127. * 实际动作由**宿主页面**注册(`onAction(handler)`),因为只有页面
  128. * 知道「重新发送」该填哪条草稿。服务本身保持无业务依赖。
  129. */
  130. private actionHandler: ((alert: AppAlert) => void) | null = null;
  131. onAction(handler: (alert: AppAlert) => void): void {
  132. this.actionHandler = handler;
  133. }
  134. /** 由提示条组件在用户点按钮时调用 */
  135. runAction(alert: AppAlert): void {
  136. if (!alert.action) return;
  137. this.actionHandler?.(alert);
  138. /* 点完就撤 —— 留着会让人以为还没执行 */
  139. this.dismiss(alert.id);
  140. }
  141. /** 用户点关闭 */
  142. dismiss(id: number): void {
  143. const hit = this.alerts().find((a) => a.id === id);
  144. if (hit?.stateDriven) this._dismissedThisRound.add(hit.title);
  145. this.alerts.update((list) => list.filter((a) => a.id !== id));
  146. }
  147. /** 清空全部 */
  148. clear(): void {
  149. this.alerts.set([]);
  150. this._dismissedThisRound.clear();
  151. }
  152. /**
  153. * 解除「用户已关闭」的锁(2026-11-10)。
  154. *
  155. * 用于「新的超时发生」时:用户上一次关掉了提示,但新的问题又出现了,
  156. * 必须重新提示 —— 用户裁决「每次超时都要弹」。
  157. */
  158. resetDismiss(title: string): void {
  159. this._dismissedThisRound.delete(title);
  160. }
  161. }