Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 66 additions & 10 deletions packages/flow-design/src/plugins/view/error-trigger-view.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
import React from "react";
import {GroovyScriptConvertorUtil} from "@coding-flow/flow-core";
import {Button, Form, Select, Space} from "antd";
import {Button, Form, Input, Select, Space} from "antd";
import {CodeOutlined, ReloadOutlined} from "@ant-design/icons";
import {ErrorTriggerViewPlugin, VIEW_KEY} from "@/plugins/error-trigger-view-type";
import {ViewBindPlugin} from "@coding-flow/flow-core";
import {SCRIPT_DEFAULT_ERROR_TRIGGER} from "@/script-components/default-script";
import {
DEFAULT_ERROR_TRIGGER_MESSAGE,
SCRIPT_DEFAULT_ERROR_TRIGGER
} from "@/script-components/default-script";
import {useNodeRouterManager} from "@/components/design-panel/hooks/use-node-router-manager";
import {useNodeRenderContext} from "@/components/design-editor/hooks/use-node-render-context";
import {ErrorTriggerScriptUtils} from "@/script-components/services/node-error-trigger";
import {useScriptMetaData} from "@/script-components/hooks/use-script-meta-data";
import styles from "./error_trigger_view.module.scss";

/**
* 异常处理方式(issue #296)
*
* 三种语义:跳转节点、跳转用户(暂未开放)、异常提示。
* 异常提示不推进流程,提交时由后端阻断并把文案返回给用户。
*/
export const ERROR_TRIGGER_TYPE_OPTIONS = [
{label: '跳转节点', value: 'node'},
{label: '跳转用户', value: 'user'},
{label: '异常提示', value: 'prompt'},
];

