From 3ed166b95761aaed736e1f7a4738b49bea62ab81 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Mon, 13 May 2024 17:52:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B6=88=E6=81=AF=E6=A8=A1=E6=9D=BF=E6=A0=87?= =?UTF-8?q?=E9=A2=98=E7=9A=84=E6=96=87=E6=9C=AC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notice/sender/AbstractNoticeSender.java | 16 ++++++++++++++++ .../sender/impl/DingCustomNoticeSender.java | 7 ++++--- .../notice/sender/impl/InSiteNoticeSender.java | 7 ++++--- .../notice/sender/impl/LarkNoticeSender.java | 7 ++++--- .../notice/sender/impl/MailNoticeSender.java | 7 ++++--- .../notice/sender/impl/WeComNoticeSender.java | 7 ++++--- 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/AbstractNoticeSender.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/AbstractNoticeSender.java index 92516f0da0..5dd5eef927 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/AbstractNoticeSender.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/AbstractNoticeSender.java @@ -92,6 +92,22 @@ public abstract class AbstractNoticeSender implements NoticeSender { return MessageTemplateUtils.getContent(context, noticeModel.getParamMap()); } + protected String getSubjectText(MessageDetail messageDetail, NoticeModel noticeModel) { + //处理自定义字段的值 + handleCustomFields(noticeModel); + // 处理 userIds 中包含的特殊值 + noticeModel.setReceivers(getRealUserIds(messageDetail, noticeModel, messageDetail.getEvent())); + // 如果配置了模版就直接使用模版 + if (StringUtils.isNotBlank(messageDetail.getSubject())) { + return MessageTemplateUtils.getContent(messageDetail.getSubject(), noticeModel.getParamMap()); + } + String context = StringUtils.EMPTY; + if (StringUtils.isBlank(context)) { + context = noticeModel.getSubject(); + } + return MessageTemplateUtils.getContent(context, noticeModel.getParamMap()); + } + private void handleCustomFields(NoticeModel noticeModel) { if (!noticeModel.getParamMap().containsKey("fields")) { return; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/DingCustomNoticeSender.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/DingCustomNoticeSender.java index 31dfa62e0e..097385c394 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/DingCustomNoticeSender.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/DingCustomNoticeSender.java @@ -16,7 +16,7 @@ import java.util.stream.Collectors; @Component public class DingCustomNoticeSender extends AbstractNoticeSender { - public void sendDingCustom(MessageDetail messageDetail, NoticeModel noticeModel, String context) { + public void sendDingCustom(MessageDetail messageDetail, NoticeModel noticeModel, String context, String subjectText) { List receivers = super.getReceivers(noticeModel.getReceivers(), noticeModel.isExcludeSelf(), noticeModel.getOperator()); if (CollectionUtils.isEmpty(receivers)) { return; @@ -29,13 +29,14 @@ public class DingCustomNoticeSender extends AbstractNoticeSender { List mobileList = users.stream().map(User::getPhone).toList(); LogUtils.info("钉钉自定义机器人收件人: {}", userIds); - DingClient.send(messageDetail.getWebhook(), messageDetail.getSubject() + ": \n" + context, mobileList); + DingClient.send(messageDetail.getWebhook(), subjectText + ": \n" + context, mobileList); } @Override public void send(MessageDetail messageDetail, NoticeModel noticeModel) { String context = super.getContext(messageDetail, noticeModel); - sendDingCustom(messageDetail, noticeModel, context); + String subjectText = super.getSubjectText(messageDetail, noticeModel); + sendDingCustom(messageDetail, noticeModel, context, subjectText); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/InSiteNoticeSender.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/InSiteNoticeSender.java index efc9a33a95..ec6d701346 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/InSiteNoticeSender.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/InSiteNoticeSender.java @@ -26,7 +26,7 @@ public class InSiteNoticeSender extends AbstractNoticeSender { @Resource private ProjectMapper projectMapper; - public void sendAnnouncement(MessageDetail messageDetail, NoticeModel noticeModel, String context) { + public void sendAnnouncement(MessageDetail messageDetail, NoticeModel noticeModel, String context, String subjectText) { List receivers = super.getReceivers(noticeModel.getReceivers(), noticeModel.isExcludeSelf(), noticeModel.getOperator()); if (CollectionUtils.isEmpty(receivers)) { return; @@ -36,7 +36,7 @@ public class InSiteNoticeSender extends AbstractNoticeSender { receivers.forEach(receiver -> { Map paramMap = noticeModel.getParamMap(); Notification notification = new Notification(); - notification.setSubject(noticeModel.getSubject()); + notification.setSubject(subjectText); notification.setProjectId(messageDetail.getProjectId()); notification.setOrganizationId(project.getOrganizationId()); notification.setOperator(noticeModel.getOperator()); @@ -61,7 +61,8 @@ public class InSiteNoticeSender extends AbstractNoticeSender { @Override public void send(MessageDetail messageDetail, NoticeModel noticeModel) { String context = super.getContext(messageDetail, noticeModel); - sendAnnouncement(messageDetail, noticeModel, context); + String subjectText = super.getSubjectText(messageDetail, noticeModel); + sendAnnouncement(messageDetail, noticeModel, context, subjectText); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/LarkNoticeSender.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/LarkNoticeSender.java index 43cda38062..7b426acb7b 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/LarkNoticeSender.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/LarkNoticeSender.java @@ -17,7 +17,7 @@ import java.util.stream.Collectors; @Component public class LarkNoticeSender extends AbstractNoticeSender { - public void sendLark(MessageDetail messageDetail, NoticeModel noticeModel, String context) { + public void sendLark(MessageDetail messageDetail, NoticeModel noticeModel, String context, String subjectText) { List receivers = super.getReceivers(noticeModel.getReceivers(), noticeModel.isExcludeSelf(), noticeModel.getOperator()); if (CollectionUtils.isEmpty(receivers)) { return; @@ -34,12 +34,13 @@ public class LarkNoticeSender extends AbstractNoticeSender { LogUtils.info("飞书收件人: {}", userIds); context += StringUtils.join(collect, StringUtils.SPACE); - LarkClient.send(messageDetail.getWebhook(), messageDetail.getSubject() + ": \n" + context); + LarkClient.send(messageDetail.getWebhook(), subjectText + ": \n" + context); } @Override public void send(MessageDetail messageDetail, NoticeModel noticeModel) { String context = super.getContext(messageDetail, noticeModel); - sendLark(messageDetail, noticeModel, context); + String subjectText = super.getSubjectText(messageDetail, noticeModel); + sendLark(messageDetail, noticeModel, context, subjectText); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/MailNoticeSender.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/MailNoticeSender.java index 91e134dcf5..32a3dcfcd9 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/MailNoticeSender.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/MailNoticeSender.java @@ -33,7 +33,7 @@ public class MailNoticeSender extends AbstractNoticeSender { @Resource private SystemParameterMapper systemParameterMapper; - public void sendMail(String context, NoticeModel noticeModel, String projectId) throws Exception { + public void sendMail(String context, NoticeModel noticeModel, String projectId, String subjectText) throws Exception { List receivers = super.getReceivers(noticeModel.getReceivers(), noticeModel.isExcludeSelf(), noticeModel.getOperator()); if (CollectionUtils.isEmpty(receivers)) { return; @@ -47,7 +47,7 @@ public class MailNoticeSender extends AbstractNoticeSender { .distinct() .toArray(String[]::new); - send(noticeModel.getSubject(), context, users, new String[0]); + send(subjectText, context, users, new String[0]); } private void send(String subject, String context, String[] users, String[] cc) throws Exception { @@ -157,8 +157,9 @@ public class MailNoticeSender extends AbstractNoticeSender { @Override public void send(MessageDetail messageDetail, NoticeModel noticeModel) { String context = super.getContext(messageDetail, noticeModel); + String subjectText = super.getSubjectText(messageDetail, noticeModel); try { - sendMail(context, noticeModel, messageDetail.getProjectId()); + sendMail(context, noticeModel, messageDetail.getProjectId(), subjectText); LogUtils.debug("发送邮件结束"); } catch (Exception e) { LogUtils.error(e); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/WeComNoticeSender.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/WeComNoticeSender.java index 6f920ae793..425e5d49da 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/WeComNoticeSender.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/impl/WeComNoticeSender.java @@ -16,7 +16,7 @@ import java.util.stream.Collectors; @Component public class WeComNoticeSender extends AbstractNoticeSender { - public void sendWeCom(MessageDetail messageDetail, NoticeModel noticeModel, String context) { + public void sendWeCom(MessageDetail messageDetail, NoticeModel noticeModel, String context, String subjectText) { List receivers = super.getReceivers(noticeModel.getReceivers(), noticeModel.isExcludeSelf(), noticeModel.getOperator()); if (CollectionUtils.isEmpty(receivers)) { return; @@ -28,13 +28,14 @@ public class WeComNoticeSender extends AbstractNoticeSender { List users = super.getUsers(userIds, messageDetail.getProjectId()); List mobileList = users.stream().map(User::getPhone).toList(); LogUtils.info("企业微信收件人: {}", userIds); - WeComClient.send(messageDetail.getWebhook(), messageDetail.getSubject() + ": \n" + context, mobileList); + WeComClient.send(messageDetail.getWebhook(), subjectText + ": \n" + context, mobileList); } @Override public void send(MessageDetail messageDetail, NoticeModel noticeModel) { String context = super.getContext(messageDetail, noticeModel); - sendWeCom(messageDetail, noticeModel, context); + String subjectText = super.getSubjectText(messageDetail, noticeModel); + sendWeCom(messageDetail, noticeModel, context, subjectText); } }