fix(approval): wire workflow task drawer actions
This commit is contained in:
12
src/amis/approvalActionEvents.ts
Normal file
12
src/amis/approvalActionEvents.ts
Normal 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;
|
||||
}
|
||||
86
src/amis/registerApprovalAction.test.ts
Normal file
86
src/amis/registerApprovalAction.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
81
src/amis/registerApprovalAction.ts
Normal file
81
src/amis/registerApprovalAction.ts
Normal 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;
|
||||
}
|
||||
@@ -43,23 +43,10 @@ export function collectLeaves(items: MenuItem[], breadcrumb: string[] = []): Men
|
||||
return out;
|
||||
}
|
||||
|
||||
// 系统管理(现代层标准页,F1-5 入库 sys_page_schema,不在老菜单表里)。
|
||||
// F1 阶段对所有登录用户可见;F2 接入模块授权后改为按权限下发。
|
||||
const SYS_ADMIN_ROOT: MenuRoot = {
|
||||
id: 'SYS_ADMIN',
|
||||
name: '系统管理',
|
||||
seq: 999,
|
||||
children: [
|
||||
{ id: 'sys-admin-user', name: '用户管理', seq: 1, templateId: 'sys-admin-user', children: [] },
|
||||
{ id: 'sys-admin-role', name: '角色管理', seq: 2, templateId: 'sys-admin-role', children: [] },
|
||||
{ id: 'sys-admin-depart', name: '部门管理', seq: 3, templateId: 'sys-admin-depart', children: [] },
|
||||
{ id: 'sys-admin-menu', name: '菜单管理', seq: 4, templateId: 'sys-admin-menu', children: [] },
|
||||
{ id: 'sys-admin-filterrule', name: '数据权限规则', seq: 5, templateId: 'sys-admin-filterrule', children: [] },
|
||||
{ id: 'sys-admin-assign', name: '权限分配', seq: 6, templateId: 'sys-admin-assign', children: [] },
|
||||
],
|
||||
};
|
||||
|
||||
export async function fetchMenu(): Promise<MenuResponse> {
|
||||
// 只用真菜单(/api/menu = 老 sys_menu 转换页)。原硬编码的「系统管理」demo 组
|
||||
// (sys-admin-* 手搓 RBAC 页)已弃用——其功能由真菜单里的转换页覆盖
|
||||
// (用户管理→629463ae / 角色管理→498c077c / 用户组管理→5078945c / 组织管理 / 功能管理 等)。
|
||||
const resp = await http.get<MenuResponse>('/api/menu');
|
||||
return { roots: [...resp.data.roots, SYS_ADMIN_ROOT] };
|
||||
return { roots: resp.data.roots };
|
||||
}
|
||||
|
||||
@@ -9,8 +9,10 @@
|
||||
// 此处替换为自定义 fetcher,统一注入 Bearer(由 http 拦截器完成)并复用宿主 axios,
|
||||
// 后端 /api/entity 已是 amis CRUD 格式 {status,msg,data:{items,total}},无需再适配响应。
|
||||
// ============================================================================
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { http } from '../api/http';
|
||||
import { APPROVAL_COMPLETED_EVENT } from '../amis/approvalActionEvents';
|
||||
import { registerApprovalAction } from '../amis/registerApprovalAction';
|
||||
import { registerReplayAction } from '../amis/registerReplayAction';
|
||||
import { getProfile } from '../auth/token';
|
||||
|
||||
@@ -31,6 +33,13 @@ interface AmisFetcherOptions {
|
||||
export function AmisRenderer({ schema }: AmisRendererProps) {
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
const instanceRef = useRef<{ unmount?: () => void } | null>(null);
|
||||
const [reloadSeq, setReloadSeq] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
const reload = () => setReloadSeq((n) => n + 1);
|
||||
window.addEventListener(APPROVAL_COMPLETED_EVENT, reload);
|
||||
return () => window.removeEventListener(APPROVAL_COMPLETED_EVENT, reload);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!rootRef.current || typeof amisRequire !== 'function') return;
|
||||
@@ -44,6 +53,11 @@ export function AmisRenderer({ schema }: AmisRendererProps) {
|
||||
} catch {
|
||||
// amis-core 未暴露 registerAction 时静默降级(onEvent 重放不可用,不阻断渲染)
|
||||
}
|
||||
try {
|
||||
registerApprovalAction(amisRequire('amis'));
|
||||
} catch {
|
||||
// amis-core 未暴露 registerAction 时静默降级(审批抽屉入口不可用,不阻断渲染)
|
||||
}
|
||||
|
||||
// 自定义 fetcher:复用宿主 axios(已带 Bearer 注入 + 401 跳登录拦截)
|
||||
const fetcher = (options: AmisFetcherOptions) => {
|
||||
@@ -89,7 +103,7 @@ export function AmisRenderer({ schema }: AmisRendererProps) {
|
||||
instanceRef.current?.unmount?.();
|
||||
instanceRef.current = null;
|
||||
};
|
||||
}, [schema]);
|
||||
}, [schema, reloadSeq]);
|
||||
|
||||
return <div ref={rootRef} style={{ height: '100%' }} />;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ export interface ApprovalDrawerProps {
|
||||
businessTable?: string;
|
||||
/** 业务摘要标题字段(无可渲染 schema 时的极简降级展示)。 */
|
||||
businessSummary?: Record<string, unknown>;
|
||||
/** 已办/历史任务查看:只展示业务单据与轨迹,不暴露办理动作。 */
|
||||
readOnly?: boolean;
|
||||
onClose: () => void;
|
||||
/** 办理成功后回调(刷新待办列表/铃铛)。 */
|
||||
onCompleted: () => void;
|
||||
@@ -192,6 +194,7 @@ export function ApprovalDrawer({
|
||||
businessKey,
|
||||
businessTable,
|
||||
businessSummary,
|
||||
readOnly = false,
|
||||
onClose,
|
||||
onCompleted,
|
||||
}: ApprovalDrawerProps) {
|
||||
@@ -204,11 +207,17 @@ export function ApprovalDrawer({
|
||||
const [traceNodes, setTraceNodes] = useState<TraceNode[]>([]);
|
||||
const [traceLoading, setTraceLoading] = useState(false);
|
||||
const [showDelegate, setShowDelegate] = useState(false);
|
||||
const effectiveProcessInstanceId = processInstanceId ?? meta?.processInstanceId ?? undefined;
|
||||
const effectiveBusinessKey = businessKey ?? meta?.businessKey ?? undefined;
|
||||
const effectiveBusinessTable = businessTable ?? meta?.businessTable ?? undefined;
|
||||
|
||||
// 审批元数据(按钮/脚本/业务表单全来自 WfTemplate)
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
setMetaLoading(true);
|
||||
setMeta(null);
|
||||
setFormSchema(null);
|
||||
setTraceNodes([]);
|
||||
fetchApprovalMeta(taskId)
|
||||
.then((m) => alive && setMeta(m))
|
||||
.catch(() => alive && setMeta(null))
|
||||
@@ -218,15 +227,15 @@ export function ApprovalDrawer({
|
||||
|
||||
// 流程轨迹
|
||||
useEffect(() => {
|
||||
if (!processInstanceId) return;
|
||||
if (!effectiveProcessInstanceId) return;
|
||||
let alive = true;
|
||||
setTraceLoading(true);
|
||||
fetchTrace(processInstanceId)
|
||||
fetchTrace(effectiveProcessInstanceId)
|
||||
.then((n) => alive && setTraceNodes(n))
|
||||
.catch(() => alive && setTraceNodes([]))
|
||||
.finally(() => alive && setTraceLoading(false));
|
||||
return () => { alive = false; };
|
||||
}, [processInstanceId]);
|
||||
}, [effectiveProcessInstanceId]);
|
||||
|
||||
// 业务单据只读表单(统一 editform schema;数据走 /api/task/{id}/business-data 由后端载入)
|
||||
useEffect(() => {
|
||||
@@ -260,9 +269,9 @@ export function ApprovalDrawer({
|
||||
actionType: btn.type,
|
||||
buttonId: btn.id,
|
||||
comment: comment.trim() || undefined,
|
||||
businessKey,
|
||||
processInstanceId,
|
||||
tableName: meta?.businessTable ?? businessTable,
|
||||
businessKey: effectiveBusinessKey,
|
||||
processInstanceId: effectiveProcessInstanceId,
|
||||
tableName: effectiveBusinessTable,
|
||||
});
|
||||
toast(`已${btn.label || '办理'}`);
|
||||
onCompleted();
|
||||
@@ -278,7 +287,7 @@ export function ApprovalDrawer({
|
||||
// 显示用:友好中文名(施工方案审批)优先;无则不显裸表名,只显「业务单据」
|
||||
const tableLabel = meta?.businessTableName ?? undefined;
|
||||
|
||||
const footer = (
|
||||
const footer = readOnly ? undefined : (
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 10, width: '100%' }}>
|
||||
<textarea
|
||||
data-testid="approval-comment"
|
||||
@@ -322,7 +331,7 @@ export function ApprovalDrawer({
|
||||
return (
|
||||
<>
|
||||
<Drawer
|
||||
title={taskName || meta?.taskName || '审批办理'}
|
||||
title={taskName || meta?.taskName || (readOnly ? '审批详情' : '审批办理')}
|
||||
/* 自适应宽度:表单大时给到更宽(最宽 1080px),小屏回落(最窄 600px) */
|
||||
width="clamp(600px, 58vw, 1080px)"
|
||||
onClose={onClose}
|
||||
|
||||
@@ -14,6 +14,12 @@ import { collectLeaves, fetchMenu } from '../api/menu';
|
||||
import type { MenuLeaf } from '../api/menu';
|
||||
import { fetchTodo } from '../api/todo';
|
||||
import { fetchUnreadCount } from '../api/notice';
|
||||
import {
|
||||
APPROVAL_COMPLETED_EVENT,
|
||||
OPEN_APPROVAL_DRAWER_EVENT,
|
||||
type ApprovalDrawerRequest,
|
||||
} from '../amis/approvalActionEvents';
|
||||
import { ApprovalDrawer } from '../components/ApprovalDrawer';
|
||||
import { ToastHost } from '../components/ui/Toast';
|
||||
import { Topbar } from './Topbar';
|
||||
import { SideMenu } from './SideMenu';
|
||||
@@ -72,6 +78,7 @@ export function AppShell() {
|
||||
// F2-11 消息未读数
|
||||
const [noticeBadge, setNoticeBadge] = useState<number | undefined>(undefined);
|
||||
const [showNotice, setShowNotice] = useState(false);
|
||||
const [approvalRequest, setApprovalRequest] = useState<ApprovalDrawerRequest | null>(null);
|
||||
const pollTimer = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const noticePollTimer = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
|
||||
@@ -165,6 +172,30 @@ export function AppShell() {
|
||||
};
|
||||
}, []);
|
||||
|
||||
// 转换页 amis action → 全局审批抽屉。事件 detail 只信 taskId;其余字段缺失时由 ApprovalDrawer 再查 meta 补齐。
|
||||
useEffect(() => {
|
||||
const openApprovalDrawer = (event: Event) => {
|
||||
const detail = (event as CustomEvent<ApprovalDrawerRequest>).detail;
|
||||
if (!detail?.taskId) return;
|
||||
setApprovalRequest(detail);
|
||||
};
|
||||
window.addEventListener(OPEN_APPROVAL_DRAWER_EVENT, openApprovalDrawer);
|
||||
return () => window.removeEventListener(OPEN_APPROVAL_DRAWER_EVENT, openApprovalDrawer);
|
||||
}, []);
|
||||
|
||||
const handleApprovalCompleted = useCallback(() => {
|
||||
const detail = approvalRequest;
|
||||
setApprovalRequest(null);
|
||||
fetchTodo(0, 200)
|
||||
.then((full) => {
|
||||
if (!full.unavailable) setTodoBadge(full.items.length || undefined);
|
||||
})
|
||||
.catch(() => setTodoBadge(undefined));
|
||||
window.setTimeout(() => {
|
||||
window.dispatchEvent(new CustomEvent(APPROVAL_COMPLETED_EVENT, { detail }));
|
||||
}, 0);
|
||||
}, [approvalRequest]);
|
||||
|
||||
// F2-13:回填 Tab 标题(PageRenderPage 拿到 schema title 后调用)
|
||||
const updateTabTitle = useCallback((path: string, title: string) => {
|
||||
setTabs((prev) =>
|
||||
@@ -292,6 +323,20 @@ export function AppShell() {
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
|
||||
{approvalRequest ? (
|
||||
<ApprovalDrawer
|
||||
taskId={approvalRequest.taskId}
|
||||
taskName={approvalRequest.taskName}
|
||||
processInstanceId={approvalRequest.processInstanceId}
|
||||
businessKey={approvalRequest.businessKey}
|
||||
businessTable={approvalRequest.businessTable}
|
||||
businessSummary={approvalRequest.businessSummary}
|
||||
readOnly={approvalRequest.readOnly}
|
||||
onClose={() => setApprovalRequest(null)}
|
||||
onCompleted={handleApprovalCompleted}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ table.tbl { border-collapse: separate; border-spacing: 0; width: 100%; font-size
|
||||
/* 弹层公共 */
|
||||
.mask {
|
||||
position: fixed; inset: 0; background: rgba(0,0,0,.35);
|
||||
display: flex; align-items: center; justify-content: center; z-index: 200;
|
||||
display: flex; align-items: center; justify-content: center; z-index: 2000;
|
||||
}
|
||||
.dlg {
|
||||
background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-3);
|
||||
@@ -228,7 +228,7 @@ table.tbl { border-collapse: separate; border-spacing: 0; width: 100%; font-size
|
||||
|
||||
/* 抽屉 */
|
||||
.drawer {
|
||||
background: var(--surface); box-shadow: var(--shadow-3); z-index: 200;
|
||||
background: var(--surface); box-shadow: var(--shadow-3); z-index: 1;
|
||||
display: flex; flex-direction: column;
|
||||
animation: slideIn .2s cubic-bezier(.2,.9,.3,1);
|
||||
}
|
||||
@@ -238,7 +238,7 @@ table.tbl { border-collapse: separate; border-spacing: 0; width: 100%; font-size
|
||||
}
|
||||
|
||||
/* Toast */
|
||||
.toast-wrap { position: fixed; bottom: 24px; right: 24px; z-index: 300; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
|
||||
.toast-wrap { position: fixed; bottom: 24px; right: 24px; z-index: 3000; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
|
||||
.toast {
|
||||
display: flex; align-items: center; gap: 8px;
|
||||
background: var(--surface); border: 1px solid var(--line);
|
||||
|
||||
Reference in New Issue
Block a user