From cc8c9e3ea8c2e813d44dcac7fe0857154271fb1c Mon Sep 17 00:00:00 2001 From: xiaofajia <1665375861@qq.com> Date: Thu, 10 Oct 2024 12:02:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/iocoder/yudao/common/MessageSend.java | 51 +++++++++++++++++++ .../iocoder/yudao/common/dto/MessageDTO.java | 24 +++++++++ 2 files changed, 75 insertions(+) create mode 100644 dl-module-base/src/main/java/cn/iocoder/yudao/common/MessageSend.java create mode 100644 dl-module-base/src/main/java/cn/iocoder/yudao/common/dto/MessageDTO.java diff --git a/dl-module-base/src/main/java/cn/iocoder/yudao/common/MessageSend.java b/dl-module-base/src/main/java/cn/iocoder/yudao/common/MessageSend.java new file mode 100644 index 00000000..deb65978 --- /dev/null +++ b/dl-module-base/src/main/java/cn/iocoder/yudao/common/MessageSend.java @@ -0,0 +1,51 @@ +package cn.iocoder.yudao.common; + +import cn.hutool.core.collection.CollectionUtil; +import cn.iocoder.yudao.common.dto.MessageDTO; +import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi; +import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0; + +/** + * 用于发送消息 + * + * @author 小李 + * @date 17:15 2024/10/9 + **/ +@Component +public class MessageSend { + + @Resource + private NotifyMessageSendApi messageSendApi; + + /** + * 通用消息发送 + * + * @param messageDTO 消息对象 + * @author 小李 + * @date 9:58 2024/10/10 + **/ + public void send(MessageDTO messageDTO) { + // 设置参数 + Map map = new HashMap<>(); + for (int i = 1; i <= messageDTO.getParamList().size(); i++) { + map.put("key" + i, messageDTO.getParamList().get(i - 1)); + } + if (CollectionUtil.isEmpty(messageDTO.getUserIds())){ + throw exception0(500, "没有接收人"); + } + messageDTO.getUserIds().forEach(id -> { + messageSendApi.sendSingleMessageToAdmin( + new NotifySendSingleToUserReqDTO() + .setUserId(id) + .setTemplateCode(messageDTO.getTemplateCode()) + .setTemplateParams(map)); + }); + } +} diff --git a/dl-module-base/src/main/java/cn/iocoder/yudao/common/dto/MessageDTO.java b/dl-module-base/src/main/java/cn/iocoder/yudao/common/dto/MessageDTO.java new file mode 100644 index 00000000..c1ccb675 --- /dev/null +++ b/dl-module-base/src/main/java/cn/iocoder/yudao/common/dto/MessageDTO.java @@ -0,0 +1,24 @@ +package cn.iocoder.yudao.common.dto; + +import lombok.Data; + +import java.util.List; + +/** + * 用于发消息 + * + * @author 小李 + * @date 17:34 2024/10/9 + **/ +@Data +public class MessageDTO { + + /** 接收方ids 来自system_users */ + private List userIds; + + /** 模板枚举 */ + private String templateCode; + + /** 参数列表 */ + private List paramList; +}