TriggerWebHookController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package jnpf.flowable.controller;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import io.swagger.v3.oas.annotations.Operation;
  4. import io.swagger.v3.oas.annotations.Parameter;
  5. import io.swagger.v3.oas.annotations.Parameters;
  6. import io.swagger.v3.oas.annotations.tags.Tag;
  7. import jnpf.base.ActionResult;
  8. import jnpf.config.ConfigValueUtil;
  9. import jnpf.constant.MsgCode;
  10. import jnpf.database.util.TenantDataSourceUtil;
  11. import jnpf.exception.WorkFlowException;
  12. import jnpf.flowable.entity.TemplateJsonEntity;
  13. import jnpf.flowable.model.trigger.TriggerWebHookInfoVo;
  14. import jnpf.flowable.service.TemplateJsonService;
  15. import jnpf.flowable.util.TriggerUtil;
  16. import jnpf.util.*;
  17. import org.apache.commons.codec.binary.Base64;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.*;
  20. import java.nio.charset.StandardCharsets;
  21. import java.util.*;
  22. /**
  23. * 类的描述
  24. *
  25. * @author JNPF@YinMai Info. Co., Ltd
  26. * @version 5.0.x
  27. * @since 2024/9/21 15:39
  28. */
  29. @Tag(name = "webhook触发", description = "WebHook")
  30. @RestController
  31. @RequestMapping("/api/workflow/Hooks")
  32. public class TriggerWebHookController {
  33. @Autowired
  34. private RedisUtil redisUtil;
  35. @Autowired
  36. private ConfigValueUtil configValueUtil;
  37. @Autowired
  38. private TriggerUtil triggerUtil;
  39. @Autowired
  40. private TemplateJsonService templateJsonService;
  41. private static final String WEBHOOK_RED_KEY = "webhook_trigger";
  42. private static final long DEFAULT_CACHE_TIME = 60 * 5;
  43. @Operation(summary = "数据接收接口")
  44. @Parameters({
  45. @Parameter(name = "id", description = "base64转码id", required = true),
  46. @Parameter(name = "tenantId", description = "租户id", required = false)
  47. })
  48. @PostMapping("/{id}")
  49. @NoDataSourceBind
  50. public ActionResult webhookTrigger(@PathVariable("id") String id,
  51. @RequestParam(value = "tenantId", required = false) String tenantId,
  52. @RequestBody Map<String, Object> body) throws Exception {
  53. String idReal = new String(Base64.decodeBase64(id.getBytes(StandardCharsets.UTF_8)));
  54. if (configValueUtil.isMultiTenancy()) {
  55. // 判断是不是从外面直接请求
  56. if (StringUtil.isNotEmpty(tenantId)) {
  57. //切换成租户库
  58. try {
  59. TenantDataSourceUtil.switchTenant(tenantId);
  60. } catch (Exception e) {
  61. return ActionResult.fail(MsgCode.LOG105.get());
  62. }
  63. }
  64. }
  65. try {
  66. triggerUtil.handleWebhookTrigger(idReal, tenantId, body);
  67. } catch (Exception e) {
  68. // triggerUtil.createErrorRecord();
  69. throw e;
  70. }
  71. return ActionResult.success();
  72. }
  73. @Operation(summary = "获取webhookUrl")
  74. @Parameters({
  75. @Parameter(name = "id", description = "主键", required = true)
  76. })
  77. @GetMapping("/getUrl")
  78. public ActionResult getWebhookUrl(@RequestParam("id") String id) {
  79. String enCodeBase64 = new String(Base64.encodeBase64(id.getBytes(StandardCharsets.UTF_8)));
  80. String randomStr = UUID.randomUUID().toString().substring(0, 5);
  81. TriggerWebHookInfoVo vo = new TriggerWebHookInfoVo();
  82. vo.setEnCodeStr(enCodeBase64);
  83. vo.setRandomStr(randomStr);
  84. vo.setWebhookUrl("/api/workflow/Hooks/" + enCodeBase64);
  85. vo.setRequestUrl("/api/workflow/Hooks/" + enCodeBase64 + "/params/" + randomStr);
  86. return ActionResult.success(vo);
  87. }
  88. @Operation(summary = "通过get接口获取参数")
  89. @Parameters({
  90. @Parameter(name = "id", description = "base64转码id", required = true),
  91. @Parameter(name = "randomStr", description = "获取webhookUrl提供的随机字符", required = true)
  92. })
  93. @GetMapping("/{id}/params/{randomStr}")
  94. @NoDataSourceBind
  95. public ActionResult getWebhookParams(@PathVariable("id") String id,
  96. @PathVariable("randomStr") String randomStr) throws WorkFlowException {
  97. insertRedis(id, randomStr, new HashMap<>());
  98. return ActionResult.success();
  99. }
  100. @Operation(summary = "通过post接口获取参数")
  101. @Parameters({
  102. @Parameter(name = "id", description = "base64转码id", required = true),
  103. @Parameter(name = "randomStr", description = "获取webhookUrl提供的随机字符", required = true)
  104. })
  105. @PostMapping("/{id}/params/{randomStr}")
  106. @NoDataSourceBind
  107. public ActionResult postWebhookParams(@PathVariable("id") String id,
  108. @PathVariable("randomStr") String randomStr,
  109. @RequestBody Map<String, Object> obj) throws WorkFlowException {
  110. insertRedis(id, randomStr, new HashMap<>(obj));
  111. return ActionResult.success();
  112. }
  113. /**
  114. * 助手id查询信息,写入缓存
  115. *
  116. * @param id
  117. * @param randomStr
  118. * @param resultMap
  119. * @throws WorkFlowException
  120. */
  121. private void insertRedis(String id, String randomStr, Map<String, Object> resultMap) throws WorkFlowException {
  122. String idReal = new String(Base64.decodeBase64(id.getBytes(StandardCharsets.UTF_8)));
  123. String key1 = WEBHOOK_RED_KEY + "_" + idReal + "_" + randomStr;
  124. if (!redisUtil.exists(key1)) {
  125. throw new WorkFlowException(MsgCode.VS016.get());
  126. }
  127. String tenantId = redisUtil.getString(key1).toString();
  128. if (configValueUtil.isMultiTenancy()) {
  129. // 判断是不是从外面直接请求
  130. if (StringUtil.isNotEmpty(tenantId)) {
  131. //切换成租户库
  132. try {
  133. TenantDataSourceUtil.switchTenant(tenantId);
  134. } catch (Exception e) {
  135. throw new WorkFlowException(MsgCode.LOG105.get());
  136. }
  137. }
  138. }
  139. TemplateJsonEntity jsonEntity = templateJsonService.getById(idReal == null ? "" : idReal);
  140. if (!ObjectUtil.equals(jsonEntity.getState(), 1)) {
  141. throw new WorkFlowException("版本未启用");
  142. }
  143. Map<String, Object> parameterMap = new HashMap<>(ServletUtil.getRequest().getParameterMap());
  144. for (String key : parameterMap.keySet()) {
  145. String[] parameterValues = ServletUtil.getRequest().getParameterValues(key);
  146. if (parameterValues.length == 1) {
  147. parameterMap.put(key, parameterValues[0]);
  148. } else {
  149. parameterMap.put(key, parameterValues);
  150. }
  151. }
  152. resultMap.putAll(parameterMap);
  153. if (resultMap.keySet().size() > 0) {
  154. redisUtil.insert(WEBHOOK_RED_KEY + "_" + randomStr, resultMap, DEFAULT_CACHE_TIME);
  155. redisUtil.remove(key1);
  156. }
  157. }
  158. @Operation(summary = "请求参数添加触发接口")
  159. @Parameters({
  160. @Parameter(name = "id", description = "base64转码id", required = true),
  161. @Parameter(name = "randomStr", description = "获取webhookUrl提供的随机字符", required = true)
  162. })
  163. @GetMapping("/{id}/start/{randomStr}")
  164. public ActionResult start(@PathVariable("id") String id,
  165. @PathVariable("randomStr") String randomStr) {
  166. redisUtil.remove(WEBHOOK_RED_KEY + "_" + randomStr);
  167. redisUtil.insert(WEBHOOK_RED_KEY + "_" + id + "_" + randomStr, UserProvider.getUser().getTenantId(), DEFAULT_CACHE_TIME);
  168. return ActionResult.success();
  169. }
  170. @Operation(summary = "获取缓存的接口参数")
  171. @Parameters({
  172. @Parameter(name = "randomStr", description = "获取webhookUrl提供的随机字符", required = true)
  173. })
  174. @GetMapping("/getParams/{randomStr}")
  175. public ActionResult getRedisParams(@PathVariable("randomStr") String randomStr) {
  176. Map<String, Object> mapRedis = new HashMap<>();
  177. String key = WEBHOOK_RED_KEY + "_" + randomStr;
  178. if (redisUtil.exists(key)) {
  179. mapRedis = redisUtil.getMap(key);
  180. }
  181. List<Map<String, Object>> list = new ArrayList<>();
  182. for (String redisKey : mapRedis.keySet()) {
  183. Map<String, Object> map = new HashMap<>();
  184. map.put("id", redisKey);
  185. map.put("fullName", mapRedis.get(redisKey));
  186. list.add(map);
  187. }
  188. return ActionResult.success(list);
  189. }
  190. }