fix(approval): wire workflow task drawer actions

This commit is contained in:
2026-06-17 20:44:52 +08:00
parent eae31b31c1
commit f7ac4cd49e
8 changed files with 264 additions and 30 deletions

View File

@@ -0,0 +1,12 @@
export const OPEN_APPROVAL_DRAWER_EVENT = 'eip4:open-approval-drawer';
export const APPROVAL_COMPLETED_EVENT = 'eip4:approval-completed';
export interface ApprovalDrawerRequest {
taskId: string;
taskName?: string;
processInstanceId?: string;
businessKey?: string;
businessTable?: string;
businessSummary?: Record<string, unknown>;
readOnly?: boolean;
}

View File

@@ -0,0 +1,86 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { OPEN_APPROVAL_DRAWER_EVENT } from './approvalActionEvents';
import { registerApprovalAction, __resetApprovalActionForTest } from './registerApprovalAction';
interface RunCtx {
run: (action: { args?: Record<string, unknown> }, renderer: unknown, event: unknown) => Promise<void>;
}
describe('registerApprovalAction', () => {
beforeEach(() => {
__resetApprovalActionForTest();
});
it('amis-core 缺 registerAction 时返回 false(静默降级)', () => {
expect(registerApprovalAction(null)).toBe(false);
expect(registerApprovalAction({})).toBe(false);
});
it('注册 openApprovalDrawer 动作(幂等)', () => {
const registerAction = vi.fn();
const core = { registerAction };
expect(registerApprovalAction(core)).toBe(true);
expect(registerAction).toHaveBeenCalledTimes(1);
expect(registerAction.mock.calls[0][0]).toBe('openApprovalDrawer');
expect(registerApprovalAction(core)).toBe(true);
expect(registerAction).toHaveBeenCalledTimes(1);
});
it('run 把 taskId 等参数派发为全局审批抽屉事件', async () => {
let captured: RunCtx | null = null;
registerApprovalAction({
registerAction: (_type: string, action: RunCtx) => {
captured = action;
},
});
expect(captured).not.toBeNull();
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: {
taskId: 'task-1',
taskName: '审核',
processInstanceId: 'pi-1',
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: 'task-1',
taskName: '审核',
processInstanceId: 'pi-1',
businessKey: 'biz-1',
businessTable: 'PM_DEMO',
businessSummary: undefined,
readOnly: true,
});
});
it('缺 taskId 时不派发事件', async () => {
let captured: RunCtx | null = null;
registerApprovalAction({
registerAction: (_type: string, action: RunCtx) => {
captured = action;
},
});
const listener = vi.fn();
window.addEventListener(OPEN_APPROVAL_DRAWER_EVENT, listener);
try {
await captured!.run({ args: { taskName: '审核' } }, {}, {});
} finally {
window.removeEventListener(OPEN_APPROVAL_DRAWER_EVENT, listener);
}
expect(listener).not.toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,81 @@
import { OPEN_APPROVAL_DRAWER_EVENT, type ApprovalDrawerRequest } from './approvalActionEvents';
interface OpenApprovalActionArg {
args?: {
taskId?: unknown;
taskName?: unknown;
processInstanceId?: unknown;
businessKey?: unknown;
businessTable?: unknown;
businessSummary?: unknown;
readOnly?: unknown;
readonly?: unknown;
mode?: unknown;
};
[key: string]: unknown;
}
interface AmisRenderer {
[key: string]: unknown;
}
interface AmisEvent {
[key: string]: unknown;
}
interface AmisCore {
registerAction?: (type: string, action: unknown) => void;
}
let registered = false;
function asString(v: unknown): string | undefined {
if (v === null || v === undefined) return undefined;
const s = String(v).trim();
return s ? s : undefined;
}
function asRecord(v: unknown): Record<string, unknown> | undefined {
return v && typeof v === 'object' && !Array.isArray(v) ? v as Record<string, unknown> : undefined;
}
function asBoolean(v: unknown): boolean {
return v === true || v === 'true' || v === 1 || v === '1';
}
/**
* 注册 converted amis 页使用的审批抽屉动作。
*
* 转换器只生成通用动作,不直接依赖 React 状态;这里把 amis click 转成应用级事件,
* 由 AppShell 挂载真实 ApprovalDrawer。
*/
export function registerApprovalAction(amisCore: AmisCore | undefined | null): boolean {
if (registered) return true;
if (!amisCore || typeof amisCore.registerAction !== 'function') return false;
amisCore.registerAction('openApprovalDrawer', {
async run(action: OpenApprovalActionArg, _renderer: AmisRenderer, _event: AmisEvent) {
const args = action?.args ?? {};
const taskId = asString(args.taskId);
if (!taskId) return;
const mode = asString(args.mode);
const detail: ApprovalDrawerRequest = {
taskId,
taskName: asString(args.taskName),
processInstanceId: asString(args.processInstanceId),
businessKey: asString(args.businessKey),
businessTable: asString(args.businessTable),
businessSummary: asRecord(args.businessSummary),
readOnly: asBoolean(args.readOnly) || asBoolean(args.readonly) || mode === 'read',
};
window.dispatchEvent(new CustomEvent<ApprovalDrawerRequest>(OPEN_APPROVAL_DRAWER_EVENT, { detail }));
},
});
registered = true;
return true;
}
export function __resetApprovalActionForTest(): void {
registered = false;
}