From c55aca77e84607238c3354168a8642176c575ca1 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期二, 10 十一月 2026 11:42:16 +0800
Subject: [PATCH] feat(bpm): 待办任务站内信实时推送

---
 yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/security/LoginUserHandshakeInterceptor.java |    7 ++
 yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImpl.java                                              |   32 ++++++++++
 yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/dto/NotifyMessagePushDTO.java                                           |   40 +++++++++++++
 yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/message/BpmMessageServiceImpl.java                                                   |   57 ++++++++++++++++++-
 sql/mysql/20261110_bpm_task_notify_template.sql                                                                                                         |   25 ++++++++
 yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/session/WebSocketSessionManagerImpl.java    |    6 +
 6 files changed, 161 insertions(+), 6 deletions(-)

diff --git a/sql/mysql/20261110_bpm_task_notify_template.sql b/sql/mysql/20261110_bpm_task_notify_template.sql
new file mode 100644
index 0000000..8ac8b7c
--- /dev/null
+++ b/sql/mysql/20261110_bpm_task_notify_template.sql
@@ -0,0 +1,25 @@
+-- =============================================================================
+-- BPM 寰呭姙浠诲姟绔欏唴淇¢�氱煡妯℃澘
+-- 鐩爣搴擄細mom-ntsh
+-- 鑳屾櫙锛欱pmMessageServiceImpl#sendMessageWhenTaskAssigned 宸叉敼涓哄彂閫佺珯鍐呬俊锛�
+--       渚濊禆 system_notify_template 涓瓨鍦� code=bpm_task_assigned 鐨勬ā鏉匡紝
+--       缂哄け鏃朵細鎶涖�岀珯鍐呬俊妯℃澘涓嶅瓨鍦ㄣ�嶅苟璺宠繃鍙戦�侊紙宸叉崟鑾峰紓甯革紝涓嶅奖鍝嶆祦绋嬫彁浜わ級銆�
+-- 鍙傛暟锛歱rocessName锛堟祦绋嬪悕绉帮級銆乼askName锛堜换鍔¤妭鐐癸級銆乻tartUserName锛堟彁浜や汉锛�
+-- 骞傜瓑锛歝ode 宸插瓨鍦ㄥ垯璺宠繃锛屽彲閲嶅鎵ц銆�
+-- =============================================================================
+
+SET NAMES utf8mb4;
+
+INSERT INTO system_notify_template
+    (name, code, nickname, content, type, params, status, remark, creator, updater)
+SELECT '娴佺▼寰呭姙浠诲姟鎻愰啋', 'bpm_task_assigned', '瀹℃壒涓績',
+       '鎮ㄦ湁鏂扮殑寰呭姙浠诲姟锛歿processName} -銆寋taskName}銆嶏紝鎻愪氦浜猴細{startUserName}銆傝鍒般�屽鎵逛腑蹇� 鈫� 寰呭姙浠诲姟銆嶅強鏃跺鐞嗐��',
+       1, '["processName","taskName","startUserName"]', 0,
+       'BPM 浠诲姟鍒嗛厤鍒板鎵逛汉鏃跺彂閫侊紝鐐瑰嚮閫氱煡鐩存帴璺宠浆寰呭姙浠诲姟', '1', '1'
+ WHERE NOT EXISTS (SELECT 1 FROM system_notify_template
+                    WHERE code = 'bpm_task_assigned' AND deleted = b'0');
+
+-- 鏍¢獙
+SELECT id, name, code, nickname, content, params, status
+  FROM system_notify_template
+ WHERE code = 'bpm_task_assigned' AND deleted = b'0';
diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/security/LoginUserHandshakeInterceptor.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/security/LoginUserHandshakeInterceptor.java
index d4fb515..30663bc 100644
--- a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/security/LoginUserHandshakeInterceptor.java
+++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/security/LoginUserHandshakeInterceptor.java
@@ -1,5 +1,6 @@
 package cn.iocoder.yudao.framework.websocket.core.security;
 
+import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
 import cn.iocoder.yudao.framework.security.core.LoginUser;
 import cn.iocoder.yudao.framework.security.core.filter.TokenAuthenticationFilter;
 import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
@@ -30,6 +31,12 @@
         if (loginUser == null) {
             return false;
         }
