diff --git a/src/amis/approvalActionEvents.ts b/src/amis/approvalActionEvents.ts index 147beb7..81fe7ca 100644 --- a/src/amis/approvalActionEvents.ts +++ b/src/amis/approvalActionEvents.ts @@ -2,7 +2,7 @@ export const OPEN_APPROVAL_DRAWER_EVENT = 'eip4:open-approval-drawer'; export const APPROVAL_COMPLETED_EVENT = 'eip4:approval-completed'; export interface ApprovalDrawerRequest { - taskId: string; + taskId?: string; taskName?: string; processInstanceId?: string; businessKey?: string; diff --git a/src/amis/registerApprovalAction.test.ts b/src/amis/registerApprovalAction.test.ts index 52faef8..5a8a534 100644 --- a/src/amis/registerApprovalAction.test.ts +++ b/src/amis/registerApprovalAction.test.ts @@ -67,7 +67,43 @@ describe('registerApprovalAction', () => { }); }); - it('缺 taskId 时不派发事件', async () => { + it('缺 taskId 但有 businessKey 时仍派发只读历史审批事件', async () => { + let captured: RunCtx | null = null; + registerApprovalAction({ + registerAction: (_type: string, action: RunCtx) => { + captured = action; + }, + }); + const seen: unknown[] = []; + const listener = (event: Event) => { + seen.push((event as CustomEvent).detail); + }; + window.addEventListener(OPEN_APPROVAL_DRAWER_EVENT, listener); + try { + await captured!.run({ + args: { + taskName: '审批详情', + businessKey: 'biz-1', + businessTable: 'PM_DEMO', + readOnly: true, + }, + }, {}, {}); + } finally { + window.removeEventListener(OPEN_APPROVAL_DRAWER_EVENT, listener); + } + expect(seen).toHaveLength(1); + expect(seen[0]).toEqual({ + taskId: undefined, + taskName: '审批详情', + processInstanceId: undefined, + businessKey: 'biz-1', + businessTable: 'PM_DEMO', + businessSummary: undefined, + readOnly: true, + }); + }); + + it('缺 taskId/businessKey/processInstanceId 时不派发事件', async () => { let captured: RunCtx | null = null; registerApprovalAction({ registerAction: (_type: string, action: RunCtx) => { diff --git a/src/amis/registerApprovalAction.ts b/src/amis/registerApprovalAction.ts index 4f2e205..e9e8ffc 100644 --- a/src/amis/registerApprovalAction.ts +++ b/src/amis/registerApprovalAction.ts @@ -57,14 +57,16 @@ export function registerApprovalAction(amisCore: AmisCore | undefined | null): b async run(action: OpenApprovalActionArg, _renderer: AmisRenderer, _event: AmisEvent) { const args = action?.args ?? {}; const taskId = asString(args.taskId); - if (!taskId) return; + const processInstanceId = asString(args.processInstanceId); + const businessKey = asString(args.businessKey); + if (!taskId && !processInstanceId && !businessKey) return; const mode = asString(args.mode); const detail: ApprovalDrawerRequest = { taskId, taskName: asString(args.taskName), - processInstanceId: asString(args.processInstanceId), - businessKey: asString(args.businessKey), + processInstanceId, + businessKey, businessTable: asString(args.businessTable), businessSummary: asRecord(args.businessSummary), readOnly: asBoolean(args.readOnly) || asBoolean(args.readonly) || mode === 'read', diff --git a/src/api/approval.ts b/src/api/approval.ts index 0e38620..6fbbf51 100644 --- a/src/api/approval.ts +++ b/src/api/approval.ts @@ -52,6 +52,13 @@ export async function fetchTrace(processInstanceId: string): Promise { + const { data } = await http.get( + `/api/flow/business/${encodeURIComponent(businessKey)}/trace`, + ); + return Array.isArray(data) ? data : []; +} + // --------------------------------------------------------------------------- // WfTemplate 驱动审批 —— 动态按钮 + 业务表单 + 按 type 分派 // --------------------------------------------------------------------------- @@ -104,6 +111,30 @@ export async function fetchApprovalMeta(taskId: string): Promise { return data; } +export interface ApprovalHistoryParams { + businessKey?: string; + processInstanceId?: string; + businessTable?: string; +} + +function historyQuery(params: ApprovalHistoryParams): string { + const query = new URLSearchParams(); + if (params.businessKey) query.set('businessKey', params.businessKey); + if (params.processInstanceId) query.set('processInstanceId', params.processInstanceId); + if (params.businessTable) query.set('businessTable', params.businessTable); + const s = query.toString(); + return s ? `?${s}` : ''; +} + +export async function fetchApprovalHistoryMeta(params: ApprovalHistoryParams): Promise { + const { data } = await http.get(`/api/approval/history/meta${historyQuery(params)}`); + return data; +} + +export function approvalHistoryBusinessDataApi(params: ApprovalHistoryParams): string { + return `get:/api/approval/history/business-data${historyQuery(params)}`; +} + export async function submitApprovalAction( taskId: string, req: ApprovalActionRequest, diff --git a/src/components/ApprovalDrawer.tsx b/src/components/ApprovalDrawer.tsx index 83caeef..e5b23c0 100644 --- a/src/components/ApprovalDrawer.tsx +++ b/src/components/ApprovalDrawer.tsx @@ -10,11 +10,19 @@ import { Drawer } from './ui/Dialog'; import { toast } from './ui/Toast'; import { AmisRenderer } from './AmisRenderer'; import { http } from '../api/http'; -import { delegateTask, fetchTrace, fetchApprovalMeta, submitApprovalAction } from '../api/approval'; +import { + approvalHistoryBusinessDataApi, + delegateTask, + fetchApprovalHistoryMeta, + fetchApprovalMeta, + fetchTrace, + fetchTraceByBusiness, + submitApprovalAction, +} from '../api/approval'; import type { TraceNode, ApprovalMeta, ApprovalButton } from '../api/approval'; export interface ApprovalDrawerProps { - taskId: string; + taskId?: string; taskName?: string; processInstanceId?: string; businessKey?: string; @@ -56,13 +64,13 @@ function buttonClass(type: string): { className: string; style?: React.CSSProper * (含子表、引用列 id→名,对齐老审批页),4s 超时硬上限防挂死、超时退化 loadPOJO。前端无需关心 load/entity 之分。 * 返回 amis 单记录契约 {status:0,data:{...record}},表单字段(name=列名)直接绑定。 */ -function buildReadonlyForm(rawSchema: unknown, taskId: string): Record | null { +function buildReadonlyForm(rawSchema: unknown, dataApi: string): Record | null { if (!rawSchema || typeof rawSchema !== 'object') return null; const obj = rawSchema as Record; const inner = (obj.body && typeof obj.body === 'object' ? obj.body : obj) as Record; return { type: 'service', - api: `get:/api/task/${taskId}/business-data`, + api: dataApi, body: { ...inner, static: true, actions: [], submitText: '' }, }; } @@ -210,6 +218,11 @@ export function ApprovalDrawer({ const effectiveProcessInstanceId = processInstanceId ?? meta?.processInstanceId ?? undefined; const effectiveBusinessKey = businessKey ?? meta?.businessKey ?? undefined; const effectiveBusinessTable = businessTable ?? meta?.businessTable ?? undefined; + const historyParams = { + businessKey: effectiveBusinessKey, + processInstanceId: effectiveProcessInstanceId, + businessTable: effectiveBusinessTable, + }; // 审批元数据(按钮/脚本/业务表单全来自 WfTemplate) useEffect(() => { @@ -218,24 +231,30 @@ export function ApprovalDrawer({ setMeta(null); setFormSchema(null); setTraceNodes([]); - fetchApprovalMeta(taskId) + const loader = taskId + ? fetchApprovalMeta(taskId) + : fetchApprovalHistoryMeta({ businessKey, processInstanceId, businessTable }); + loader .then((m) => alive && setMeta(m)) .catch(() => alive && setMeta(null)) .finally(() => alive && setMetaLoading(false)); return () => { alive = false; }; - }, [taskId]); + }, [taskId, businessKey, processInstanceId, businessTable]); // 流程轨迹 useEffect(() => { - if (!effectiveProcessInstanceId) return; + if (!effectiveProcessInstanceId && !effectiveBusinessKey) return; let alive = true; setTraceLoading(true); - fetchTrace(effectiveProcessInstanceId) + const loader = effectiveProcessInstanceId + ? fetchTrace(effectiveProcessInstanceId) + : fetchTraceByBusiness(effectiveBusinessKey!); + loader .then((n) => alive && setTraceNodes(n)) .catch(() => alive && setTraceNodes([])) .finally(() => alive && setTraceLoading(false)); return () => { alive = false; }; - }, [effectiveProcessInstanceId]); + }, [effectiveProcessInstanceId, effectiveBusinessKey]); // 业务单据只读表单(统一 editform schema;数据走 /api/task/{id}/business-data 由后端载入) useEffect(() => { @@ -243,12 +262,15 @@ export function ApprovalDrawer({ const tpl = meta?.formTemplateId; if (tpl) { setFormLoading(true); + const dataApi = taskId + ? `get:/api/task/${taskId}/business-data` + : approvalHistoryBusinessDataApi(historyParams); http .get(`/api/page/schema/${tpl}`) .then((res) => { if (!alive) return; const raw = typeof res.data === 'string' ? JSON.parse(res.data) : res.data; - setFormSchema(buildReadonlyForm(raw, taskId)); + setFormSchema(buildReadonlyForm(raw, dataApi)); }) .catch(() => alive && setFormSchema(null)) .finally(() => alive && setFormLoading(false)); @@ -256,9 +278,13 @@ export function ApprovalDrawer({ setFormSchema(null); } return () => { alive = false; }; - }, [meta, taskId]); + }, [meta, taskId, effectiveBusinessKey, effectiveProcessInstanceId, effectiveBusinessTable]); const handleAction = async (btn: ApprovalButton) => { + if (!taskId) { + toast('缺少任务ID,无法办理', 'warn'); + return; + } if (btn.type === 'agent') { setShowDelegate(true); return; @@ -387,7 +413,7 @@ export function ApprovalDrawer({ {/* 转办弹窗 */} - {showDelegate && ( + {showDelegate && taskId && ( setShowDelegate(false)}> { const openApprovalDrawer = (event: Event) => { const detail = (event as CustomEvent).detail; - if (!detail?.taskId) return; + if (!detail?.taskId && !detail?.businessKey && !detail?.processInstanceId) return; setApprovalRequest(detail); }; window.addEventListener(OPEN_APPROVAL_DRAWER_EVENT, openApprovalDrawer);