WebHookController.java 8.0 KB

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