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
4 changes: 2 additions & 2 deletions docs/Integration/extension-points.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ IFlowAction#run

| 类 | 用途 |
|---|---|
| `NodeStrategyManager` | 节点全部策略的门面:`loadOperators(session)`、`getMaxOperatorCount()`、`getOperatorSelectType()`、`generateTitle(session)`、`errorTrigger(session)`、`getFieldPermissions()`、`isAdviceRequired()` 等 |
| `NodeStrategyManager` | 节点全部策略的门面:`loadOperators(session)`、`getMaxOperatorCount()`、`getOperatorSelectType()`、`generateTitle(session)`、`errorTrigger(session)`(返回 `ErrorThrow`,含 `NODE` / `OPERATORS` / `PROMPT` 三种处理方式)、`getFieldPermissions()`、`isAdviceRequired()` 等 |
| `ActionManager` | 动作管理:`filterActions(session)`、`getActionById(id)`、`getActionByType(type)`、`verifySession(session)` |
| `OperatorManager` | 操作人集合:`isEmpty()`、`match(operator)`、`getOperator(userId)` |
| `WorkflowStrategyManager` | 流程策略:`isEnableInterfere()`、`verifyOperator` |
Expand All @@ -157,7 +157,7 @@ IFlowAction#run

| 子类 | 典型工厂方法与错误场景 |
|---|---|
| `FlowValidationException` | `fieldReadOnly`(字段只读)、`operatorCountExceeded(nodeId, max)`(超出最大可选人数)、`operatorOutOfRange`(超出可选范围)、`required` 等 |
| `FlowValidationException` | `fieldReadOnly`(字段只读)、`operatorCountExceeded(nodeId, max)`(超出最大可选人数)、`operatorOutOfRange`(超出可选范围)、`nextNodeOperatorEmpty(nodeId, nodeName, message)`(下一节点无审批人且配置为异常提示,issue #296)、`required` 等 |
| `FlowExecutionException` | `scriptExecutionError`、`routerNodeNotFound`、`operatorCountExceeded`、`invalidJumpTarget(nodeType)`、`errorTriggerLoop`、`subProcessLoop`、`addAuditOperatorAlreadyInTodo`、`reduceAuditSingleApprover`、`reduceAuditTargetNotFound` 等 |
| `FlowStateException` | `repositoryNotRegistered`、`recordAlreadyDone`、`operatorNotMatch`、`workflowAlreadyDisable`、`recordNotSupportRevoke` 等 |
| `FlowNotFoundException` | `workflow(workCode)`、`record(recordId)`、`node(nodeId)`、`operator(operatorId)`、`action(actionId)` |
Expand Down
12 changes: 12 additions & 0 deletions docs/Integration/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ operatorIdentity → 存在 → 校验后作为当前身份(空串 = 默认身
| POST | `/api/cmd/record/revoke` | `IdRequest` | `Response` | 撤销 |
| POST | `/api/cmd/record/delete` | `IdRequest` | `Response` | 删除(仅未流转实例,开始节点) |

### 2.1 异常触发(`errorTrigger`)的三种语义

`ErrorThrow` 按脚本返回值分流,历史写法保持兼容:

| 脚本返回 | 类型 | 提交时行为 |
|---|---|---|
| `'节点id'` 或 `[type:'node', node:'节点id']` | NODE | 跳转到指定节点 |
| `[1,2]` / `1` 或 `[type:'operators', operators:[1,2]]` | OPERATORS | 使用指定人员兜底 |
| `[type:'prompt', message:'...']` | PROMPT | 阻断本次操作,抛 `validation.node.operatorEmpty` 并回滚 |

默认脚本已改为 PROMPT(`ScriptDefaultConstants.SCRIPT_DEFAULT_ERROR_TRIGGER`),文案可由业务方实现 `IScriptRegistry#getErrorTriggerScript` 并调用 `ScriptRegistryContext#setRegistry` 覆盖。

## 3. 流程设计(`/api/cmd/workflow`)

`flow-engine-starter-api/src/main/java/com/codingapi/flow/api/controller/WorkflowController.java`
Expand Down
13 changes: 13 additions & 0 deletions docs/capabilities/flow-engine-framework/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ ScriptRegistryContext.getInstance().setRegistry(new IScriptRegistry() {
});
```

### 异常触发脚本(errorTrigger)返回格式

节点取人为空时执行,按返回值分流为三种处理方式(前两种为历史能力,`prompt` 为 issue #296 新增):

| 返回值 | 处理方式 | 说明 |
|---|---|---|
| `[type:'prompt', message:'...']` | 异常提示 | 阻断本次操作并展示文案,事务回滚;默认脚本即此形式 |
| `[type:'node', node:'节点id']` | 指定节点 | 跳转到指定节点继续办理 |
| `[type:'operators', operators:[1,2]]` | 指定人员 | 使用给定人员兜底,继续生成记录 |
| `'节点id'` / `[1,2]` / `1` | 兼容写法 | 依次等价于 `node` / `operators` / 单个人员,历史脚本无需改动 |

Map 未声明 `type` 时按 `message` → `node` → `operators` 的顺序推断。`@SCRIPT_META` 应与返回类型一致(如 `{"type":"prompt"}`),设计器据此渲染配置面板;`getErrorTriggerScript()` 的默认实现返回「异常提示」,业务方可通过 `ScriptRegistryContext#setRegistry` 整体覆盖文案或改为其他处理方式。

### 脚本执行上下文

`FlowScriptContext` 承载脚本的 `$bind` 上下文,内部持有 `IBeanFactory` 字段并委托其实现(具体实现由 starter 的 `FlowScriptContextRegister` 装配),供 Groovy 脚本以 `$bind.getBean(...)`、`$bind.getRecordById(...)`、`$bind.getOperatorById(...)` 等方式访问 Spring 容器中的 Bean、流程记录与操作者信息;脚本的实际执行由 `FlowScriptRuntimeContext` 结合 spring-boot 的 `GroovyScript` 完成。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,119 @@
import com.codingapi.flow.node.IFlowNode;
import com.codingapi.flow.operator.IFlowOperator;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
* 异常流程配置,在节点没有匹配到人员时触发
* 异常时可以指定跳转到的节点或者指定默认的审批人员,两者二选一,优先以跳转的节点为准
* <p>
* 三种处理方式(issue #296):
* <ol>
* <li>{@link Type#NODE}:跳转到指定节点;</li>
* <li>{@link Type#OPERATORS}:使用指定的默认审批人员;</li>
* <li>{@link Type#PROMPT}:不跳转也不兜底,阻断本次操作并把提示信息交给调用方展示。</li>
* </ol>
* 前两种为历史能力,保持既有语义;{@link Type#PROMPT} 为新增语义,用于避免「静默回退到开始节点」
* 造成发起人误以为流程已正常流转。
*/
@Setter
@Getter
public class ErrorThrow {

/**
* 异常处理类型
*/
public enum Type {
/**
* 跳转节点
*/
NODE,
/**
* 指定默认审批人员
*/
OPERATORS,
/**
* 异常提示(阻断并提示)
*/
PROMPT
}

/**
* 异常处理类型,未显式设置时由 {@link #isNode()} 等语义方法按字段推导
*/
private Type type;

/**
* 跳转的节点
*/
private IFlowNode node;

/**
* 默认审批人员
*/
private List<IFlowOperator> operators;

/**
* 是否为节点
* 异常提示信息,仅 {@link Type#PROMPT} 使用
*/
private String message;

public void setType(Type type) {
this.type = type;
}

public void setNode(IFlowNode node) {
this.node = node;
if (node != null) {
this.type = Type.NODE;
}
}

public void setOperators(List<IFlowOperator> operators) {
this.operators = operators;
if (operators != null && !operators.isEmpty()) {
this.type = Type.OPERATORS;
}
}

public void setMessage(String message) {
this.message = message;
if (this.node == null && (this.operators == null || this.operators.isEmpty())) {
this.type = Type.PROMPT;
}
}

/**
* 创建异常提示结果
*
* @param message 提示信息,为空时由调用方使用兜底文案
* @return 异常提示配置
*/
public static ErrorThrow prompt(String message) {
ErrorThrow errorThrow = new ErrorThrow();
errorThrow.message = message;
// 即使文案为空也保持 PROMPT 语义:类型由配置决定,不由文案是否为空决定
errorThrow.type = Type.PROMPT;
return errorThrow;
}

/**
* 是否为节点跳转
*/
public boolean isNode() {
return node!=null;
return node != null;
}

/**
* 是否为异常提示
*/
public boolean isPrompt() {
return type == Type.PROMPT;
}

/**
* 是否指定了默认审批人员
*/
public boolean hasOperators() {
return operators != null && !operators.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,22 @@ public static FlowValidationException invalidParameter(String fieldName, String
return new FlowValidationException("validation.parameter." + fieldName,
String.format("Parameter %s has invalid value '%s', expected %s", fieldName, value, expected));
}

/**
* 节点未匹配到审批人且异常处理配置为「异常提示」,需要阻断本次操作(issue #296)。
* <p>
* 提示信息优先使用流程配置的文案;未配置时回落为带节点名称的默认文案。
*
* @param nodeId 节点 id
* @param nodeName 节点名称
* @param message 流程配置的提示信息,可为空
* @return exception
*/
public static FlowValidationException nextNodeOperatorEmpty(String nodeId, String nodeName, String message) {
String text = (message == null || message.isBlank())
? String.format("No operator matched for node '%s' (id: %s), the flow cannot continue",
nodeName, nodeId)
: message;
return new FlowValidationException("validation.node.operatorEmpty", text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ private List<FlowRecord> generateCurrentRecords(FlowSession session, Set<String>
if(errorThrow==null){
throw FlowValidationException.nodeRequired("errorTrigger");
}
// 异常提示(issue #296):阻断本次操作并把提示信息交给调用方,避免默认「回退至开始节点」
// 造成发起人误以为流程已正常流转
if(errorThrow.isPrompt()){
throw FlowValidationException.nextNodeOperatorEmpty(this.getId(), this.getName(), errorThrow.getMessage());
}
if(errorThrow.isNode()){
IFlowNode errorNode = errorThrow.getNode();
// 抄送/子流程节点不产生有效记录,跳转会静默停滞
Expand All @@ -251,6 +256,10 @@ private List<FlowRecord> generateCurrentRecords(FlowSession session, Set<String>
}
return errorNode.generateCurrentRecords(errorSession);
}else {
// 指定人员兜底:结果为空视为配置无效,否则会生成空记录让流程静默停滞
if (!errorThrow.hasOperators()) {
throw FlowValidationException.nodeRequired("errorTrigger");
}
operatorManager = new OperatorManager(errorThrow.getOperators());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ def run(request){
""";

/**
* 默认异常触发脚本
* 默认异常触发脚本(issue #296)
* <p>
* 默认行为为「异常提示」:不跳转、不兜底,阻断本次操作并提示用户,避免历史默认的
* 「回退至开始节点」让发起人误以为流程已正常流转。业务方可通过
* {@code ScriptRegistryContext#setRegistry} 覆盖文案或改为指定节点/指定人员。
*/
public static final String SCRIPT_DEFAULT_ERROR_TRIGGER = """
// @SCRIPT_TITLE 回退至开始节点
// @SCRIPT_META {"type":"node","node":"START"}
// @SCRIPT_TITLE 异常提示
// @SCRIPT_META {"type":"prompt"}
def run(request){
return request.getStartNode().getId();
return [type:'prompt', message:'流程未匹配到审批人,无法继续处理,请联系流程管理员']
}
""";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@

/**
* 异常触发脚本
* <p>
* 返回值支持四种写法(issue #296):
* <ul>
* <li>{@code Map}:{@code [type:'prompt', message:'...']} 异常提示,或 {@code [type:'node', node:'节点id']} /
* {@code [type:'operators', operators:[1,2]]} 显式声明类型;</li>
* <li>{@code String}:跳转节点 id(历史写法);</li>
* <li>{@code List}:默认审批人员 id 列表(历史写法);</li>
* <li>{@code Number}:单个默认审批人员 id(历史写法)。</li>
* </ul>
* 历史写法全部保持兼容。
*/
@AllArgsConstructor
public class ErrorTriggerScript {
Expand All @@ -32,21 +42,16 @@ public ErrorThrow execute(FlowSession session) {
Object value = FlowScriptRuntimeContext.getInstance()
.getGroovyScript(script)
.invoke(Map.of("$bind", new GroovyScriptBind(FlowScriptContext.getInstance())), request);
if(value instanceof String){
String nodeId = (String) value;
if (value instanceof Map<?, ?> map) {
return parseMap(session, map);
}
if (value instanceof String nodeId) {
ErrorThrow errorThrow = new ErrorThrow();
errorThrow.setNode(session.getNode(nodeId));
return errorThrow;
}
if(value instanceof List){
List<Object> userIds =(List<Object>) value;
List<Long> operatorIds = new ArrayList<>();
for(Object userId:userIds){
operatorIds.add(Long.parseLong(String.valueOf(userId)));
}
ErrorThrow errorThrow = new ErrorThrow();
errorThrow.setOperators(session.getRepositoryHolder().findOperatorByIds(operatorIds));
return errorThrow;
if (value instanceof List<?> userIds) {
return parseOperators(session, userIds);
}

long userId = Long.parseLong(String.valueOf(value));
Expand All @@ -58,6 +63,57 @@ public ErrorThrow execute(FlowSession session) {

}

/**
* 解析 Map 形式的返回值
*
* @param session 当前会话
* @param map 脚本返回值
* @return 异常处理配置
*/
private ErrorThrow parseMap(FlowSession session, Map<?, ?> map) {
String type = map.get("type") == null ? null : String.valueOf(map.get("type"));
if (ErrorThrow.Type.PROMPT.name().equalsIgnoreCase(type) || "prompt".equalsIgnoreCase(type)) {
Object message = map.get("message");
return ErrorThrow.prompt(message == null ? null : String.valueOf(message));
}
if (ErrorThrow.Type.NODE.name().equalsIgnoreCase(type)) {
Object nodeId = map.get("node");
ErrorThrow errorThrow = new ErrorThrow();
errorThrow.setNode(nodeId == null ? null : session.getNode(String.valueOf(nodeId)));
return errorThrow;
}
if (ErrorThrow.Type.OPERATORS.name().equalsIgnoreCase(type)) {
Object operators = map.get("operators");
if (operators instanceof List<?> userIds) {
return parseOperators(session, userIds);
}
return new ErrorThrow();
}
// 未声明 type 时按字段存在性推断:message 优先(提示),其次 node,最后 operators
if (map.get("message") != null) {
return ErrorThrow.prompt(String.valueOf(map.get("message")));
}
if (map.get("node") != null) {
ErrorThrow errorThrow = new ErrorThrow();
errorThrow.setNode(session.getNode(String.valueOf(map.get("node"))));
return errorThrow;
}
if (map.get("operators") instanceof List<?> userIds) {
return parseOperators(session, userIds);
}
return new ErrorThrow();
}

private ErrorThrow parseOperators(FlowSession session, List<?> userIds) {
List<Long> operatorIds = new ArrayList<>();
for (Object userId : userIds) {
operatorIds.add(Long.parseLong(String.valueOf(userId)));
}
ErrorThrow errorThrow = new ErrorThrow();
errorThrow.setOperators(session.getRepositoryHolder().findOperatorByIds(operatorIds));
return errorThrow;
}

/**
* 默认节点脚本
*/
Expand Down
Loading
Loading