| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- /**
- * 册10 SC-04 审计日志页(运营侧宿主;现路由 /audit-logs)。
- * 契约:docs/.../10-第10册-定时任务与审计视图/14-页面契约本册逐页.md §14.4、作业§5。
- * 接线现状:
- * - 行集=/audit/get_list@v1 [实] 游标翻页(⛔ 总数、⛔ 跳页);11001 ⇒ 整页 W35;其余失败 ⇒ W36+重试(横幅保留);
- * - 六列 W31:操作人「姓名快照+角色码」与对象「object_display 快照」后端未下发(I38 写侧待平台接通),
- * 操作人列显 username/user_id 真值;对象列走 D-71 回落链=契约快照 → 写侧随流水落下的名称快照
- * (`detail_json.object`/`after` 里的 `plan_name`/`contract_no`/`name` 等白名单键)→ 都取不到才 W32
- * +对象类型小字(⚠️ 不留空、不猜、⛔ 拿当前库里的名字顶替历史行=B-42/I38);
- * - 操作类型=编译期注册表(lib/audit-registry.ts,册1/2/3/5/10 登记表并集 + 真库实际机器名)机器名→中文名;
- * 查表前先 `normalizeAuditAction` 归一(D-59:写侧混用 `bos.` 前缀/点分变体/中文串三种形态);
- * 未登记动作 ⇒ 原样+「未登记动作」小字(作业§9.4 纪律②,⚠️ 不隐藏该行);
- * - 前后值区都为空时把 `detail_json` 以「明细:」并列进「变更后」列(D-67,⛔ 新增第七列);
- * - 导出=`/audit/export@v1` [实] 已声明并接线:按钮仅 admin/regAdmin 渲染(U-77),确认弹窗三行 W37a–c 缺一即缺陷;
- * ⚠️ 键 `bos.audit.export@v1` 尚未绑给运营三角色(2026-11-18 补绑撞平台 grant ceiling)⇒ 未绑期仍回
- * `11001`(按 `AUDIT_EXPORT_WIRING_NOTE` 提示,⛔ 说成「接口未声明」),KLT-R20903 ⇒ W38,KLT-S405 ⇒ W39。
- */
- import { useState } from "react";
- import { PageIntro } from "../components/PageIntro";
- import { Download, Search } from "lucide-react";
- import { Button } from "../components/ui/button";
- import { Input } from "../components/ui/input";
- import { FilterSelect } from "../components/ui/filter-select";
- import { DateRangeFilter } from "../components/ui/date-range-filter";
- import { OpsNotice, OpsTableColumns, OpsTableEmpty } from "../components/OperationsKit";
- import { useDemoIdentity } from "../lib/demo-session";
- import { EMPTY_AUDIT_FILTER, auditDetailLines, auditObjectSnapshot, auditSeatLines, formatAuditValue, submitAuditExport, useAuditList, type AuditFilter } from "../lib/audit-logs";
- import {
- AUDIT_ACTION_DISPLAY,
- AUDIT_ACTION_UNREGISTERED,
- AUDIT_OBJECT_TYPES,
- AUDIT_OBJECT_TYPE_DISPLAY,
- normalizeAuditAction,
- normalizeAuditObjectType,
- } from "../lib/audit-registry";
- import {
- BTN_CANCEL,
- BTN_CONFIRM_EXPORT,
- EXPORT_DIALOG_TITLE,
- W15,
- W31_COLUMNS,
- W32,
- W33A,
- W33B,
- W33C,
- W33D,
- W34A,
- W34B,
- W35,
- W36,
- W37A,
- W37B,
- W37C,
- } from "../lib/copy-jobs";
- import type { AuditItem } from "../lib/api/types";
- import "../styles/operations.css";
- /** occurred_at → 东八区「日期+时刻」两行(总则§10.2,⚠️ 不显 UTC 串);null ⇒ 「—」(时刻未知,⛔ 猜) */
- function TimeCell({ at }: { at: string | null }) {
- if (!at) return <>—</>;
- const date = new Date(at);
- if (Number.isNaN(date.getTime())) return <>{at}</>;
- const dateText = new Intl.DateTimeFormat("zh-CN", { timeZone: "Asia/Shanghai", year: "numeric", month: "2-digit", day: "2-digit" }).format(date);
- const timeText = new Intl.DateTimeFormat("zh-CN", { timeZone: "Asia/Shanghai", hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false }).format(date);
- return (
- <>
- {dateText}
- <br />
- <small>{timeText}</small>
- </>
- );
- }
- /** 第 3 列:中文动作名(注册表命中)/原样+未登记小字(作业§9.4②) */
- function ActionCell({ action }: { action: string }) {
- // D-59:写侧同一动作有三种形态(`bos.` 前缀/点分变体/中文串)⇒ 先归一再查表,查不到才判未登记
- const display = AUDIT_ACTION_DISPLAY[normalizeAuditAction(action)];
- if (display) return <>{display}</>;
- return (
- <>
- <span className="ops-mono">{action}</span>
- <br />
- <small>{AUDIT_ACTION_UNREGISTERED}</small>
- </>
- );
- }
- /**
- * 第 4 列:对象名回落链(D-71)=契约 `object_display` 快照 → 写侧随流水落下的名称快照
- * (`detail_json.object`/前后值区的白名单名称键)→ 都取不到才 W32;三种形态都并列「类型 · 主键」小字。
- * ⛔ 拿当前库里的名字顶替历史行(I38:改名会伪造流水),⛔ 留空。
- */
- function ObjectCell({ item }: { item: AuditItem }) {
- const typeText = AUDIT_OBJECT_TYPE_DISPLAY[normalizeAuditObjectType(item.object.type)] ?? item.object.type;
- const snapshot = auditObjectSnapshot(item);
- return (
- <>
- {snapshot ? (
- <span title={`对象名取自${snapshot.source}(事件当时的快照,不随后续改名变化)`}>{snapshot.text}</span>
- ) : (
- W32
- )}
- <br />
- <small>
- {typeText} · {item.object.id}
- </small>
- </>
- );
- }
- function ValueCell({ value }: { value: unknown }) {
- return (
- <>
- {formatAuditValue(value).map((line, index) => (
- <div key={index}>{line}</div>
- ))}
- </>
- );
- }
- /**
- * 第 6 列:变更后。前后值区都为空时把 `detail_json` 以「明细:」小标题并列(D-67,
- * 六列契约⛔ 新增第七列);脱敏沿用 `formatAuditValue`(识别号只出末 4 位、激活码不渲染值)。
- * 「分账号:」一行⛔ 受前后值区是否为空影响(D-76):订阅类流水前后值都有值,而 D-41 的额度
- * 对齐结果只落在 `detail_json.sub_accounts`,一并短路=审计页读不到补建了哪几个号。
- */
- function AfterCell({ item }: { item: AuditItem }) {
- const detail = auditDetailLines(item);
- const seats = auditSeatLines(item);
- return (
- <>
- <ValueCell value={item.after} />
- {detail.length > 0 && (
- <>
- <br />
- <small>明细:</small>
- {detail.map((line, index) => (
- <div key={`detail-${index}`}>{line}</div>
- ))}
- </>
- )}
- {seats.length > 0 && (
- <>
- <br />
- <small>分账号:</small>
- {seats.map((line, index) => (
- <div key={`seat-${index}`}>{line}</div>
- ))}
- </>
- )}
- </>
- );
- }
- /** C88 横幅(契约 14.4.2):按身份逐字;⚠️ 四句不得合并为通用文案 */
- function ScopeBanners({ role }: { role: string | undefined }) {
- if (role === "regAdmin") return <p className="ops-import-note">{W33A}</p>;
- if (role === "enterprise" || role === "sub") return <p className="ops-import-note">{W33C}</p>;
- if (role === "partner" || role === "operator" || role === "teamLead") {
- // 只读横幅三行:partner/operator 必挂,teamLead 建议挂(② 范围口径句各取 W33b/W33d)
- return (
- <>
- <p className="ops-import-note">{W34A}</p>
- <p className="ops-import-note">{role === "teamLead" ? W33D : W33B}</p>
- <p className="ops-import-note">{W34B}</p>
- </>
- );
- }
- return null; // admin 无追加句;ops 无本册横幅句
- }
- /** 导出二次确认弹窗(契约 14.4.4):正文三行缺一即缺陷;按钮「取消/确认导出」 */
- function ExportDialog({ filter, onCancel, onDone }: { filter: AuditFilter; onCancel: () => void; onDone: (message: string) => void }) {
- const [pending, setPending] = useState(false);
- const [error, setError] = useState<string | null>(null);
- const submit = async () => {
- setPending(true);
- setError(null);
- const outcome = await submitAuditExport(filter);
- setPending(false);
- if (outcome.kind === "error") {
- setError(outcome.message); // 失败:弹窗保持,⛔ 关窗吞错
- return;
- }
- onDone(`已产出导出文件 ${outcome.fileName}(${outcome.rowCount} 行),文件经 OSS 下发。`);
- };
- return (
- <div className="ops-confirm-backdrop" role="presentation" onMouseDown={onCancel}>
- <div className="ops-confirm" role="dialog" aria-modal="true" aria-labelledby="audit-export-title" onMouseDown={(event) => event.stopPropagation()}>
- <h2 id="audit-export-title">{EXPORT_DIALOG_TITLE}</h2>
- <p>{W37A}</p>
- <p>{W37B}</p>
- <p>{W37C}</p>
- {error && (
- <p className="ops-form-error" role="alert">
- {error}
- </p>
- )}
- <div>
- <Button type="button" variant="ghost" disabled={pending} onClick={onCancel}>
- {BTN_CANCEL}
- </Button>
- <Button type="button" disabled={pending} onClick={submit}>
- {pending ? "提交中…" : BTN_CONFIRM_EXPORT}
- </Button>
- </div>
- </div>
- </div>
- );
- }
- /**
- * SC-04 审计日志页 / SC-05 四角色侧「变更记录」区。
- *
- * ⚠️ 同一组件承载两条用例(作业§5.3 纪律 3「同页两套行集按身份分叉,不复制页面」):
- * 运营侧标题「审计日志」并可导出;`partnerAcct`/`opAcct`/`teamLead` 由宿主路由传入
- * `pageTitle="本方变更记录"`,横幅与导出按钮都按身份自动收起(四类身份⛔ 渲染导出)。
- */
- export function AuditLogPage({ pageTitle = "审计日志" }: { pageTitle?: string } = {}) {
- const identity = useDemoIdentity();
- const role = identity?.role;
- const canExport = role === "admin" || role === "regAdmin"; // U-77:未放开身份不渲染按钮(⛔ 灰按钮)
- const list = useAuditList();
- const [draft, setDraft] = useState<AuditFilter>(EMPTY_AUDIT_FILTER);
- const [filterError, setFilterError] = useState<string | null>(null);
- const [exporting, setExporting] = useState(false);
- const [notice, setNotice] = useState<string | null>(null);
- const change = (key: keyof AuditFilter, value: string) => {
- setDraft((current) => ({ ...current, [key]: value }));
- setFilterError(null);
- };
- const apply = () => {
- // 主体定位只接受 ID 且须与对象类型成对(作业§5.4);缺类型 ⇒ 拒发并提示,⛔ 静默丢条件
- if (draft.objectId && !draft.objectType) {
- setFilterError("主体定位须先选择对象类型。");
- return;
- }
- setFilterError(null);
- list.apply(draft);
- };
- const reset = () => {
- setDraft(EMPTY_AUDIT_FILTER);
- setFilterError(null);
- list.apply(EMPTY_AUDIT_FILTER);
- };
- // 读口未声明(audit_read_ready=false 等价信号)⇒ 整页 W35,⚠️ 不显 W15
- if (list.phase.kind === "undeclared") {
- return (
- <section className="ops-page ops-page--audit page-fill">
- <header className="ops-heading">
- <div>
- <h1>{pageTitle}</h1>
- <PageIntro>需要追查一笔操作时,可按时间、账号或业务对象查找记录。</PageIntro>
- </div>
- </header>
- <div className="ops-empty">
- <Search size={27} />
- <h3>{W35}</h3>
- </div>
- </section>
- );
- }
- return (
- <section className="ops-page ops-page--audit page-fill">
- <header className="ops-heading">
- <div>
- <h1>{pageTitle}</h1>
- <PageIntro>需要追查一笔操作时,可按时间、账号或业务对象查找记录。</PageIntro>
- </div>
- {canExport && (
- <Button variant="outline" type="button" onClick={() => setExporting(true)}>
- <Download size={16} />
- {EXPORT_DIALOG_TITLE}
- </Button>
- )}
- </header>
- <ScopeBanners role={role} />
- <section className="ops-list-shell list-shell-fill" aria-label="操作记录">
- {/* 检索区四件(契约 14.4.1):对象类型/主体定位(只接受 ID)/操作人(ID)/时间区间+关键字 */}
- <div className="ops-toolbar flex-wrap">
- <FilterSelect icon="type" aria-label="对象类型筛选" value={draft.objectType} onChange={(event) => change("objectType", event.target.value)}>
- <option value="">全部对象类型</option>
- {AUDIT_OBJECT_TYPES.map((type) => (
- <option value={type} key={type}>
- {AUDIT_OBJECT_TYPE_DISPLAY[type]}
- </option>
- ))}
- </FilterSelect>
- <label className="ops-search">
- <Search size={17} />
- <Input aria-label="主体定位(对象 ID)" placeholder="主体定位:对象 ID" value={draft.objectId} onChange={(event) => change("objectId", event.target.value)} />
- </label>
- <label className="ops-search">
- <Search size={17} />
- <Input aria-label="操作人(账号 ID)" placeholder="操作人:账号 ID" value={draft.actorId} onChange={(event) => change("actorId", event.target.value)} />
- </label>
- <DateRangeFilter label="时间范围" from={draft.from} to={draft.to} onChange={change} constrainRange />
- <label className="ops-search">
- <Search size={17} />
- <Input aria-label="关键字" placeholder="关键字" value={draft.keyword} onChange={(event) => change("keyword", event.target.value)} />
- </label>
- <Button type="button" size="sm" onClick={apply}>
- 查询
- </Button>
- <Button type="button" size="sm" variant="ghost" onClick={reset}>
- 重置筛选
- </Button>
- </div>
- {filterError && (
- <p className="ops-form-error" role="alert">
- {filterError}
- </p>
- )}
- {list.phase.kind === "error" && (
- <p className="ops-form-error" role="alert">
- {W36}:{list.phase.message}{" "}
- <Button size="sm" type="button" variant="outline" onClick={list.reload}>
- 重试
- </Button>
- </p>
- )}
- <div className="measured-table-scroll ops-table-wrap" role="region" aria-label={`${pageTitle},可横向滚动查看字段`} tabIndex={0}>
- <table className="ops-table">
- <OpsTableColumns widths={[11, 11, 13, 18, 22, 25]} />
- <thead>
- <tr>
- {W31_COLUMNS.map((column) => (
- <th key={column}>{column}</th>
- ))}
- </tr>
- </thead>
- <tbody>
- {list.items.map((item) => (
- <tr key={item.cursor ?? `${item.object.type}-${item.object.id}-${item.action}`}>
- <td>
- <TimeCell at={item.occurred_at} />
- </td>
- <td>{item.actor.username ?? item.actor.user_id ?? "—"}</td>
- <td>
- <ActionCell action={item.action} />
- </td>
- <td>
- <ObjectCell item={item} />
- </td>
- <td>
- <ValueCell value={item.before} />
- </td>
- <td>
- <AfterCell item={item} />
- </td>
- </tr>
- ))}
- {list.phase.kind === "loading" && <OpsTableEmpty colSpan={6}>正在加载…</OpsTableEmpty>}
- {list.phase.kind === "ready" && list.items.length === 0 && <OpsTableEmpty colSpan={6}>{W15}</OpsTableEmpty>}
- </tbody>
- </table>
- </div>
- {list.nextCursor && (
- <footer className="ops-list-footer">
- <Button variant="outline" size="sm" type="button" disabled={list.loadingMore} onClick={list.loadMore}>
- {list.loadingMore ? "加载中…" : "加载更多"}
- </Button>
- </footer>
- )}
- </section>
- {exporting && (
- <ExportDialog
- filter={list.applied}
- onCancel={() => setExporting(false)}
- onDone={(message) => {
- setExporting(false);
- setNotice(message);
- }}
- />
- )}
- {notice && <OpsNotice message={notice} onClose={() => setNotice(null)} />}
- </section>
- );
- }
|