AuditLogPage.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * 册10 SC-04 审计日志页(运营侧宿主;现路由 /audit-logs)。
  3. * 契约:docs/.../10-第10册-定时任务与审计视图/14-页面契约本册逐页.md §14.4、作业§5。
  4. * 接线现状:
  5. * - 行集=/audit/get_list@v1 [实] 游标翻页(⛔ 总数、⛔ 跳页);11001 ⇒ 整页 W35;其余失败 ⇒ W36+重试(横幅保留);
  6. * - 六列 W31:操作人「姓名快照+角色码」与对象「object_display 快照」后端未下发(I38 写侧待平台接通),
  7. * 操作人列显 username/user_id 真值;对象列走 D-71 回落链=契约快照 → 写侧随流水落下的名称快照
  8. * (`detail_json.object`/`after` 里的 `plan_name`/`contract_no`/`name` 等白名单键)→ 都取不到才 W32
  9. * +对象类型小字(⚠️ 不留空、不猜、⛔ 拿当前库里的名字顶替历史行=B-42/I38);
  10. * - 操作类型=编译期注册表(lib/audit-registry.ts,册1/2/3/5/10 登记表并集 + 真库实际机器名)机器名→中文名;
  11. * 查表前先 `normalizeAuditAction` 归一(D-59:写侧混用 `bos.` 前缀/点分变体/中文串三种形态);
  12. * 未登记动作 ⇒ 原样+「未登记动作」小字(作业§9.4 纪律②,⚠️ 不隐藏该行);
  13. * - 前后值区都为空时把 `detail_json` 以「明细:」并列进「变更后」列(D-67,⛔ 新增第七列);
  14. * - 导出=`/audit/export@v1` [实] 已声明并接线:按钮仅 admin/regAdmin 渲染(U-77),确认弹窗三行 W37a–c 缺一即缺陷;
  15. * ⚠️ 键 `bos.audit.export@v1` 尚未绑给运营三角色(2026-11-18 补绑撞平台 grant ceiling)⇒ 未绑期仍回
  16. * `11001`(按 `AUDIT_EXPORT_WIRING_NOTE` 提示,⛔ 说成「接口未声明」),KLT-R20903 ⇒ W38,KLT-S405 ⇒ W39。
  17. */
  18. import { useState } from "react";
  19. import { PageIntro } from "../components/PageIntro";
  20. import { Download, Search } from "lucide-react";
  21. import { Button } from "../components/ui/button";
  22. import { Input } from "../components/ui/input";
  23. import { FilterSelect } from "../components/ui/filter-select";
  24. import { DateRangeFilter } from "../components/ui/date-range-filter";
  25. import { OpsNotice, OpsTableColumns, OpsTableEmpty } from "../components/OperationsKit";
  26. import { useDemoIdentity } from "../lib/demo-session";
  27. import { EMPTY_AUDIT_FILTER, auditDetailLines, auditObjectSnapshot, auditSeatLines, formatAuditValue, submitAuditExport, useAuditList, type AuditFilter } from "../lib/audit-logs";
  28. import {
  29. AUDIT_ACTION_DISPLAY,
  30. AUDIT_ACTION_UNREGISTERED,
  31. AUDIT_OBJECT_TYPES,
  32. AUDIT_OBJECT_TYPE_DISPLAY,
  33. normalizeAuditAction,
  34. normalizeAuditObjectType,
  35. } from "../lib/audit-registry";
  36. import {
  37. BTN_CANCEL,
  38. BTN_CONFIRM_EXPORT,
  39. EXPORT_DIALOG_TITLE,
  40. W15,
  41. W31_COLUMNS,
  42. W32,
  43. W33A,
  44. W33B,
  45. W33C,
  46. W33D,
  47. W34A,
  48. W34B,
  49. W35,
  50. W36,
  51. W37A,
  52. W37B,
  53. W37C,
  54. } from "../lib/copy-jobs";
  55. import type { AuditItem } from "../lib/api/types";
  56. import "../styles/operations.css";
  57. /** occurred_at → 东八区「日期+时刻」两行(总则§10.2,⚠️ 不显 UTC 串);null ⇒ 「—」(时刻未知,⛔ 猜) */
  58. function TimeCell({ at }: { at: string | null }) {
  59. if (!at) return <>—</>;
  60. const date = new Date(at);
  61. if (Number.isNaN(date.getTime())) return <>{at}</>;
  62. const dateText = new Intl.DateTimeFormat("zh-CN", { timeZone: "Asia/Shanghai", year: "numeric", month: "2-digit", day: "2-digit" }).format(date);
  63. const timeText = new Intl.DateTimeFormat("zh-CN", { timeZone: "Asia/Shanghai", hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false }).format(date);
  64. return (
  65. <>
  66. {dateText}
  67. <br />
  68. <small>{timeText}</small>
  69. </>
  70. );
  71. }
  72. /** 第 3 列:中文动作名(注册表命中)/原样+未登记小字(作业§9.4②) */
  73. function ActionCell({ action }: { action: string }) {
  74. // D-59:写侧同一动作有三种形态(`bos.` 前缀/点分变体/中文串)⇒ 先归一再查表,查不到才判未登记
  75. const display = AUDIT_ACTION_DISPLAY[normalizeAuditAction(action)];
  76. if (display) return <>{display}</>;
  77. return (
  78. <>
  79. <span className="ops-mono">{action}</span>
  80. <br />
  81. <small>{AUDIT_ACTION_UNREGISTERED}</small>
  82. </>
  83. );
  84. }
  85. /**
  86. * 第 4 列:对象名回落链(D-71)=契约 `object_display` 快照 → 写侧随流水落下的名称快照
  87. * (`detail_json.object`/前后值区的白名单名称键)→ 都取不到才 W32;三种形态都并列「类型 · 主键」小字。
  88. * ⛔ 拿当前库里的名字顶替历史行(I38:改名会伪造流水),⛔ 留空。
  89. */
  90. function ObjectCell({ item }: { item: AuditItem }) {
  91. const typeText = AUDIT_OBJECT_TYPE_DISPLAY[normalizeAuditObjectType(item.object.type)] ?? item.object.type;
  92. const snapshot = auditObjectSnapshot(item);
  93. return (
  94. <>
  95. {snapshot ? (
  96. <span title={`对象名取自${snapshot.source}(事件当时的快照,不随后续改名变化)`}>{snapshot.text}</span>
  97. ) : (
  98. W32
  99. )}
  100. <br />
  101. <small>
  102. {typeText} · {item.object.id}
  103. </small>
  104. </>
  105. );
  106. }
  107. function ValueCell({ value }: { value: unknown }) {
  108. return (
  109. <>
  110. {formatAuditValue(value).map((line, index) => (
  111. <div key={index}>{line}</div>
  112. ))}
  113. </>
  114. );
  115. }
  116. /**
  117. * 第 6 列:变更后。前后值区都为空时把 `detail_json` 以「明细:」小标题并列(D-67,
  118. * 六列契约⛔ 新增第七列);脱敏沿用 `formatAuditValue`(识别号只出末 4 位、激活码不渲染值)。
  119. * 「分账号:」一行⛔ 受前后值区是否为空影响(D-76):订阅类流水前后值都有值,而 D-41 的额度
  120. * 对齐结果只落在 `detail_json.sub_accounts`,一并短路=审计页读不到补建了哪几个号。
  121. */
  122. function AfterCell({ item }: { item: AuditItem }) {
  123. const detail = auditDetailLines(item);
  124. const seats = auditSeatLines(item);
  125. return (
  126. <>
  127. <ValueCell value={item.after} />
  128. {detail.length > 0 && (
  129. <>
  130. <br />
  131. <small>明细:</small>
  132. {detail.map((line, index) => (
  133. <div key={`detail-${index}`}>{line}</div>
  134. ))}
  135. </>
  136. )}
  137. {seats.length > 0 && (
  138. <>
  139. <br />
  140. <small>分账号:</small>
  141. {seats.map((line, index) => (
  142. <div key={`seat-${index}`}>{line}</div>
  143. ))}
  144. </>
  145. )}
  146. </>
  147. );
  148. }
  149. /** C88 横幅(契约 14.4.2):按身份逐字;⚠️ 四句不得合并为通用文案 */
  150. function ScopeBanners({ role }: { role: string | undefined }) {
  151. if (role === "regAdmin") return <p className="ops-import-note">{W33A}</p>;
  152. if (role === "enterprise" || role === "sub") return <p className="ops-import-note">{W33C}</p>;
  153. if (role === "partner" || role === "operator" || role === "teamLead") {
  154. // 只读横幅三行:partner/operator 必挂,teamLead 建议挂(② 范围口径句各取 W33b/W33d)
  155. return (
  156. <>
  157. <p className="ops-import-note">{W34A}</p>
  158. <p className="ops-import-note">{role === "teamLead" ? W33D : W33B}</p>
  159. <p className="ops-import-note">{W34B}</p>
  160. </>
  161. );
  162. }
  163. return null; // admin 无追加句;ops 无本册横幅句
  164. }
  165. /** 导出二次确认弹窗(契约 14.4.4):正文三行缺一即缺陷;按钮「取消/确认导出」 */
  166. function ExportDialog({ filter, onCancel, onDone }: { filter: AuditFilter; onCancel: () => void; onDone: (message: string) => void }) {
  167. const [pending, setPending] = useState(false);
  168. const [error, setError] = useState<string | null>(null);
  169. const submit = async () => {
  170. setPending(true);
  171. setError(null);
  172. const outcome = await submitAuditExport(filter);
  173. setPending(false);
  174. if (outcome.kind === "error") {
  175. setError(outcome.message); // 失败:弹窗保持,⛔ 关窗吞错
  176. return;
  177. }
  178. onDone(`已产出导出文件 ${outcome.fileName}(${outcome.rowCount} 行),文件经 OSS 下发。`);
  179. };
  180. return (
  181. <div className="ops-confirm-backdrop" role="presentation" onMouseDown={onCancel}>
  182. <div className="ops-confirm" role="dialog" aria-modal="true" aria-labelledby="audit-export-title" onMouseDown={(event) => event.stopPropagation()}>
  183. <h2 id="audit-export-title">{EXPORT_DIALOG_TITLE}</h2>
  184. <p>{W37A}</p>
  185. <p>{W37B}</p>
  186. <p>{W37C}</p>
  187. {error && (
  188. <p className="ops-form-error" role="alert">
  189. {error}
  190. </p>
  191. )}
  192. <div>
  193. <Button type="button" variant="ghost" disabled={pending} onClick={onCancel}>
  194. {BTN_CANCEL}
  195. </Button>
  196. <Button type="button" disabled={pending} onClick={submit}>
  197. {pending ? "提交中…" : BTN_CONFIRM_EXPORT}
  198. </Button>
  199. </div>
  200. </div>
  201. </div>
  202. );
  203. }
  204. /**
  205. * SC-04 审计日志页 / SC-05 四角色侧「变更记录」区。
  206. *
  207. * ⚠️ 同一组件承载两条用例(作业§5.3 纪律 3「同页两套行集按身份分叉,不复制页面」):
  208. * 运营侧标题「审计日志」并可导出;`partnerAcct`/`opAcct`/`teamLead` 由宿主路由传入
  209. * `pageTitle="本方变更记录"`,横幅与导出按钮都按身份自动收起(四类身份⛔ 渲染导出)。
  210. */
  211. export function AuditLogPage({ pageTitle = "审计日志" }: { pageTitle?: string } = {}) {
  212. const identity = useDemoIdentity();
  213. const role = identity?.role;
  214. const canExport = role === "admin" || role === "regAdmin"; // U-77:未放开身份不渲染按钮(⛔ 灰按钮)
  215. const list = useAuditList();
  216. const [draft, setDraft] = useState<AuditFilter>(EMPTY_AUDIT_FILTER);
  217. const [filterError, setFilterError] = useState<string | null>(null);
  218. const [exporting, setExporting] = useState(false);
  219. const [notice, setNotice] = useState<string | null>(null);
  220. const change = (key: keyof AuditFilter, value: string) => {
  221. setDraft((current) => ({ ...current, [key]: value }));
  222. setFilterError(null);
  223. };
  224. const apply = () => {
  225. // 主体定位只接受 ID 且须与对象类型成对(作业§5.4);缺类型 ⇒ 拒发并提示,⛔ 静默丢条件
  226. if (draft.objectId && !draft.objectType) {
  227. setFilterError("主体定位须先选择对象类型。");
  228. return;
  229. }
  230. setFilterError(null);
  231. list.apply(draft);
  232. };
  233. const reset = () => {
  234. setDraft(EMPTY_AUDIT_FILTER);
  235. setFilterError(null);
  236. list.apply(EMPTY_AUDIT_FILTER);
  237. };
  238. // 读口未声明(audit_read_ready=false 等价信号)⇒ 整页 W35,⚠️ 不显 W15
  239. if (list.phase.kind === "undeclared") {
  240. return (
  241. <section className="ops-page ops-page--audit page-fill">
  242. <header className="ops-heading">
  243. <div>
  244. <h1>{pageTitle}</h1>
  245. <PageIntro>需要追查一笔操作时,可按时间、账号或业务对象查找记录。</PageIntro>
  246. </div>
  247. </header>
  248. <div className="ops-empty">
  249. <Search size={27} />
  250. <h3>{W35}</h3>
  251. </div>
  252. </section>
  253. );
  254. }
  255. return (
  256. <section className="ops-page ops-page--audit page-fill">
  257. <header className="ops-heading">
  258. <div>
  259. <h1>{pageTitle}</h1>
  260. <PageIntro>需要追查一笔操作时,可按时间、账号或业务对象查找记录。</PageIntro>
  261. </div>
  262. {canExport && (
  263. <Button variant="outline" type="button" onClick={() => setExporting(true)}>
  264. <Download size={16} />
  265. {EXPORT_DIALOG_TITLE}
  266. </Button>
  267. )}
  268. </header>
  269. <ScopeBanners role={role} />
  270. <section className="ops-list-shell list-shell-fill" aria-label="操作记录">
  271. {/* 检索区四件(契约 14.4.1):对象类型/主体定位(只接受 ID)/操作人(ID)/时间区间+关键字 */}
  272. <div className="ops-toolbar flex-wrap">
  273. <FilterSelect icon="type" aria-label="对象类型筛选" value={draft.objectType} onChange={(event) => change("objectType", event.target.value)}>
  274. <option value="">全部对象类型</option>
  275. {AUDIT_OBJECT_TYPES.map((type) => (
  276. <option value={type} key={type}>
  277. {AUDIT_OBJECT_TYPE_DISPLAY[type]}
  278. </option>
  279. ))}
  280. </FilterSelect>
  281. <label className="ops-search">
  282. <Search size={17} />
  283. <Input aria-label="主体定位(对象 ID)" placeholder="主体定位:对象 ID" value={draft.objectId} onChange={(event) => change("objectId", event.target.value)} />
  284. </label>
  285. <label className="ops-search">
  286. <Search size={17} />
  287. <Input aria-label="操作人(账号 ID)" placeholder="操作人:账号 ID" value={draft.actorId} onChange={(event) => change("actorId", event.target.value)} />
  288. </label>
  289. <DateRangeFilter label="时间范围" from={draft.from} to={draft.to} onChange={change} constrainRange />
  290. <label className="ops-search">
  291. <Search size={17} />
  292. <Input aria-label="关键字" placeholder="关键字" value={draft.keyword} onChange={(event) => change("keyword", event.target.value)} />
  293. </label>
  294. <Button type="button" size="sm" onClick={apply}>
  295. 查询
  296. </Button>
  297. <Button type="button" size="sm" variant="ghost" onClick={reset}>
  298. 重置筛选
  299. </Button>
  300. </div>
  301. {filterError && (
  302. <p className="ops-form-error" role="alert">
  303. {filterError}
  304. </p>
  305. )}
  306. {list.phase.kind === "error" && (
  307. <p className="ops-form-error" role="alert">
  308. {W36}:{list.phase.message}{" "}
  309. <Button size="sm" type="button" variant="outline" onClick={list.reload}>
  310. 重试
  311. </Button>
  312. </p>
  313. )}
  314. <div className="measured-table-scroll ops-table-wrap" role="region" aria-label={`${pageTitle},可横向滚动查看字段`} tabIndex={0}>
  315. <table className="ops-table">
  316. <OpsTableColumns widths={[11, 11, 13, 18, 22, 25]} />
  317. <thead>
  318. <tr>
  319. {W31_COLUMNS.map((column) => (
  320. <th key={column}>{column}</th>
  321. ))}
  322. </tr>
  323. </thead>
  324. <tbody>
  325. {list.items.map((item) => (
  326. <tr key={item.cursor ?? `${item.object.type}-${item.object.id}-${item.action}`}>
  327. <td>
  328. <TimeCell at={item.occurred_at} />
  329. </td>
  330. <td>{item.actor.username ?? item.actor.user_id ?? "—"}</td>
  331. <td>
  332. <ActionCell action={item.action} />
  333. </td>
  334. <td>
  335. <ObjectCell item={item} />
  336. </td>
  337. <td>
  338. <ValueCell value={item.before} />
  339. </td>
  340. <td>
  341. <AfterCell item={item} />
  342. </td>
  343. </tr>
  344. ))}
  345. {list.phase.kind === "loading" && <OpsTableEmpty colSpan={6}>正在加载…</OpsTableEmpty>}
  346. {list.phase.kind === "ready" && list.items.length === 0 && <OpsTableEmpty colSpan={6}>{W15}</OpsTableEmpty>}
  347. </tbody>
  348. </table>
  349. </div>
  350. {list.nextCursor && (
  351. <footer className="ops-list-footer">
  352. <Button variant="outline" size="sm" type="button" disabled={list.loadingMore} onClick={list.loadMore}>
  353. {list.loadingMore ? "加载中…" : "加载更多"}
  354. </Button>
  355. </footer>
  356. )}
  357. </section>
  358. {exporting && (
  359. <ExportDialog
  360. filter={list.applied}
  361. onCancel={() => setExporting(false)}
  362. onDone={(message) => {
  363. setExporting(false);
  364. setNotice(message);
  365. }}
  366. />
  367. )}
  368. {notice && <OpsNotice message={notice} onClose={() => setNotice(null)} />}
  369. </section>
  370. );
  371. }