+        // WebSocket 鍦板潃锛堝 /infra/ws锛変笉甯� /admin-api銆�/app-api 鍓嶇紑锛�
+        // 鍥犳 mock 鐧诲綍鏃� WebFrameworkUtils#getLoginUserType 鍙栦笉鍒扮敤鎴风被鍨嬨��
+        // 杩欓噷鍏滃簳涓虹鐞嗗憳绫诲瀷锛岄伩鍏� WebSocketSessionManagerImpl 鐢� null 浣滀负 key 璁板綍浼氳瘽鏃舵姏 NPE 鏂紑杩炴帴銆�
+        if (loginUser.getUserType() == null) {
+            loginUser.setUserType(UserTypeEnum.ADMIN.getValue());
+        }
         WebSocketFrameworkUtils.setLoginUser(loginUser, attributes);
         return true;
     }
diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/session/WebSocketSessionManagerImpl.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/session/WebSocketSessionManagerImpl.java
index 9b9b933..2e73801 100644
--- a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/session/WebSocketSessionManagerImpl.java
+++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/session/WebSocketSessionManagerImpl.java
@@ -43,7 +43,8 @@
         idSessions.put(session.getId(), session);
         // 娣诲姞鍒� userSessions 涓�
         LoginUser user = WebSocketFrameworkUtils.getLoginUser(session);
