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 const APPROVAL_COMPLETED_EVENT = 'eip4:approval-completed';
|
||||||
|
|
||||||
export interface ApprovalDrawerRequest {
|
export interface ApprovalDrawerRequest {
|
||||||
taskId: string;
|
taskId?: string;
|
||||||
taskName?: string;
|
taskName?: string;
|
||||||
processInstanceId?: string;
|
processInstanceId?: string;
|
||||||
businessKey?: 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;
|
let captured: RunCtx | null = null;
|
||||||
registerApprovalAction({
|
registerApprovalAction({
|
||||||
registerAction: (_type: string, action: RunCtx) => {
|
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) {
|
async run(action: OpenApprovalActionArg, _renderer: AmisRenderer, _event: AmisEvent) {
|
||||||
const args = action?.args ?? {};
|
const args = action?.args ?? {};
|
||||||
const taskId = asString(args.taskId);
|
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 mode = asString(args.mode);
|
||||||
const detail: ApprovalDrawerRequest = {
|
const detail: ApprovalDrawerRequest = {
|
||||||
taskId,
|
taskId,
|
||||||
taskName: asString(args.taskName),
|
taskName: asString(args.taskName),
|
||||||
processInstanceId: asString(args.processInstanceId),
|
processInstanceId,
|
||||||
businessKey: asString(args.businessKey),
|
businessKey,
|
||||||
businessTable: asString(args.businessTable),
|
businessTable: asString(args.businessTable),
|
||||||
businessSummary: asRecord(args.businessSummary),
|
businessSummary: asRecord(args.businessSummary),
|
||||||
readOnly: asBoolean(args.readOnly) || asBoolean(args.readonly) || mode === 'read',
|
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 : [];
|
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 分派
|
// WfTemplate 驱动审批 —— 动态按钮 + 业务表单 + 按 type 分派
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -104,6 +111,30 @@ export async function fetchApprovalMeta(taskId: string): Promise<ApprovalMeta> {
|
|||||||
return data;
|
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(
|
export async function submitApprovalAction(
|
||||||
taskId: string,
|
taskId: string,
|
||||||
req: ApprovalActionRequest,
|
req: ApprovalActionRequest,
|
||||||
|
|||||||
@@ -10,11 +10,19 @@ import { Drawer } from './ui/Dialog';
|
|||||||
import { toast } from './ui/Toast';
|
import { toast } from './ui/Toast';
|
||||||
import { AmisRenderer } from './AmisRenderer';
|
import { AmisRenderer } from './AmisRenderer';
|
||||||
import { http } from '../api/http';
|
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';
|
import type { TraceNode, ApprovalMeta, ApprovalButton } from '../api/approval';
|
||||||
|
|
||||||
export interface ApprovalDrawerProps {
|
export interface ApprovalDrawerProps {
|
||||||
taskId: string;
|
taskId?: string;
|
||||||
taskName?: string;
|
taskName?: string;
|
||||||
processInstanceId?: string;
|
processInstanceId?: string;
|
||||||
businessKey?: string;
|
businessKey?: string;
|
||||||
@@ -56,13 +64,13 @@ function buttonClass(type: string): { className: string; style?: React.CSSProper
|
|||||||
* (含子表、引用列 id→名,对齐老审批页),4s 超时硬上限防挂死、超时退化 loadPOJO。前端无需关心 load/entity 之分。
|
* (含子表、引用列 id→名,对齐老审批页),4s 超时硬上限防挂死、超时退化 loadPOJO。前端无需关心 load/entity 之分。
|
||||||
* 返回 amis 单记录契约 {status:0,data:{...record}},表单字段(name=列名)直接绑定。
|
* 返回 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;
|
if (!rawSchema || typeof rawSchema !== 'object') return null;
|
||||||
const obj = rawSchema as Record<string, unknown>;
|
const obj = rawSchema as Record<string, unknown>;
|
||||||
const inner = (obj.body && typeof obj.body === 'object' ? obj.body : obj) as Record<string, unknown>;
|
const inner = (obj.body && typeof obj.body === 'object' ? obj.body : obj) as Record<string, unknown>;
|
||||||
return {
|
return {
|
||||||
type: 'service',
|
type: 'service',
|
||||||
api: `get:/api/task/${taskId}/business-data`,
|
api: dataApi,
|
||||||
body: { ...inner, static: true, actions: [], submitText: '' },
|
body: { ...inner, static: true, actions: [], submitText: '' },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -210,6 +218,11 @@ export function ApprovalDrawer({
|
|||||||
const effectiveProcessInstanceId = processInstanceId ?? meta?.processInstanceId ?? undefined;
|
const effectiveProcessInstanceId = processInstanceId ?? meta?.processInstanceId ?? undefined;
|
||||||
const effectiveBusinessKey = businessKey ?? meta?.businessKey ?? undefined;
|
const effectiveBusinessKey = businessKey ?? meta?.businessKey ?? undefined;
|
||||||
const effectiveBusinessTable = businessTable ?? meta?.businessTable ?? undefined;
|
const effectiveBusinessTable = businessTable ?? meta?.businessTable ?? undefined;
|
||||||
|
const historyParams = {
|
||||||
|
businessKey: effectiveBusinessKey,
|
||||||
|
processInstanceId: effectiveProcessInstanceId,
|
||||||
|
businessTable: effectiveBusinessTable,
|
||||||
|
};
|
||||||
|
|
||||||
// 审批元数据(按钮/脚本/业务表单全来自 WfTemplate)
|
// 审批元数据(按钮/脚本/业务表单全来自 WfTemplate)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -218,24 +231,30 @@ export function ApprovalDrawer({
|
|||||||
setMeta(null);
|
setMeta(null);
|
||||||
setFormSchema(null);
|
setFormSchema(null);
|
||||||
setTraceNodes([]);
|
setTraceNodes([]);
|
||||||
fetchApprovalMeta(taskId)
|
const loader = taskId
|
||||||
|
? fetchApprovalMeta(taskId)
|
||||||
|
: fetchApprovalHistoryMeta({ businessKey, processInstanceId, businessTable });
|
||||||
|
loader
|
||||||
.then((m) => alive && setMeta(m))
|
.then((m) => alive && setMeta(m))
|
||||||
.catch(() => alive && setMeta(null))
|
.catch(() => alive && setMeta(null))
|
||||||
.finally(() => alive && setMetaLoading(false));
|
.finally(() => alive && setMetaLoading(false));
|
||||||
return () => { alive = false; };
|
return () => { alive = false; };
|
||||||
}, [taskId]);
|
}, [taskId, businessKey, processInstanceId, businessTable]);
|
||||||
|
|
||||||
// 流程轨迹
|
// 流程轨迹
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!effectiveProcessInstanceId) return;
|
if (!effectiveProcessInstanceId && !effectiveBusinessKey) return;
|
||||||
let alive = true;
|
let alive = true;
|
||||||
setTraceLoading(true);
|
setTraceLoading(true);
|
||||||
fetchTrace(effectiveProcessInstanceId)
|
const loader = effectiveProcessInstanceId
|
||||||
|
? fetchTrace(effectiveProcessInstanceId)
|
||||||
|
: fetchTraceByBusiness(effectiveBusinessKey!);
|
||||||
|
loader
|
||||||
.then((n) => alive && setTraceNodes(n))
|
.then((n) => alive && setTraceNodes(n))
|
||||||
.catch(() => alive && setTraceNodes([]))
|
.catch(() => alive && setTraceNodes([]))
|
||||||
.finally(() => alive && setTraceLoading(false));
|
.finally(() => alive && setTraceLoading(false));
|
||||||
return () => { alive = false; };
|
return () => { alive = false; };
|
||||||
}, [effectiveProcessInstanceId]);
|
}, [effectiveProcessInstanceId, effectiveBusinessKey]);
|
||||||
|
|
||||||
// 业务单据只读表单(统一 editform schema;数据走 /api/task/{id}/business-data 由后端载入)
|
// 业务单据只读表单(统一 editform schema;数据走 /api/task/{id}/business-data 由后端载入)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -243,12 +262,15 @@ export function ApprovalDrawer({
|
|||||||
const tpl = meta?.formTemplateId;
|
const tpl = meta?.formTemplateId;
|
||||||
if (tpl) {
|
if (tpl) {
|
||||||
setFormLoading(true);
|
setFormLoading(true);
|
||||||
|
const dataApi = taskId
|
||||||
|
? `get:/api/task/${taskId}/business-data`
|
||||||
|
: approvalHistoryBusinessDataApi(historyParams);
|
||||||
http
|
http
|
||||||
.get(`/api/page/schema/${tpl}`)
|
.get(`/api/page/schema/${tpl}`)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (!alive) return;
|
if (!alive) return;
|
||||||
const raw = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
|
const raw = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
|
||||||
setFormSchema(buildReadonlyForm(raw, taskId));
|
setFormSchema(buildReadonlyForm(raw, dataApi));
|
||||||
})
|
})
|
||||||
.catch(() => alive && setFormSchema(null))
|
.catch(() => alive && setFormSchema(null))
|
||||||
.finally(() => alive && setFormLoading(false));
|
.finally(() => alive && setFormLoading(false));
|
||||||
@@ -256,9 +278,13 @@ export function ApprovalDrawer({
|
|||||||
setFormSchema(null);
|
setFormSchema(null);
|
||||||
}
|
}
|
||||||
return () => { alive = false; };
|
return () => { alive = false; };
|
||||||
}, [meta, taskId]);
|
}, [meta, taskId, effectiveBusinessKey, effectiveProcessInstanceId, effectiveBusinessTable]);
|
||||||
|
|
||||||
const handleAction = async (btn: ApprovalButton) => {
|
const handleAction = async (btn: ApprovalButton) => {
|
||||||
|
if (!taskId) {
|
||||||
|
toast('缺少任务ID,无法办理', 'warn');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (btn.type === 'agent') {
|
if (btn.type === 'agent') {
|
||||||
setShowDelegate(true);
|
setShowDelegate(true);
|
||||||
return;
|
return;
|
||||||
@@ -387,7 +413,7 @@ export function ApprovalDrawer({
|
|||||||
</Drawer>
|
</Drawer>
|
||||||
|
|
||||||
{/* 转办弹窗 */}
|
{/* 转办弹窗 */}
|
||||||
{showDelegate && (
|
{showDelegate && taskId && (
|
||||||
<Drawer title="转办" width={360} onClose={() => setShowDelegate(false)}>
|
<Drawer title="转办" width={360} onClose={() => setShowDelegate(false)}>
|
||||||
<DelegateForm
|
<DelegateForm
|
||||||
taskId={taskId}
|
taskId={taskId}
|
||||||
|
|||||||
@@ -179,11 +179,11 @@ export function AppShell() {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// 转换页 amis action → 全局审批抽屉。事件 detail 只信 taskId;其余字段缺失时由 ApprovalDrawer 再查 meta 补齐。
|
// 转换页 amis action → 全局审批抽屉。taskId 走办理/已办详情;businessKey/processInstanceId 走只读历史查看。
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const openApprovalDrawer = (event: Event) => {
|
const openApprovalDrawer = (event: Event) => {
|
||||||
const detail = (event as CustomEvent<ApprovalDrawerRequest>).detail;
|
const detail = (event as CustomEvent<ApprovalDrawerRequest>).detail;
|
||||||
if (!detail?.taskId) return;
|
if (!detail?.taskId && !detail?.businessKey && !detail?.processInstanceId) return;
|
||||||
setApprovalRequest(detail);
|
setApprovalRequest(detail);
|
||||||
};
|
};
|
||||||
window.addEventListener(OPEN_APPROVAL_DRAWER_EVENT, openApprovalDrawer);
|
window.addEventListener(OPEN_APPROVAL_DRAWER_EVENT, openApprovalDrawer);
|
||||||
|
|||||||
Reference in New Issue
Block a user