企业微信消息发送测试代码

This commit is contained in:
2025-08-19 17:07:26 +08:00
parent 297853c0cd
commit 062f386ff6
3 changed files with 44 additions and 4 deletions

View File

@@ -1,42 +1,69 @@
package com.srs.flowable.listener.disciplinary;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.WeChatUtil;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.flowable.mapper.DisciplinaryMapper;
import com.srs.flowable.mapper.LeaveMapper;
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 org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
/**
* 根据辅导员的部门id查询该部门的学无干事人员
*/
@Component
@Slf4j
public class XWGSListener implements ExecutionListener {
@Override
public void notify(DelegateExecution delegateExecution) {
DisciplinaryMapper disciplinaryMapper = (DisciplinaryMapper) SpringUtils.getBean("disciplinaryMapper");
DisciplinaryMapper disciplinaryMapper = SpringUtils.getBean(DisciplinaryMapper.class);
Long deptId = (Long) delegateExecution.getVariable("deptId");
if (deptId != null) {
// 步骤 1: 获取审批人的系统ID (userId) 知无涯
Long userId = disciplinaryMapper.getApprovalByDeptIdAndRoleKey(deptId, "stumanger");
if (userId != null) {
// 将审批人ID设置到流程变量中供后续任务节点使用
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='/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("该学院学务干事审批人员未设置");
}
} else {
throw new RuntimeException("未找到部门相关信息");
}
}
}

View File

@@ -16,6 +16,13 @@ public interface DisciplinaryMapper {
*/
Long getApprovalByDeptIdAndRoleKey(@Param("deptId") Long deptId,@Param("roleKey") String roleKey);
/**
* 知无涯 新增根据用户ID查询用户名 (用于企业微信通知)
* @param userId 系统内部的用户ID
* @return 用户的username
*/
String getUserNameByUserId(@Param("userId") Long userId);
/**
* 根据rolekey获取该角色的用户信息审批
* @param roleKey
@@ -23,6 +30,7 @@ public interface DisciplinaryMapper {
*/
List<Long> getApprovalByRoleKey(String roleKey);
int updateRtStuDisciplinaryApplication(StuDisciplinaryApplication rtStuDisciplinaryApplication);
/**

View File

@@ -101,5 +101,10 @@
WHERE c.role_key=#{roleKey}
</select>
<!--知无涯 新增: 根据用户ID查询用户名的SQL实现 -->
<select id="getUserNameByUserId" resultType="java.lang.String">
SELECT user_name FROM sys_user WHERE user_id = #{userId}
</select>
</mapper>