-        if (user == null) {
+        // 鐢ㄦ埛鎴栫敤鎴风被鍨嬩负绌烘椂涓嶅鐞嗭細ConcurrentHashMap 涓嶅厑璁� null key锛屼細鐩存帴鎶� NPE 鏂紑杩炴帴
+        if (user == null || user.getUserType() == null || user.getId() == null) {
             return;
         }
         ConcurrentMap<Long, CopyOnWriteArrayList<WebSocketSession>> userSessionsMap = userSessions.get(user.getUserType());
@@ -69,7 +70,8 @@
         idSessions.remove(session.getId());
         // 绉婚櫎浠� idSessions 涓�
         LoginUser user = WebSocketFrameworkUtils.getLoginUser(session);
-        if (user == null) {
+        // 鐢ㄦ埛鎴栫敤鎴风被鍨嬩负绌烘椂涓嶅鐞嗭細ConcurrentHashMap 涓嶅厑璁� null key锛屼細鐩存帴鎶� NPE 鏂紑杩炴帴
+        if (user == null || user.getUserType() == null || user.getId() == null) {
             return;
         }
         ConcurrentMap<Long, CopyOnWriteArrayList<WebSocketSession>> userSessionsMap = userSessions.get(user.getUserType());
diff --git a/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/message/BpmMessageServiceImpl.java b/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/message/BpmMessageServiceImpl.java
index 0175358..af93ad3 100644
--- a/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/message/BpmMessageServiceImpl.java
+++ b/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/message/BpmMessageServiceImpl.java
@@ -1,18 +1,25 @@
 package cn.iocoder.yudao.module.bpm.service.message;
 
+import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceApproveReqDTO;
 import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceRejectReqDTO;
 import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
 import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenTaskTimeoutReqDTO;
+import cn.iocoder.yudao.module.system.service.notify.NotifySendService;
+import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
+import org.flowable.engine.TaskService;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
+
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * BPM 娑堟伅 Service 瀹炵幇绫�
  *
- * 娉ㄦ剰锛氬凡鍏抽棴鐭俊閫氱煡鍔熻兘锛岀浉鍏虫柟娉曚粎璁板綍鏃ュ織锛屼笉鍐嶈皟鐢ㄧ煭淇″彂閫佹帴鍙o紝
- * 浠ラ伩鍏嶁�滅煭淇℃ā鏉夸笉瀛樺湪鈥濈瓑闂銆�
+ * 褰撳墠鍙繚鐣欍�愬緟鍔炰换鍔°�戠殑绔欏唴淇¢�氱煡锛氫换鍔″垎閰嶅埌鍏蜂綋瀹℃壒浜烘椂锛岀粰瀹℃壒浜哄彂涓�鏉$珯鍐呬俊锛堢郴缁熷彸涓婅灏忛搩閾涘彲瑙侊級銆�
+ * 鐭俊閫氱煡宸叉暣浣撳叧闂紝鍏朵綑鏂规硶浠呰褰曟棩蹇椼��
  *
  * @author 瓒呯骇绠$悊鍛�
  */
@@ -20,6 +27,24 @@
 @Validated
 @Slf4j
 public class BpmMessageServiceImpl implements BpmMessageService {
+
+    /**
+     * 寰呭姙浠诲姟绔欏唴淇℃ā鏉跨紪鐮侊紝瀵瑰簲 system_notify_template.code
+     */
+    private static final String TEMPLATE_CODE_TASK_ASSIGNED = "bpm_task_assigned";
+
+    /**
+     * 绔欏唴淇″弬鏁板�肩殑鏈�澶ч暱搴︺��
+     * 璇存槑锛歴ystem_notify_message.template_params 鍙湁 varchar(255)锛屽弬鏁板�艰繃闀夸細瀵艰嚧鎻掑叆澶辫触锛�
+     * 杩欓噷缁熶竴鎴柇锛屼繚璇佷竴鏉″緟鍔炲繀瀹氳兘鍙戝嚭鍘汇��
+     */
+    private static final int PARAM_VALUE_MAX_LENGTH = 60;
+
+    @Resource
+    private NotifySendService notifySendService;
+
+    @Resource
+    private TaskService flowTaskService;
 
     @Override
     public void sendMessageWhenProcessInstanceApprove(BpmMessageSendWhenProcessInstanceApproveReqDTO reqDTO) {
@@ -31,9 +56,35 @@
         log.info("[sendMessageWhenProcessInstanceReject][宸插叧闂煭淇¢�氱煡锛岃烦杩囧彂閫乚[processInstanceId: {}]", reqDTO.getProcessInstanceId());
     }
 
+    /**
+     * 浠诲姟鍒嗛厤鍒板鎵逛汉鏃讹紝鍙戦�佺珯鍐呬俊閫氱煡銆�
+     *
+     * 璇存槑锛氭墍鏈夋祦绋嬭妭鐐圭殑寰呭姙浠诲姟閮戒細璧� BpmTaskServiceImpl#processTaskAssigned 鐨勬湰娆¤皟鐢紙鍔犵銆佽浆鍔炲悓鏍蜂細瑙﹀彂锛夛紝
+     * 鍥犳杩欓噷瀹炵幇涓�娆″嵆鍙鐩栧叏閮ㄦ祦绋嬬殑寰呭姙閫氱煡銆�
+     */
     @Override
     public void sendMessageWhenTaskAssigned(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
-        log.info("[sendMessageWhenTaskAssigned][宸插叧闂煭淇¢�氱煡锛岃烦杩囧彂閫乚[processInstanceId: {}]", reqDTO.getProcessInstanceId());
+        if (reqDTO.getAssigneeUserId() == null) {
+            return;
+        }
+        // 浠诲姟鍙兘鍦ㄥ悓涓�浜嬪姟閲屽凡琚嚜鍔ㄩ�氳繃銆佽嚜鍔ㄦ嫆缁濇垨鍙栨秷锛堝厤瀹℃壒寮�鍏炽�佹垨绛捐鎶㈠崰绛夛級锛屾鏃跺彂寰呭姙閫氱煡鍙細璇瀹℃壒浜�
+        if (flowTaskService.createTaskQuery().taskId(reqDTO.getTaskId()).count() == 0) {
+            log.info("[sendMessageWhenTaskAssigned][taskId({}) 宸蹭笉鍦ㄥ緟鍔炰腑锛岃烦杩囧緟鍔為�氱煡]", reqDTO.getTaskId());
+            return;
+        }
+        Map<String, Object> templateParams = new HashMap<>(4);
+        templateParams.put("processName", StrUtil.maxLength(reqDTO.getProcessInstanceName(), PARAM_VALUE_MAX_LENGTH));
+        templateParams.put("taskName", StrUtil.maxLength(reqDTO.getTaskName(), PARAM_VALUE_MAX_LENGTH));
+        templateParams.put("startUserName", StrUtil.maxLength(reqDTO.getStartUserNickname(), PARAM_VALUE_MAX_LENGTH));
+        try {
+            notifySendService.sendSingleNotifyToAdmin(reqDTO.getAssigneeUserId(), TEMPLATE_CODE_TASK_ASSIGNED, templateParams);
+            log.info("[sendMessageWhenTaskAssigned][宸插悜鐢ㄦ埛({})鍙戦�佸緟鍔炵珯鍐呬俊][taskId({}) taskName({})]",
+                    reqDTO.getAssigneeUserId(), reqDTO.getTaskId(), reqDTO.getTaskName());
+        } catch (Exception ex) {
+            // 绔欏唴淇″彂閫佸け璐ヤ笉鑳藉奖鍝嶅鎵逛换鍔℃湰韬殑鍒涘缓锛屽惁鍒欐暣涓祦绋嬫彁浜や簨鍔′細琚洖婊�
+            log.error("[sendMessageWhenTaskAssigned][鍚戠敤鎴�({})鍙戦�佸緟鍔炵珯鍐呬俊澶辫触][taskId({})]",
+                    reqDTO.getAssigneeUserId(), reqDTO.getTaskId(), ex);
+        }
     }
 
     @Override
diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImpl.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImpl.java
index a47d7fe..501f2f9 100644
--- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImpl.java
+++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImpl.java
@@ -2,7 +2,9 @@
 
 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
+import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
+import cn.iocoder.yudao.module.system.service.notify.dto.NotifyMessagePushDTO;
 import com.google.common.annotations.VisibleForTesting;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
@@ -31,6 +33,9 @@
     @Resource
     private NotifyMessageService notifyMessageService;
 
+    @Resource
+    private WebSocketSenderApi webSocketSenderApi;
+
     @Override
     public Long sendSingleNotifyToAdmin(Long userId, String templateCode, Map<String, Object> templateParams) {
         return sendSingleNotify(userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams);
@@ -54,7 +59,32 @@
 
         // 鍙戦�佺珯鍐呬俊
         String content = notifyTemplateService.formatNotifyTemplateContent(template.getContent(), templateParams);
-        return notifyMessageService.createNotifyMessage(userId, userType, template, content, templateParams);
+        Long messageId = notifyMessageService.createNotifyMessage(userId, userType, template, content, templateParams);
+        // 瀹炴椂鎺ㄩ�侊細鍓嶇閾冮摏鏀跺埌鍚庣珛鍗冲埛鏂版湭璇绘暟锛岄伩鍏嶅彧鑳界瓑寰呰疆璇�
+        pushToWebSocket(userId, userType, messageId, template, content);
+        return messageId;
+    }
+
+    /**
+     * 閫氳繃 WebSocket 鎶婄珯鍐呬俊瀹炴椂鎺ㄩ�佺粰鐢ㄦ埛锛堣鐢ㄦ埛鎵�鏈夊湪绾块〉闈㈤兘浼氭敹鍒帮級
+     *
+     * 璇存槑锛氭帹閫佸彧鏄�滄彁鍓嶉�氱煡鍓嶇鍒锋柊鈥濓紝鎺ㄩ�佸け璐ヤ笉褰卞搷绔欏唴淇℃湰韬紝鍥犳杩欓噷鍚炴帀寮傚父浠呰褰曟棩蹇椼��
+     */
+    private void pushToWebSocket(Long userId, Integer userType, Long messageId,
+                                 NotifyTemplateDO template, String content) {
+        if (messageId == null) {
+            return;
+        }
+        NotifyMessagePushDTO message = new NotifyMessagePushDTO();
+        message.setMessageId(messageId);
+        message.setTemplateCode(template.getCode());
+        message.setTemplateNickname(template.getNickname());
+        message.setTemplateContent(content);
+        try {
+            webSocketSenderApi.sendObject(userType, userId, NotifyMessagePushDTO.MESSAGE_TYPE, message);
+        } catch (Exception ex) {
+            log.warn("[pushToWebSocket][鍚戠敤鎴�({}/{})鎺ㄩ�佺珯鍐呬俊({})澶辫触]", userId, userType, messageId, ex);
+        }
     }
 
     @VisibleForTesting
diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/dto/NotifyMessagePushDTO.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/dto/NotifyMessagePushDTO.java
new file mode 100644
index 0000000..baa90de
--- /dev/null
+++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/dto/NotifyMessagePushDTO.java
@@ -0,0 +1,40 @@
+package cn.iocoder.yudao.module.system.service.notify.dto;
+
+import lombok.Data;
+
+/**
+ * 绔欏唴淇� WebSocket 瀹炴椂鎺ㄩ�佺殑娑堟伅浣�
+ *
+ * 鍓嶇鏀跺埌 type = notify-message 鐨勬秷鎭悗锛屽埛鏂版湭璇绘暟閲忓拰鏈鍒楄〃锛屾棤闇�绛夊緟杞銆�
+ *
+ * @author 瓒呯骇绠$悊鍛�
+ */
+@Data
+public class NotifyMessagePushDTO {
+
+    /**
+     * 娑堟伅绫诲瀷锛氱珯鍐呬俊
+     */
+    public static final String MESSAGE_TYPE = "notify-message";
+
+    /**
+     * 绔欏唴淇$紪鍙�
+     */
+    private Long messageId;
+
+    /**
+     * 绔欏唴淇℃ā鏉跨紪鐮�
+     */
+    private String templateCode;
+
+    /**
+     * 绔欏唴淇″彂閫佷汉鍚嶇О
+     */
+    private String templateNickname;
+
+    /**
+     * 绔欏唴淇″唴瀹癸紙宸叉牸寮忓寲锛�
+     */
+    private String templateContent;
+
+}

--
Gitblit v1.9.3