Преглед на файлове

fix(web): 加固工单总览异常信封校验(D-03/D-04/D-05)

整份 status_totals 缺失或非法计数值不再伪装为 0/NaN;队列缺 rows 或非法 total 抛错,避免误显空队列或垃圾值上屏。

Co-authored-by: Cursor <cursoragent@cursor.com>
cg преди 1 ден
родител
ревизия
7e61fab9f4
променени са 2 файла, в които са добавени 38 реда и са изтрити 4 реда
  1. 11 1
      web-scaffold/src/lib/ticket.ts
  2. 27 3
      web-scaffold/src/lib/work-order-counts.ts

+ 11 - 1
web-scaffold/src/lib/ticket.ts

@@ -96,6 +96,16 @@ export function useTicketList(
  * `includeCandidates=false`(默认):只要 total/行卡片(徽标/总览/队列首屏),服务端跳过候选人装配。
  * 指定成员时再按工单现拉 `include_candidates:true`(Wave F:首屏 ~2 UDS,避免热后 11 跳)。
  */
+/** D-05:成功信封须带数组 `rows` 与非负整数 `total`;缺字段/垃圾值抛错,⛔ 伪装空队列或直接上屏。 */
+function assertPendingQueue(r: PendingQueueResult): PendingQueueResult {
+  if (!Array.isArray(r.rows)) throw new ApiError(null, "待分派队列结构异常");
+  const total = r.total as unknown;
+  if (typeof total !== "number" || !Number.isFinite(total) || total < 0 || !Number.isInteger(total)) {
+    throw new ApiError(null, "待分派队列结构异常");
+  }
+  return r;
+}
+
 export function usePendingQueue(
   f: { ticket_types?: string[]; assign_team_id?: string; keyword?: string },
   page: number,
@@ -110,7 +120,7 @@ export function usePendingQueue(
     include_candidates: includeCandidates,
   };
   return useLoad<PendingQueueResult>(
-    () => ticketApi.ticketPendingQueue(body),
+    () => ticketApi.ticketPendingQueue(body).then(assertPendingQueue),
     [JSON.stringify(clean(body as Record<string, unknown>))],
   );
 }

+ 27 - 3
web-scaffold/src/lib/work-order-counts.ts

@@ -8,12 +8,15 @@
  * 纪律:
  * - ⛔ 用当页行数冒充总数;
  * - ⛔ 失败折成 0:任一路失败即整体 `error`,页面把数字渲染成 `—`;
+ * - 整份 `status_totals` 缺失/类型错误 ⇒ 抛错(D-03),⛔ 伪装成真实 0;
+ * - 个别非法计数值省略该键(D-04),调用方渲染 `—`,⛔ NaN/负张数上屏;
  * - 一次页面只发一次;状态集合变化才重取。
  */
+import { ApiError } from "./api/http.ts";
 import * as ticketApi from "./api/ticket.ts";
 import { useLoad } from "./useLoad.ts";
 
-/** 单状态张数;取不到(失败/未取)时调用方渲染 `—`。 */
+/** 单状态张数;取不到(失败/未取/非法)时调用方渲染 `—`。 */
 export type StatusCounts = Partial<Record<string, number>>;
 
 /** 只留能带进 `statuses` 的合法状态码。 */
@@ -21,6 +24,16 @@ const KNOWN = new Set([
   "unassigned", "pending", "processing", "completed", "rejected", "settled", "terminated",
 ]);
 
+/**
+ * 解析单个状态计数:键缺失 ⇒ 真实 0;值非法 ⇒ `undefined`(调用方渲染 `—`)。
+ * 合法=有限、非负整数(JSON 数字;⛔ 字符串垃圾、NaN、负数、小数)。
+ */
+function parseStatusTotal(raw: unknown): number | undefined {
+  if (raw === undefined) return 0;
+  if (typeof raw !== "number" || !Number.isFinite(raw) || raw < 0 || !Number.isInteger(raw)) return undefined;
+  return raw;
+}
+
 /**
  * 一次取回可见范围内各状态 `total`(服务端 `status_totals`)。
  *
@@ -32,7 +45,18 @@ export function useTicketStatusCounts(statuses: readonly string[]) {
   return useLoad<StatusCounts>(async () => {
     const wanted = key.split(",");
     const r = await ticketApi.ticketList({ page: 1, page_size: 1, include_status_totals: true });
-    const totals = r.status_totals ?? {};
-    return Object.fromEntries(wanted.map((status) => [status, Number(totals[status] ?? 0)])) as StatusCounts;
+    const raw = r.status_totals;
+    // D-03:整份缺失/null/数组/非对象 ⇒ 未知,⛔ 折成 {} 再填 0。
+    if (raw == null || typeof raw !== "object" || Array.isArray(raw)) {
+      throw new ApiError(null, "状态计数结构异常");
+    }
+    const totals = raw as Record<string, unknown>;
+    const out: StatusCounts = {};
+    for (const status of wanted) {
+      const n = parseStatusTotal(Object.prototype.hasOwnProperty.call(totals, status) ? totals[status] : undefined);
+      // D-04:非法值不写入 ⇒ Partial 保持 undefined ⇒ KPI「—」;合法/缺键照常。
+      if (n !== undefined) out[status] = n;
+    }
+    return out;
   }, [key], Boolean(key));
 }