【功能新增】 解析 Http 请求触发器返回值,修改流程变量
This commit is contained in:
parent
d63e315876
commit
53789d9b80
|
@ -1,5 +1,6 @@
|
|||
package cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import cn.iocoder.yudao.module.bpm.enums.definition.*;
|
||||
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum;
|
||||
|
@ -359,6 +360,14 @@ public class BpmSimpleModelNodeVO {
|
|||
@Schema(description = "请求头参数设置", example = "[]")
|
||||
@Valid
|
||||
private List<HttpRequestParam> body;
|
||||
|
||||
/**
|
||||
* 请求返回处理设置。 用于修改流程表单值
|
||||
* key: 表示要修改的流程表单字段 Id.
|
||||
* value: 接口返回的字段名
|
||||
*/
|
||||
@Schema(description = "请求返回处理设置", example = "[]")
|
||||
private List<KeyValue<String,String>> response;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -157,4 +157,12 @@ public interface BpmProcessInstanceService {
|
|||
*/
|
||||
void processProcessInstanceCompleted(ProcessInstance instance);
|
||||
|
||||
/**
|
||||
* 更新 ProcessInstance 的变量
|
||||
*
|
||||
* @param processInstanceId 流程编号
|
||||
* @param variables 流程变量
|
||||
*/
|
||||
void updateProcessInstanceVariables(String processInstanceId, Map<String, Object> variables);
|
||||
|
||||
}
|
||||
|
|
|
@ -743,4 +743,8 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
|
|||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProcessInstanceVariables(String processInstanceId, Map<String, Object> variables) {
|
||||
runtimeService.setVariables(processInstanceId, variables);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package cn.iocoder.yudao.module.bpm.service.task.trigger;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO.TriggerSetting.HttpRequestTriggerSetting;
|
||||
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTriggerTypeEnum;
|
||||
|
@ -17,6 +22,8 @@ import org.springframework.util.MultiValueMap;
|
|||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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_
|
|||
@Slf4j
|
||||
public class BpmHttpRequestTrigger implements BpmTrigger {
|
||||
|
||||
private static final String PARSE_RESPONSE_FIELD = "data";
|
||||
|
||||
@Resource
|
||||
private BpmProcessInstanceService processInstanceService;
|
||||
|
||||
|
@ -67,9 +76,45 @@ public class BpmHttpRequestTrigger implements BpmTrigger {
|
|||
ResponseEntity<String> responseEntity = restTemplate.exchange(setting.getUrl(), HttpMethod.POST,
|
||||
requestEntity, String.class);
|
||||
log.info("[execute][HTTP 触发器,请求头:{},请求体:{},响应结果:{}]", headers, body, responseEntity);
|
||||
// 4. 处理请求返回
|
||||
if (CollUtil.isNotEmpty(setting.getResponse()) && responseEntity.getStatusCode().is2xxSuccessful()
|
||||
&& StrUtil.isNotEmpty(responseEntity.getBody())) {
|
||||
// 4.1 获取需要更新的流程变量
|
||||
Map<String, Object> updateVariables = getNeedUpdatedVariablesFromResponse(responseEntity.getBody(), setting.getResponse());
|
||||
// 4.2 更新流程变量
|
||||
if (CollUtil.isNotEmpty(updateVariables)) {
|
||||
processInstanceService.updateProcessInstanceVariables(processInstanceId, updateVariables);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (RestClientException e) {
|
||||
log.error("[execute][HTTP 触发器,请求头:{},请求体:{},请求出错:{}]", headers, body, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从请求返回值获取需要更新的流程变量。优先从 data 字段获取,如果 data 字段不存在,从根节点获取。
|
||||
*
|
||||
* @param responseBody 请求返回报文体
|
||||
* @param responseSettings 返回设置
|
||||
* @return 需要更新的流程变量
|
||||
*/
|
||||
private Map<String, Object> getNeedUpdatedVariablesFromResponse(String responseBody,
|
||||
List<KeyValue<String, String>> responseSettings) {
|
||||
Map<String, Object> updateVariables = new HashMap<>();
|
||||
if (JSONUtil.isTypeJSONObject(responseBody)) {
|
||||
JSONObject dataObj = null;
|
||||
if (JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD) instanceof JSONObject) {
|
||||
dataObj = (JSONObject) JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD);
|
||||
}
|
||||
JSONObject updateObj = dataObj == null ? JSONUtil.parseObj(responseBody) : dataObj;
|
||||
responseSettings.forEach(respSetting -> {
|
||||
if (StrUtil.isNotEmpty(respSetting.getKey()) && updateObj.containsKey(respSetting.getValue())) {
|
||||
updateVariables.put(respSetting.getKey(), updateObj.get(respSetting.getValue()));
|
||||
}
|
||||
});
|
||||
}
|
||||
return updateVariables;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue