|
@@ -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;
|
|
|
+ }
|
|
|
}
|