企业微信消息发送测试代码(截至到学校违纪委员会意见

This commit is contained in:
2025-08-20 15:33:22 +08:00
parent 3c5818dee2
commit 9eb3cfd422
9 changed files with 129 additions and 9 deletions

View File

@@ -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", toUser);
msg.put("touser", "2023429229");
msg.put("msgtype", "text");
msg.put("agentid", weChatConfig.getAgentId());

View File

@@ -1,5 +1,6 @@
package com.srs.flowable.listener.disciplinary;
import com.srs.common.utils.WeChatUtil;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.flowable.mapper.DisciplinaryMapper;
import lombok.extern.slf4j.Slf4j;
@@ -14,9 +15,10 @@ import org.springframework.stereotype.Component;
@Slf4j
public class EJXYSJListener implements ExecutionListener {
@Override
public void notify(DelegateExecution delegateExecution) {
DisciplinaryMapper disciplinaryMapper = (DisciplinaryMapper)SpringUtils.getBean("disciplinaryMapper");
DisciplinaryMapper disciplinaryMapper = SpringUtils.getBean("disciplinaryMapper");
Long deptId = (Long) delegateExecution.getVariable("deptId");
@@ -24,7 +26,29 @@ public class EJXYSJListener implements ExecutionListener {
if (userId!=null){
delegateExecution.setVariable("approval", userId);
// todo 企业微信推送消息
// --- 企业微信推送消息模块 ---
try {
// 步骤 2: 【关键】使用 userId 查询对应的企业微信账号 (userName)
String userName = disciplinaryMapper.getUserNameByUserId(userId);
// 步骤 3: 检查 userName 是否存在
if (userName != null && !userName.isEmpty()) {
WeChatUtil weChatUtil = SpringUtils.getBean(WeChatUtil.class);
// 构造包含超链接的消息内容
String content = "您有一条新的学生违纪审批任务待处理,<a href='http://zhxg.gxsdxy.cn/web/#/pages/Approval/index'>请点击前往处理</a>。";
// 步骤 4: 【关键】使用 userName 作为接收人发送消息
weChatUtil.sendTextMessage(userName, content);
log.info("已成功向二级学院书记(userName:{})发送企业微信审批通知。", userName);
} else {
// 如果找不到userName记录警告日志但流程继续
log.warn("找到了二级学院书记审批人(userId:{}),但其对应的企业微信账号(userName)为空,无法发送通知。", userId);
}
} catch (Exception e) {
// 保证即使通知失败,流程也能继续
log.error("向二级学院书记(userId:{})发送企业微信通知时出现异常。流程将继续。错误详情: {}", userId, e.getMessage(), e);
}
}else{
throw new RuntimeException("该二级学院书记审批人未配置");
}

View File

@@ -19,6 +19,5 @@ public class StuInfoListener implements ExecutionListener {
log.info("学生信息:{}",stuId);
delegateExecution.setVariable("approval",stuId);
// todo 企业微信推送消息
}
}

View File

@@ -0,0 +1,59 @@
package com.srs.flowable.listener.disciplinary;
import com.srs.common.utils.WeChatUtil;
import com.srs.common.utils.spring.SpringUtils;
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.springframework.stereotype.Component;
import java.util.List;
/**
* 学生教育管理科审核节点的执行监听器。
* 向“学生教育管理科”角色发送企业微信通知。
*/
@Component
@Slf4j
public class XSJYGLKListener implements ExecutionListener {
@Override
public void notify(DelegateExecution delegateExecution) {
DisciplinaryMapper disciplinaryMapper = SpringUtils.getBean(DisciplinaryMapper.class);
// 1. 【核心】定义“学生教育管理科”xsjyglksh
final String TARGET_ROLE_KEY = "xsjyglksh";
log.info("流程实例 [{}]: 准备向角色 '{}' 发送通知。", delegateExecution.getProcessInstanceId(), TARGET_ROLE_KEY);
// 2. 根据这个固定的角色Key查询该角色的所有成员ID即使只有一个
List<Long> userIdList = disciplinaryMapper.getApprovalByRoleKey(TARGET_ROLE_KEY);
// 3. 检查是否找到了成员。
if (userIdList == null || userIdList.isEmpty()) {
log.error("根据角色Key '{}' 未找到任何用户,无法发送通知。", TARGET_ROLE_KEY);
return; // 中止执行,不抛出异常以免中断流程
}
// 4. 发送企业微信通知。
// (我们的批量发送逻辑即使只有一个用户也能完美处理)
try {
// 批量查询userName (即使只有一个ID这个方法也能正常工作)
List<String> userNameList = disciplinaryMapper.getUserNamesByUserIdList(userIdList);
if (!userNameList.isEmpty()) {
// 将列表拼接成 "username1|username2|..." 的格式
String toUser = String.join("|", userNameList);
WeChatUtil weChatUtil = SpringUtils.getBean(WeChatUtil.class);
String content = "您有一条新的学生违纪审批任务待处理,<a href='http://zhxg.gxsdxy.cn/web/#/pages/Approval/index'>请点击前往处理</a>。";
weChatUtil.sendTextMessage(toUser, content);
log.info("已成功向角色 '{}' 的成员发送通知。接收人: {}", TARGET_ROLE_KEY, toUser);
} else {
log.warn("角色 '{}' 中找到了 {} 个用户,但无人配置企业微信账号,未发送任何通知。", TARGET_ROLE_KEY, userIdList.size());
}
} catch (Exception e) {
log.error("向角色 '{}' 发送企业微信通知时出现异常。流程将继续。错误详情: {}", TARGET_ROLE_KEY, e.getMessage(), e);
}
}
}

View File

@@ -45,7 +45,7 @@ public class XWGSListener implements ExecutionListener {
WeChatUtil weChatUtil = SpringUtils.getBean(WeChatUtil.class);
// 构造包含超链接的消息内容
String content = "您有一条新的学生违纪审批任务待处理,<a href='/pages/Approval/index'>请点击前往处理</a>。";
String content = "您有一条新的学生违纪审批任务待处理,<a href='http://zhxg.gxsdxy.cn/web/#/pages/Approval/index'>请点击前往处理</a>。";
// 步骤 4: 使用 userName 作为接收人发送消息
weChatUtil.sendTextMessage(userName, content);

View File

@@ -1,5 +1,6 @@
package com.srs.flowable.listener.disciplinary;
import com.srs.common.utils.WeChatUtil;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.flowable.mapper.DisciplinaryMapper;
import lombok.extern.slf4j.Slf4j;
@@ -18,15 +19,36 @@ public class XYWJCLWYHListener implements ExecutionListener {
@Override
public void notify(DelegateExecution delegateExecution) {
DisciplinaryMapper disciplinaryMapper = (DisciplinaryMapper)SpringUtils.getBean("disciplinaryMapper");
List<Long> xywjclwyh = disciplinaryMapper.getApprovalByRoleKey("xywjclwyh");
if (xywjclwyh.size() == 0){
DisciplinaryMapper disciplinaryMapper = SpringUtils.getBean("disciplinaryMapper");
// 步骤 1: 根据角色Key获取审批人ID列表 (第1次DB查询)
List<Long> userIdList = disciplinaryMapper.getApprovalByRoleKey("xywjclwyh");
if (userIdList == null || userIdList.isEmpty()){
log.error("未找到角色相关信息");
throw new RuntimeException("学院违纪处理委员会审批人员未设置");
}else{
delegateExecution.setVariable("userList",xywjclwyh);
delegateExecution.setVariable("userList",userIdList);
// todo 企业微信推送消息
// --- 企业微信推送消息模块 ---
try {
// 步骤 2: 一次性批量查询所有有效的userName (第2次DB查询性能最优)
List<String> userNameList = disciplinaryMapper.getUserNamesByUserIdList(userIdList);
if (!userNameList.isEmpty()) {
// 步骤 3: 将 userName 列表用 "|" 连接成一个字符串
String toUser = String.join("|", userNameList);
WeChatUtil weChatUtil = SpringUtils.getBean(WeChatUtil.class);
String content = "您有一条新的学生违纪审批任务待处理,<a href='http://zhxg.gxsdxy.cn/web/#/pages/Approval/index'>请点击前往处理</a>。";
weChatUtil.sendTextMessage(toUser, content);
log.info("已成功向学院违纪处理委员会发送群组通知。接收人: {}", toUser);
} else {
log.warn("角色 'xywjclwyh' 存在审批人,但无人配置企业微信账号,未发送任何通知。");
}
} catch (Exception e) {
// 保证即使通知失败,流程也能继续
log.error("向学院违纪处理委员会发送企业微信通知时出现异常。流程将继续。错误详情: {}", e.getMessage(), e);
}
}
}

View File

@@ -22,6 +22,12 @@ public interface DisciplinaryMapper {
* @return 用户的username
*/
String getUserNameByUserId(@Param("userId") Long userId);
/**
* 知无涯新增根据用户ID列表批量查询用户名列表
* @param userIdList 用户ID列表
* @return 用户的username列表
*/
List<String> getUserNamesByUserIdList(@Param("userIdList") List<Long> userIdList);
/**
* 根据rolekey获取该角色的用户信息审批

View File

@@ -106,5 +106,15 @@
SELECT user_name FROM sys_user WHERE user_id = #{userId}
</select>
<!-- 知无涯 新增批量查询用户名的SQL实现 -->
<select id="getUserNamesByUserIdList" resultType="java.lang.String">
SELECT user_name FROM sys_user
WHERE user_id IN
<foreach item="userId" collection="userIdList" open="(" separator="," close=")">
#{userId}
</foreach>
AND user_name IS NOT NULL AND user_name != ''
</select>
</mapper>

View File

@@ -265,7 +265,7 @@ public class RtStuDisciplinaryApplicationServiceImpl extends ServiceImpl<RtStuDi
// todo 企业微信推送消息
AjaxResult ajaxResult = flowDefinitionService.startProcessInstanceById("flow_n27gxm4k:7:257682", variables);
AjaxResult ajaxResult = flowDefinitionService.startProcessInstanceById("flow_n27gxm4k:8:610039", variables);
String code = ajaxResult.get("code").toString();
if (code.equals("200")) {