fix(web): support approval history drawer
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -52,6 +52,13 @@ export async function fetchTrace(processInstanceId: string): Promise<TraceNode[]
|
||||
return Array.isArray(data) ? data : [];
|
||||
}
|
||||
|
||||
export async function fetchTraceByBusiness(businessKey: string): Promise<TraceNode[]> {
|
||||
const { data } = await http.get<TraceNode[]>(
|
||||
`/api/flow/business/${encodeURIComponent(businessKey)}/trace`,
|
||||
);
|
||||
return Array.isArray(data) ? data : [];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WfTemplate 驱动审批 —— 动态按钮 + 业务表单 + 按 type 分派
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -104,6 +111,30 @@ export async function fetchApprovalMeta(taskId: string): Promise<ApprovalMeta> {
|
||||
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<ApprovalMeta> {
|
||||
const { data } = await http.get<ApprovalMeta>(`/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,
|
||||
|
||||
@@ -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<string, unknown> | null {
|
||||
function buildReadonlyForm(rawSchema: unknown, dataApi: string): Record<string, unknown> | null {
|
||||
if (!rawSchema || typeof rawSchema !== 'object') return null;
|
||||
const obj = rawSchema as Record<string, unknown>;
|
||||
const inner = (obj.body && typeof obj.body === 'object' ? obj.body : obj) as Record<string, unknown>;
|
||||
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({
|
||||
</Drawer>
|
||||
|
||||
{/* 转办弹窗 */}
|
||||
{showDelegate && (
|
||||
{showDelegate && taskId && (
|
||||
<Drawer title="转办" width={360} onClose={() => setShowDelegate(false)}>
|
||||
<DelegateForm
|
||||
taskId={taskId}
|
||||
|
||||
@@ -179,11 +179,11 @@ export function AppShell() {
|
||||
};
|
||||
}, []);
|
||||
|
||||
// 转换页 amis action → 全局审批抽屉。事件 detail 只信 taskId;其余字段缺失时由 ApprovalDrawer 再查 meta 补齐。
|
||||
// 转换页 amis action → 全局审批抽屉。taskId 走办理/已办详情;businessKey/processInstanceId 走只读历史查看。
|
||||
useEffect(() => {
|
||||
const openApprovalDrawer = (event: Event) => {
|
||||
const detail = (event as CustomEvent<ApprovalDrawerRequest>).detail;
|
||||
if (!detail?.taskId) return;
|
||||
if (!detail?.taskId && !detail?.businessKey && !detail?.processInstanceId) return;
|
||||
setApprovalRequest(detail);
|
||||
};
|
||||
window.addEventListener(OPEN_APPROVAL_DRAWER_EVENT, openApprovalDrawer);
|
||||
|
||||
Reference in New Issue
Block a user