Commit 53789d9b8016e2b06a15aae2eeccf1240fbcad36
1 parent
d63e3158
【功能新增】 解析 Http 请求触发器返回值,修改流程变量
Showing
4 changed files
with
66 additions
and
0 deletions
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/definition/vo/model/simple/BpmSimpleModelNodeVO.java
| 1 | package cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple; | 1 | package cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.core.KeyValue; | ||
| 3 | import cn.iocoder.yudao.framework.common.validation.InEnum; | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 4 | import cn.iocoder.yudao.module.bpm.enums.definition.*; | 5 | import cn.iocoder.yudao.module.bpm.enums.definition.*; |
| 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; | 6 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| @@ -359,6 +360,14 @@ public class BpmSimpleModelNodeVO { | @@ -359,6 +360,14 @@ public class BpmSimpleModelNodeVO { | ||
| 359 | @Schema(description = "请求头参数设置", example = "[]") | 360 | @Schema(description = "请求头参数设置", example = "[]") |
| 360 | @Valid | 361 | @Valid |
| 361 | private List<HttpRequestParam> body; | 362 | private List<HttpRequestParam> body; |
| 363 | + | ||
| 364 | + /** | ||
| 365 | + * 请求返回处理设置。 用于修改流程表单值 | ||
| 366 | + * key: 表示要修改的流程表单字段 Id. | ||
| 367 | + * value: 接口返回的字段名 | ||
| 368 | + */ | ||
| 369 | + @Schema(description = "请求返回处理设置", example = "[]") | ||
| 370 | + private List<KeyValue<String,String>> response; | ||
| 362 | } | 371 | } |
| 363 | 372 | ||
| 364 | } | 373 | } |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmProcessInstanceService.java
| @@ -157,4 +157,12 @@ public interface BpmProcessInstanceService { | @@ -157,4 +157,12 @@ public interface BpmProcessInstanceService { | ||
| 157 | */ | 157 | */ |
| 158 | void processProcessInstanceCompleted(ProcessInstance instance); | 158 | void processProcessInstanceCompleted(ProcessInstance instance); |
| 159 | 159 | ||
| 160 | + /** | ||
| 161 | + * 更新 ProcessInstance 的变量 | ||
| 162 | + * | ||
| 163 | + * @param processInstanceId 流程编号 | ||
| 164 | + * @param variables 流程变量 | ||
| 165 | + */ | ||
| 166 | + void updateProcessInstanceVariables(String processInstanceId, Map<String, Object> variables); | ||
| 167 | + | ||
| 160 | } | 168 | } |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmProcessInstanceServiceImpl.java
| @@ -743,4 +743,8 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService | @@ -743,4 +743,8 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService | ||
| 743 | }); | 743 | }); |
| 744 | } | 744 | } |
| 745 | 745 | ||
| 746 | + @Override | ||
| 747 | + public void updateProcessInstanceVariables(String processInstanceId, Map<String, Object> variables) { | ||
| 748 | + runtimeService.setVariables(processInstanceId, variables); | ||
| 749 | + } | ||
| 746 | } | 750 | } |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/trigger/BpmHttpRequestTrigger.java
| 1 | package cn.iocoder.yudao.module.bpm.service.task.trigger; | 1 | package cn.iocoder.yudao.module.bpm.service.task.trigger; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import cn.hutool.core.util.StrUtil; | ||
| 5 | +import cn.hutool.json.JSONObject; | ||
| 6 | +import cn.hutool.json.JSONUtil; | ||
| 7 | +import cn.iocoder.yudao.framework.common.core.KeyValue; | ||
| 3 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 8 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 4 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO.TriggerSetting.HttpRequestTriggerSetting; | 9 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO.TriggerSetting.HttpRequestTriggerSetting; |
| 5 | import cn.iocoder.yudao.module.bpm.enums.definition.BpmTriggerTypeEnum; | 10 | import cn.iocoder.yudao.module.bpm.enums.definition.BpmTriggerTypeEnum; |
| @@ -17,6 +22,8 @@ import org.springframework.util.MultiValueMap; | @@ -17,6 +22,8 @@ import org.springframework.util.MultiValueMap; | ||
| 17 | import org.springframework.web.client.RestClientException; | 22 | import org.springframework.web.client.RestClientException; |
| 18 | import org.springframework.web.client.RestTemplate; | 23 | import org.springframework.web.client.RestTemplate; |
| 19 | 24 | ||
| 25 | +import java.util.HashMap; | ||
| 26 | +import java.util.List; | ||
| 20 | import java.util.Map; | 27 | import java.util.Map; |
| 21 | 28 | ||
| 22 | import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID; | 29 | import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID; |
| @@ -30,6 +37,8 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_ | @@ -30,6 +37,8 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_ | ||
| 30 | @Slf4j | 37 | @Slf4j |
| 31 | public class BpmHttpRequestTrigger implements BpmTrigger { | 38 | public class BpmHttpRequestTrigger implements BpmTrigger { |
| 32 | 39 | ||
| 40 | + private static final String PARSE_RESPONSE_FIELD = "data"; | ||
| 41 | + | ||
| 33 | @Resource | 42 | @Resource |
| 34 | private BpmProcessInstanceService processInstanceService; | 43 | private BpmProcessInstanceService processInstanceService; |
| 35 | 44 | ||
| @@ -67,9 +76,45 @@ public class BpmHttpRequestTrigger implements BpmTrigger { | @@ -67,9 +76,45 @@ public class BpmHttpRequestTrigger implements BpmTrigger { | ||
| 67 | ResponseEntity<String> responseEntity = restTemplate.exchange(setting.getUrl(), HttpMethod.POST, | 76 | ResponseEntity<String> responseEntity = restTemplate.exchange(setting.getUrl(), HttpMethod.POST, |
| 68 | requestEntity, String.class); | 77 | requestEntity, String.class); |
| 69 | log.info("[execute][HTTP 触发器,请求头:{},请求体:{},响应结果:{}]", headers, body, responseEntity); | 78 | log.info("[execute][HTTP 触发器,请求头:{},请求体:{},响应结果:{}]", headers, body, responseEntity); |
| 79 | + // 4. 处理请求返回 | ||
| 80 | + if (CollUtil.isNotEmpty(setting.getResponse()) && responseEntity.getStatusCode().is2xxSuccessful() | ||
| 81 | + && StrUtil.isNotEmpty(responseEntity.getBody())) { | ||
| 82 | + // 4.1 获取需要更新的流程变量 | ||
| 83 | + Map<String, Object> updateVariables = getNeedUpdatedVariablesFromResponse(responseEntity.getBody(), setting.getResponse()); | ||
| 84 | + // 4.2 更新流程变量 | ||
| 85 | + if (CollUtil.isNotEmpty(updateVariables)) { | ||
| 86 | + processInstanceService.updateProcessInstanceVariables(processInstanceId, updateVariables); | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 70 | } catch (RestClientException e) { | 90 | } catch (RestClientException e) { |
| 71 | log.error("[execute][HTTP 触发器,请求头:{},请求体:{},请求出错:{}]", headers, body, e.getMessage()); | 91 | log.error("[execute][HTTP 触发器,请求头:{},请求体:{},请求出错:{}]", headers, body, e.getMessage()); |
| 72 | } | 92 | } |
| 73 | } | 93 | } |
| 74 | 94 | ||
| 95 | + | ||
| 96 | + /** | ||
| 97 | + * 从请求返回值获取需要更新的流程变量。优先从 data 字段获取,如果 data 字段不存在,从根节点获取。 | ||
| 98 | + * | ||
| 99 | + * @param responseBody 请求返回报文体 | ||
| 100 | + * @param responseSettings 返回设置 | ||
| 101 | + * @return 需要更新的流程变量 | ||
| 102 | + */ | ||
| 103 | + private Map<String, Object> getNeedUpdatedVariablesFromResponse(String responseBody, | ||
| 104 | + List<KeyValue<String, String>> responseSettings) { | ||
| 105 | + Map<String, Object> updateVariables = new HashMap<>(); | ||
| 106 | + if (JSONUtil.isTypeJSONObject(responseBody)) { | ||
| 107 | + JSONObject dataObj = null; | ||
| 108 | + if (JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD) instanceof JSONObject) { | ||
| 109 | + dataObj = (JSONObject) JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD); | ||
| 110 | + } | ||
| 111 | + JSONObject updateObj = dataObj == null ? JSONUtil.parseObj(responseBody) : dataObj; | ||
| 112 | + responseSettings.forEach(respSetting -> { | ||
| 113 | + if (StrUtil.isNotEmpty(respSetting.getKey()) && updateObj.containsKey(respSetting.getValue())) { | ||
| 114 | + updateVariables.put(respSetting.getKey(), updateObj.get(respSetting.getValue())); | ||
| 115 | + } | ||
| 116 | + }); | ||
| 117 | + } | ||
| 118 | + return updateVariables; | ||
| 119 | + } | ||
| 75 | } | 120 | } |