在一些框架里面它自带了配置的表单,但是大多数不太符合业务需求,在业务上需要自定义UI的表单,并且表单还有一些接口数据,所以自带的就不太符合日常操作。
表单的展示可以通过自带的Task里面formId去配置,这块主要是前端去写。
后端在通过的时候执行业务方法,把业务数据填写到响应的表里面。
实体类如下,通过前端传入的动态表单值去匹配,这里还需要两个配置,一个是执行类,另一个是实体类,要通过配置这两个进行转换
@Schema(description = "管理后台 - 通过流程任务的 Request VO")
@Data
public class BpmTaskApproveReqVO {
@Schema(description = "任务编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
@NotEmpty(message = "任务编号不能为空")
private String id;
@Schema(description = "审批意见", requiredMode = Schema.RequiredMode.REQUIRED, example = "不错不错!")
@NotEmpty(message = "审批意见不能为空")
private String reason;
@Schema(description = "抄送的用户编号数组", requiredMode = Schema.RequiredMode.REQUIRED, example = "1,2")
private Collection<Long> copyUserIds;
@Schema(description = "变量实例(动态表单)", requiredMode = Schema.RequiredMode.REQUIRED)
private Map<String, Object> variables;
}
在bpm里面配置对应的数据实体类和执行实体类
/**
* @param task 当前任务
* @param instance 实例数据
* @param variables 参数变量
*/
private void fillBizFormData(Task task, ProcessInstance instance, Map<String, Object> variables) {
Map<String, Object> processVariables = instance.getProcessVariables() == null ? instance.getProcessVariables() : task.getProcessVariables();
// 对应bean的全限定名
String clazzName = (String) processVariables.get("clazzName");
// 对应的执行器的名称
String executorBeanName = (String) processVariables.get("executorName");
if (StrUtil.isBlank(clazzName) || StrUtil.isBlank(executorBeanName)) {
return;
}
// 实例化向上转型
try {
Class<?> clazz = Class.forName(clazzName);
BpmProcessBusinessData businessData = (BpmProcessBusinessData) BeanUtils.toBean(variables, clazz);
// 实例化执行器
BpmProcessFillExecutor<BpmProcessBusinessData> bpmProcessFill = SpringUtil.getBean(executorBeanName);
// 执行业务填充方法
Boolean result = Optional.ofNullable(bpmProcessFill.fill(businessData)).orElse(Boolean.FALSE);
if (!result) {
throw exception(BPM_TASK_APPROVE_FAILURE);
}
} catch (ClassNotFoundException e) {
log.error("找不到对应的类:{}", clazzName);
throw new RuntimeException(e);
}
}
/**
* @Description 业务主类,业务需要继承本类
*
* @Date 2024/5/7 13:25
* @Author tom
**/
public class BpmProcessBusinessData {
}
/**
* @Description //执行
* @Date 2024/5/7 13:16
* @Author tom
**/
public interface BpmProcessFillExecutor<T extends BpmProcessBusinessData> {
/**
* 业务填充数据
* @param businessData 业务类需要继承
* @return
*/
Boolean fill(T businessData);
}
执行类只要实现BpmProcessFillExecutor接口,在fill方法里面向下转型即可获取对象,转换后做业务插入工作。