Compare commits
2 Commits
e221e66dfb
...
ff46d9065f
Author | SHA1 | Date | |
---|---|---|---|
ff46d9065f | |||
5481897076 |
@@ -157,7 +157,7 @@ public class WeChatUtil {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
|
||||
|
||||
JSONObject msg = new JSONObject();
|
||||
msg.put("touser", "2023429229");
|
||||
msg.put("touser", toUser);
|
||||
msg.put("msgtype", "text");
|
||||
msg.put("agentid", weChatConfig.getAgentId());
|
||||
|
||||
|
@@ -0,0 +1,124 @@
|
||||
package com.srs.flowable.listener.disciplinary; // 您可以根据需要调整包路径
|
||||
|
||||
import com.srs.common.utils.SecurityUtils;
|
||||
import com.srs.comprehensive.domain.CphMsg;
|
||||
import com.srs.comprehensive.service.ICphMsgService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.flowable.engine.delegate.DelegateExecution;
|
||||
import org.flowable.engine.delegate.ExecutionListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 【通用线上监听器】为即将到来的任务创建内部“我的消息”记录。
|
||||
* <p>
|
||||
* 该监听器被设计为在SequenceFlow的'take'事件上触发。
|
||||
* 它会智能地从流程变量中读取单个接收人(变量名: approval)或多个接收人(变量名: userList),
|
||||
* 然后为每一位接收人创建一条内部消息。
|
||||
* </p>
|
||||
*/
|
||||
@Component("genericMessageListener") // 定义一个清晰、通用的Spring Bean名称
|
||||
@Slf4j
|
||||
public class GenericMessageListener implements ExecutionListener {
|
||||
|
||||
@Autowired
|
||||
private ICphMsgService cphMsgService;
|
||||
|
||||
@Override
|
||||
public void notify(DelegateExecution execution) {
|
||||
log.info("流程实例 [{}]: 触发通用消息监听器 (GenericMessageListener)...", execution.getProcessInstanceId());
|
||||
|
||||
// 步骤 1: 智能地从流程变量中获取接收人ID列表
|
||||
List<Long> receiverIdList = getReceiverIds(execution);
|
||||
|
||||
// 如果没有找到任何接收人,则记录日志并直接返回
|
||||
if (receiverIdList.isEmpty()) {
|
||||
log.warn("流程实例 [{}]: 未能从流程变量中找到任何有效的接收人 (未能解析 'approval' 或 'userList' 变量),已跳过创建内部消息。", execution.getProcessInstanceId());
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤 2: 准备消息内容和发送人
|
||||
// 您可以根据业务需求,让消息内容更具动态性,比如从流程变量中获取任务标题
|
||||
String taskName = execution.getCurrentFlowElement() != null ? execution.getCurrentFlowElement().getName() : "新";
|
||||
String messageContent = "您有一条新的【" + taskName + "】待办任务需要处理,<a href='http://zhxg.gxsdxy.cn/web/#/pages/Approval/index'>请点击前往处理</a>。";
|
||||
Long senderId = getSenderId();
|
||||
|
||||
// 步骤 3: 遍历所有接收人,并为每位用户创建一条内部消息
|
||||
for (Long receiverId : receiverIdList) {
|
||||
try {
|
||||
CphMsg cphMsg = new CphMsg();
|
||||
cphMsg.setReceiver(receiverId);
|
||||
cphMsg.setSender(senderId);
|
||||
cphMsg.setContent(messageContent);
|
||||
|
||||
// 调用您已有的服务插入消息
|
||||
cphMsgService.insertCphMsg(cphMsg);
|
||||
log.info("已成功为用户 [{}] 创建关于任务 [{}] 的内部消息。", receiverId, taskName);
|
||||
|
||||
} catch (Exception e) {
|
||||
// 捕获单个用户创建失败的异常,确保不影响其他人
|
||||
log.error("为用户 [{}] 创建内部消息时发生异常: {}", receiverId, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 智能地从流程变量中解析出接收人ID列表。
|
||||
* 策略是:
|
||||
* 1. 优先检查 'userList' 变量,这通常用于多实例任务。
|
||||
* 2. 如果 'userList' 不存在或无效,则检查 'approval' 变量,这通常用于单实例任务。
|
||||
*
|
||||
* @param execution 当前的执行实例
|
||||
* @return 包含一个或多个用户ID的列表,如果都找不到则返回空列表。
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Long> getReceiverIds(DelegateExecution execution) {
|
||||
// 优先检查 'userList' (用于多实例)
|
||||
Object userListObj = execution.getVariable("userList");
|
||||
if (userListObj instanceof List && !((List<?>) userListObj).isEmpty()) {
|
||||
try {
|
||||
// 确保列表内容是Long类型
|
||||
return (List<Long>) userListObj;
|
||||
} catch (ClassCastException e) {
|
||||
log.error("流程变量 'userList' 不是 Long 类型的列表。", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 其次检查 'approval' (用于单实例)
|
||||
Object approvalObj = execution.getVariable("approval");
|
||||
if (approvalObj instanceof Number) {
|
||||
return Collections.singletonList(((Number) approvalObj).longValue());
|
||||
}
|
||||
// 兼容字符串形式的ID
|
||||
if (approvalObj instanceof String) {
|
||||
try {
|
||||
return Collections.singletonList(Long.parseLong((String) approvalObj));
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("流程变量 'approval' 的值 '{}' 无法转换为Long类型。", approvalObj);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果都找不到,返回一个不可变的空列表
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全地获取当前操作的发送人ID。
|
||||
* 在异步执行的监听器中,可能无法获取到安全上下文,因此提供了默认值。
|
||||
*
|
||||
* @return 发送人用户ID,默认为1L (系统管理员)。
|
||||
*/
|
||||
private Long getSenderId() {
|
||||
try {
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
// 如果SecurityUtils返回null,也使用默认值
|
||||
return userId != null ? userId : 1L;
|
||||
} catch (Exception e) {
|
||||
log.warn("在监听器中获取发送人ID时发生异常,将使用默认系统管理员ID(1L)。异常信息: {}", e.getMessage());
|
||||
return 1L; // 默认为系统管理员
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ import com.srs.flowable.mapper.DisciplinaryMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.flowable.engine.delegate.DelegateExecution;
|
||||
import org.flowable.engine.delegate.ExecutionListener;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -14,6 +15,7 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* 【线上监听器】为“学院违纪处理委员会”多实例任务准备数据并发送通知 知无涯
|
||||
*/
|
||||
@MapperScan("com.srs.flowable.mapper")
|
||||
@Component("xywjclwyhListener")
|
||||
@Slf4j
|
||||
public class XYWJCLWYHListener implements ExecutionListener {
|
||||
|
@@ -265,7 +265,7 @@ public class RtStuDisciplinaryApplicationServiceImpl extends ServiceImpl<RtStuDi
|
||||
|
||||
// todo 企业微信推送消息
|
||||
|
||||
AjaxResult ajaxResult = flowDefinitionService.startProcessInstanceById("flow_n27gxm4k:16:672598", variables);
|
||||
AjaxResult ajaxResult = flowDefinitionService.startProcessInstanceById("flow_n27gxm4k:20:710023", variables);
|
||||
String code = ajaxResult.get("code").toString();
|
||||
if (code.equals("200")) {
|
||||
|
||||
|
Reference in New Issue
Block a user