From 34981c30a78e8bbd7791131059a9210f9928b62c Mon Sep 17 00:00:00 2001
From: 刘光辉 <347230014@qq.com>
Date: 星期四, 17 九月 2026 09:24:09 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master' into master

---
 jnpf-flowable/jnpf-flowable-controller/src/main/java/jnpf/flowable/controller/TriggerWebHookController.java |  194 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 194 insertions(+), 0 deletions(-)

diff --git a/jnpf-flowable/jnpf-flowable-controller/src/main/java/jnpf/flowable/controller/TriggerWebHookController.java b/jnpf-flowable/jnpf-flowable-controller/src/main/java/jnpf/flowable/controller/TriggerWebHookController.java
new file mode 100644
index 0000000..cf78be0
--- /dev/null
+++ b/jnpf-flowable/jnpf-flowable-controller/src/main/java/jnpf/flowable/controller/TriggerWebHookController.java
@@ -0,0 +1,194 @@
+package jnpf.flowable.controller;
+
+import cn.hutool.core.util.ObjectUtil;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jnpf.base.ActionResult;
+import jnpf.config.ConfigValueUtil;
+import jnpf.constant.MsgCode;
+import jnpf.database.util.TenantDataSourceUtil;
+import jnpf.exception.WorkFlowException;
+import jnpf.flowable.entity.TemplateJsonEntity;
+import jnpf.flowable.enums.TemplateJsonStatueEnum;
+import jnpf.flowable.model.trigger.TriggerWebHookInfoVo;
+import jnpf.flowable.service.TemplateJsonService;
+import jnpf.flowable.util.OperatorUtil;
+import jnpf.util.*;
+import lombok.RequiredArgsConstructor;
+import org.apache.commons.codec.binary.Base64;
+import org.springframework.web.bind.annotation.*;
+
+import java.nio.charset.StandardCharsets;
+import java.util.*;
+
+/**
+ * 绫荤殑鎻忚堪
+ *
+ * @author JNPF@YinMai Info. Co., Ltd
+ * @version 5.0.x
+ * @since 2024/9/21 15:39
+ */
+@Tag(name = "webhook瑙﹀彂", description = "WebHook")
+@RestController
+@RequestMapping("/Hooks")
+@RequiredArgsConstructor
+public class TriggerWebHookController {
+
+
+
+    private final  RedisUtil redisUtil;
+
+    private final  OperatorUtil operatorUtil;
+
+    private final  ConfigValueUtil configValueUtil;
+
+    private  final TemplateJsonService templateJsonService;
+
+    private static final String WEBHOOK_RED_KEY = "webhook_trigger";
+    private static final long DEFAULT_CACHE_TIME = 300;
+
+    @Operation(summary = "鏁版嵁鎺ユ敹鎺ュ彛")
+    @Parameter(name = "id", description = "base64杞爜id", required = true)
+    @Parameter(name = "tenantId", description = "绉熸埛id", required = false)
+    @PostMapping("/{id}")
+    @NoDataSourceBind
+    public ActionResult<Object> webhookTrigger(@PathVariable("id") String id,
+                                       @RequestParam(value = "tenantId", required = false) String tenantId,
+                                       @RequestBody Map<String, Object> body) throws WorkFlowException {
+        String idReal = new String(Base64.decodeBase64(id.getBytes(StandardCharsets.UTF_8)));
+        if (configValueUtil.isMultiTenancy()&&StringUtil.isNotEmpty(tenantId)) {
+            // 鍒ゆ柇鏄笉鏄粠澶栭潰鐩存帴璇锋眰
+                //鍒囨崲鎴愮鎴峰簱
+                try {
+                    TenantDataSourceUtil.switchTenant(tenantId);
+                } catch (Exception e) {
+                    return ActionResult.fail(MsgCode.LOG105.get());
+                }
+
+        }
+        try {
+            operatorUtil.handleWebhookTrigger(idReal, tenantId, body);
+        } catch (WorkFlowException e) {
+            e.printStackTrace();
+            throw e;
+        }
+        return ActionResult.success();
+    }
+
+    @Operation(summary = "鑾峰彇webhookUrl")
+    @Parameter(name = "id", description = "涓婚敭", required = true)
+    @GetMapping("/getUrl")
+    public ActionResult<Object> getWebhookUrl(@RequestParam("id") String id) {
+        String enCodeBase64 = new String(Base64.encodeBase64(id.getBytes(StandardCharsets.UTF_8)));
+        String randomStr = UUID.randomUUID().toString().substring(0, 5);
+        TriggerWebHookInfoVo vo = new TriggerWebHookInfoVo();
+        vo.setEnCodeStr(enCodeBase64);
+        vo.setRandomStr(randomStr);
+        vo.setWebhookUrl("/api/workflow/Hooks/" + enCodeBase64);
+        vo.setRequestUrl("/api/workflow/Hooks/" + enCodeBase64 + "/params/" + randomStr);
+        return ActionResult.success(vo);
+    }
+
+    @Operation(summary = "閫氳繃get鎺ュ彛鑾峰彇鍙傛暟")
+    @Parameter(name = "id", description = "base64杞爜id", required = true)
+    @Parameter(name = "randomStr", description = "鑾峰彇webhookUrl鎻愪緵鐨勯殢鏈哄瓧绗�", required = true)
+    @GetMapping("/{id}/params/{randomStr}")
+    @NoDataSourceBind
+    public ActionResult<Object> getWebhookParams(@PathVariable("id") String id,
+                                         @PathVariable("randomStr") String randomStr) throws WorkFlowException {
+        insertRedis(id, randomStr, new HashMap<>());
+        return ActionResult.success();
+    }
+
+    @Operation(summary = "閫氳繃post鎺ュ彛鑾峰彇鍙傛暟")
+    @Parameter(name = "id", description = "base64杞爜id", required = true)
+    @Parameter(name = "randomStr", description = "鑾峰彇webhookUrl鎻愪緵鐨勯殢鏈哄瓧绗�", required = true)
+    @PostMapping("/{id}/params/{randomStr}")
+    @NoDataSourceBind
+    public ActionResult<Object> postWebhookParams(@PathVariable("id") String id,
+                                          @PathVariable("randomStr") String randomStr,
+                                          @RequestBody Map<String, Object> obj) throws WorkFlowException {
+        insertRedis(id, randomStr, new HashMap<>(obj));
+        return ActionResult.success();
+    }
+
+    /**
+     * 鍔╂墜id鏌ヨ淇℃伅锛屽啓鍏ョ紦瀛�
+     *
+     * @param id
+     * @param randomStr
+     * @param resultMap
+     * @throws WorkFlowException
+     */
+    private void insertRedis(String id, String randomStr, Map<String, Object> resultMap) throws WorkFlowException {
+        String idReal = new String(Base64.decodeBase64(id.getBytes(StandardCharsets.UTF_8)));
+        String key1 = WEBHOOK_RED_KEY + "_" + idReal + "_" + randomStr;
+        if (!redisUtil.exists(key1)) {
+            throw new WorkFlowException(MsgCode.VS016.get());
+        }
+        String tenantId = redisUtil.getString(key1).toString();
+
+        if (configValueUtil.isMultiTenancy()&&StringUtil.isNotEmpty(tenantId)) {
+            // 鍒ゆ柇鏄笉鏄粠澶栭潰鐩存帴璇锋眰
+
+                //鍒囨崲鎴愮鎴峰簱
+                try {
+                    TenantDataSourceUtil.switchTenant(tenantId);
+                } catch (Exception e) {
+                    throw new WorkFlowException(MsgCode.LOG105.get());
+                }
+        }
+        TemplateJsonEntity jsonEntity = templateJsonService.getById(idReal);
+        if (!ObjectUtil.equals(jsonEntity.getState(), TemplateJsonStatueEnum.START.getCode())) {
+            throw new WorkFlowException("鐗堟湰鏈惎鐢�");
+        }
+        Map<String, Object> parameterMap = new HashMap<>(ServletUtil.getRequest().getParameterMap());
+        for (String key : parameterMap.keySet()) {
+            String[] parameterValues = ServletUtil.getRequest().getParameterValues(key);
+            if (parameterValues.length == 1) {
+                parameterMap.put(key, parameterValues[0]);
+            } else {
+                parameterMap.put(key, parameterValues);
+            }
+        }
+        resultMap.putAll(parameterMap);
+        if (!resultMap.isEmpty()) {
+            redisUtil.insert(WEBHOOK_RED_KEY + "_" + randomStr, resultMap, DEFAULT_CACHE_TIME);
+            redisUtil.remove(key1);
+        }
+    }
+
+    @Operation(summary = "璇锋眰鍙傛暟娣诲姞瑙﹀彂鎺ュ彛")
+    @Parameter(name = "id", description = "base64杞爜id", required = true)
+    @Parameter(name = "randomStr", description = "鑾峰彇webhookUrl鎻愪緵鐨勯殢鏈哄瓧绗�", required = true)
+    @GetMapping("/{id}/start/{randomStr}")
+    public ActionResult<Object> start(@PathVariable("id") String id,
+                              @PathVariable("randomStr") String randomStr) {
+        redisUtil.remove(WEBHOOK_RED_KEY + "_" + randomStr);
+        redisUtil.insert(WEBHOOK_RED_KEY + "_" + id + "_" + randomStr, UserProvider.getUser().getTenantId(), DEFAULT_CACHE_TIME);
+        return ActionResult.success();
+    }
+
+    @Operation(summary = "鑾峰彇缂撳瓨鐨勬帴鍙e弬鏁�")
+    @Parameter(name = "randomStr", description = "鑾峰彇webhookUrl鎻愪緵鐨勯殢鏈哄瓧绗�", required = true)
+    @GetMapping("/getParams/{randomStr}")
+    public ActionResult<Object> getRedisParams(@PathVariable("randomStr") String randomStr) {
+        Map<String, Object> mapRedis = new HashMap<>();
+        String key = WEBHOOK_RED_KEY + "_" + randomStr;
+        if (redisUtil.exists(key)) {
+            mapRedis = redisUtil.getMap(key);
+        }
+        List<Map<String, Object>> list = new ArrayList<>();
+
+        for (Map.Entry<String, Object> stringObjectEntry : mapRedis.entrySet()) {
+            String redisKey = stringObjectEntry.getKey();
+            Map<String, Object> map = new HashMap<>();
+            map.put("id", redisKey);
+            map.put("fullName", mapRedis.get(redisKey));
+            list.add(map);
+        }
+        return ActionResult.success(list);
+    }
+
+}

--
Gitblit v1.8.0