/**
* 异常配置视图
* <p>
* 支持三种处理方式(issue #296):跳转节点、跳转用户(暂未开放)、异常提示。
* 异常提示不会推进流程,提交时由后端阻断并把文案返回给用户。
*
* @param props
* @constructor
*/
export const ErrorTriggerPluginView: React.FC<ErrorTriggerViewPlugin> = (props) => {
const ErrorTriggerPluginViewComponent = ViewBindPlugin.getInstance().get(VIEW_KEY);
const [type, setType] = React.useState('node');

const nodeRouterManager = useNodeRouterManager();
const {node} = useNodeRenderContext();
const data = useScriptMetaData(props.script);

const mappingData = nodeRouterManager.mapping(data);

// 元数据缺失时按脚本内容推断:手写的 prompt 脚本不能回显成「跳转节点」
const resolveType = () => {
if (mappingData.type) {
return mappingData.type;
}
return ErrorTriggerScriptUtils.isPromptScript(props.script) ? 'prompt' : 'node';
};

const [type, setType] = React.useState<string>(resolveType());
const [message, setMessage] = React.useState<string>(
mappingData.message || ErrorTriggerScriptUtils.getPromptMessage(props.script)
);

// 脚本被外部替换(高级配置 / 重置)时同步表单展示,避免视图状态与脚本不一致
React.useEffect(() => {
setType(resolveType());
setMessage(mappingData.message || ErrorTriggerScriptUtils.getPromptMessage(props.script));
}, [props.script]);

if (ErrorTriggerPluginViewComponent) {
return (
<ErrorTriggerPluginViewComponent {...props} />
Expand All @@ -36,17 +73,21 @@ export const ErrorTriggerPluginView: React.FC<ErrorTriggerViewPlugin> = (props)
<div>
<Form
initialValues={{
...mappingData
...mappingData,
type: resolveType()
}}
>
{!props.nodeOnly && (
<Form.Item name={"type"} label={"触发类型"}>
<Form.Item name={"type"} label={"异常处理"}>
<Select
options={[
{label: '跳转节点', value: 'node'},
{label: '跳转用户', value: 'user'},
]}
onChange={setType}
options={ERROR_TRIGGER_TYPE_OPTIONS}
onChange={(value) => {
setType(value);
if (value === 'prompt') {
props.onChange(ErrorTriggerScriptUtils.setPrompt(
message || DEFAULT_ERROR_TRIGGER_MESSAGE));
}
}}
/>
</Form.Item>
)}
Expand All @@ -66,6 +107,21 @@ export const ErrorTriggerPluginView: React.FC<ErrorTriggerViewPlugin> = (props)
</Form.Item>
)}

{!props.nodeOnly && type === "prompt" && (
<Form.Item label={"提示信息"}>
<Input.TextArea
value={message}
rows={3}
placeholder={DEFAULT_ERROR_TRIGGER_MESSAGE}
onChange={(event) => {
const value = event.target.value;
setMessage(value);
props.onChange(ErrorTriggerScriptUtils.setPrompt(value));
}}
/>
</Form.Item>
)}

{!props.nodeOnly && type === "user" && (
<div>选择人员:暂不支持</div>
)}
Expand Down
13 changes: 9 additions & 4 deletions packages/flow-design/src/script-components/default-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ def run(request){
`


// 默认异常触发,回退至开始节点
// 默认异常提示文案(issue #296)
export const DEFAULT_ERROR_TRIGGER_MESSAGE =
'流程未匹配到审批人,无法继续处理,请联系流程管理员';

// 默认异常触发,异常提示(issue #296)
// 不再默认回退到开始节点:回退会让流程停留在发起人手里,但发起人看到的是「发起成功」
export const SCRIPT_DEFAULT_ERROR_TRIGGER =
`// @SCRIPT_TITLE 回退至开始节点
// @SCRIPT_META {"type":"node","node":"START"}
`// @SCRIPT_TITLE 异常提示
// @SCRIPT_META {"type":"prompt","message":"${DEFAULT_ERROR_TRIGGER_MESSAGE}"}
def run(request){
return request.getStartNode().getId();
return [type:'prompt', message:'${DEFAULT_ERROR_TRIGGER_MESSAGE}']
}
`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {GroovyScriptConvertorUtil} from "@coding-flow/flow-core";

/**
* 错误异常处理脚本
*/
Expand All @@ -11,4 +13,63 @@ def run(request){
}`
};

}
/**
* 异常提示(issue #296)
*
* 不跳转、不兜底,提交时阻断并把文案交给业务层展示。
* 文案需要转义反斜杠、单引号与换行,避免生成的 Groovy 脚本语法错误;
* 同时写入 SCRIPT_META,保证再次打开配置时输入框能回显原文案。
*/
public static setPrompt(message: string) {
const raw = message || '';
const text = raw
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(/\r?\n/g, '\\n');
const meta = JSON.stringify({type: 'prompt', message: raw});
return `// @SCRIPT_TITLE 异常提示
// @SCRIPT_META ${meta}
def run(request){
return [type:'prompt', message:'${text}']
}`
};

/**
* 判断脚本是否为异常提示(issue #296)
*
* 兼容「高级配置」手写脚本:这类脚本可能没有 SCRIPT_META.type,
* 只能从返回值 `[type:'prompt', message:'...']` 识别。
*/
public static isPromptScript(script: string): boolean {
return /type\s*:\s*['"]prompt['"]/.test(script || '');
}

/**
* 解析异常提示文案(issue #296)
*
* 优先读 SCRIPT_META.message(设计器生成的新脚本),
* 其次从脚本正文提取,兼容历史脚本与「高级配置」手写的脚本。
*/
public static getPromptMessage(script: string): string {
const meta = GroovyScriptConvertorUtil.getScriptMeta(script);
if (meta) {
try {
const data = JSON.parse(meta);
if (data && typeof data.message === 'string') {
return data.message;
}
} catch (e) {
// 元数据非法时退化为正文解析
}
}
const match = (script || '').match(/message\s*:\s*(['"])((?:\\[\s\S]|(?!\1)[^\\])*)\1/);
if (match) {
return match[2]
.replace(/\\n/g, '\n')
.replace(/\\'/g, "'")
.replace(/\\\\/g, '\\');
}
return '';
}

}
99 changes: 99 additions & 0 deletions packages/flow-design/tests/components/error_trigger_view.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {describe, expect, it, rs} from '@rstest/core';
import {render, screen} from '@testing-library/react';
import React from 'react';

// antd 的响应式栅格 / 虚拟滚动在 jsdom 下依赖浏览器 API
if (!window.matchMedia) {
window.matchMedia = ((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: () => undefined,
removeListener: () => undefined,
addEventListener: () => undefined,
removeEventListener: () => undefined,
dispatchEvent: () => false,
})) as any;
}

if (!(globalThis as any).ResizeObserver) {
(globalThis as any).ResizeObserver = class {
observe() {
}

unobserve() {
}

disconnect() {
}
};
}

/**
* 设计器「异常配置」视图(issue #296)
*
* 异常处理方式必须是三种:跳转节点 / 跳转用户(未开放)/ 异常提示。
* 仅支持前两种时,业务无法在流程设计阶段配置「取不到审批人时阻断并提示」。
*/
rs.mock('@/components/design-panel/hooks/use-node-router-manager', () => ({
useNodeRouterManager: () => ({
mapping: (data: any) => data,
getBackNodes: () => [{label: '开始节点', value: 'start'}],
}),
}));

rs.mock('@/components/design-editor/hooks/use-node-render-context', () => ({
useNodeRenderContext: () => ({node: {id: 'node-1'}}),
}));

import {
ERROR_TRIGGER_TYPE_OPTIONS,
ErrorTriggerPluginView
} from '@/plugins/view/error-trigger-view';

const renderView = (script: string) => {
const onChange = rs.fn();
const result = render(
<ErrorTriggerPluginView
type={'ERROR_TRIGGER' as any}
script={script}
variables={[]}
onChange={onChange}
/>
);
return {onChange, ...result};
};

describe('异常配置视图', () => {

it('异常处理方式应包含「异常提示」', () => {
const values = ERROR_TRIGGER_TYPE_OPTIONS.map(item => item.value);

expect(values).toEqual(['node', 'user', 'prompt']);
expect(ERROR_TRIGGER_TYPE_OPTIONS.map(item => item.label)).toContain('异常提示');
});

it('打开异常提示脚本时应回显提示文案', () => {
const script = `// @SCRIPT_TITLE 异常提示
// @SCRIPT_META {"type":"prompt","message":"没有可用审批人"}
def run(request){
return [type:'prompt', message:'没有可用审批人']
}`;

renderView(script);

expect(screen.getByText('异常处理')).toBeTruthy();
expect(screen.getByDisplayValue('没有可用审批人')).toBeTruthy();
});

it('兼容无元数据的历史脚本回显', () => {
const script = `// @SCRIPT_TITLE 异常提示
def run(request){
return [type:'prompt', message:'请检查审批人配置']
}`;

renderView(script);

expect(screen.getByDisplayValue('请检查审批人配置')).toBeTruthy();
});
});
74 changes: 74 additions & 0 deletions packages/flow-design/tests/script/error_trigger_prompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {describe, expect, it} from '@rstest/core';
import {GroovyScriptConvertorUtil} from '@coding-flow/flow-core';
import {ErrorTriggerScriptUtils} from '@/script-components/services/node-error-trigger';
import {
DEFAULT_ERROR_TRIGGER_MESSAGE,
SCRIPT_DEFAULT_ERROR_TRIGGER
} from '@/script-components/default-script';

/**
* 异常触发脚本(issue #296)
*
* 默认行为从「回退至开始节点」改为「异常提示」:不推进流程,提交时由后端阻断并返回文案。
*/
describe('ErrorTriggerScriptUtils', () => {

it('默认异常触发脚本应为异常提示,而不是回退开始节点', () => {
const meta = JSON.parse(GroovyScriptConvertorUtil.getScriptMeta(SCRIPT_DEFAULT_ERROR_TRIGGER) as string);

expect(meta.type).toBe('prompt');
expect(SCRIPT_DEFAULT_ERROR_TRIGGER).toContain(DEFAULT_ERROR_TRIGGER_MESSAGE);
expect(SCRIPT_DEFAULT_ERROR_TRIGGER).not.toContain('getStartNode');
});

it('setPrompt 应生成可被设计器回显的提示脚本', () => {
const script = ErrorTriggerScriptUtils.setPrompt('当前节点没有可用审批人');
const meta = JSON.parse(GroovyScriptConvertorUtil.getScriptMeta(script) as string);

expect(meta.type).toBe('prompt');
expect(script).toContain('def run');
expect(script).toContain("type:'prompt'");
expect(script).toContain('当前节点没有可用审批人');
});

it('setPrompt 应转义单引号与换行,避免生成的 Groovy 脚本语法错误', () => {
const script = ErrorTriggerScriptUtils.setPrompt("流程'卡住'\n请检查");

expect(script).toContain("\\'");
expect(script).toContain('\\n');
// 文案中的换行不能变成脚本换行
expect(script.split('\n').length).toBeLessThan(10);
});

it('setPrompt 返回空文案时仍生成合法的提示脚本', () => {
const script = ErrorTriggerScriptUtils.setPrompt('');

expect(script).toContain("type:'prompt'");
expect(script).toContain("message:''");
});

it('setPrompt 应把文案写入元数据,保证重新打开时能回显', () => {
const script = ErrorTriggerScriptUtils.setPrompt("流程'卡住',请联系管理员");
const meta = JSON.parse(GroovyScriptConvertorUtil.getScriptMeta(script) as string);

expect(meta.type).toBe('prompt');
expect(meta.message).toBe("流程'卡住',请联系管理员");
expect(ErrorTriggerScriptUtils.getPromptMessage(script)).toBe("流程'卡住',请联系管理员");
});

it('getPromptMessage 应兼容未写元数据的历史脚本与手写脚本', () => {
const legacy = `// @SCRIPT_TITLE 异常提示
// @SCRIPT_META {"type":"prompt"}
def run(request){
return [type:'prompt', message:'没有可用审批人']
}`;

expect(ErrorTriggerScriptUtils.getPromptMessage(legacy)).toBe('没有可用审批人');
expect(ErrorTriggerScriptUtils.getPromptMessage('')).toBe('');
});

it('默认异常触发脚本应能回显默认文案', () => {
expect(ErrorTriggerScriptUtils.getPromptMessage(SCRIPT_DEFAULT_ERROR_TRIGGER))
.toBe(DEFAULT_ERROR_TRIGGER_MESSAGE);
});
});
Loading