feat(项目管理): 增加消息模板标题的文本处理

This commit is contained in:
guoyuqi 2024-05-13 17:52:44 +08:00 committed by 刘瑞斌
parent bfbd4b138c
commit 3ed166b957
6 changed files with 36 additions and 15 deletions

View File

@ -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;

View File

@ -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<Receiver> receivers = super.getReceivers(noticeModel.getReceivers(), noticeModel.isExcludeSelf(), noticeModel.getOperator());
if (CollectionUtils.isEmpty(receivers)) {
return;
@ -29,13 +29,14 @@ public class DingCustomNoticeSender extends AbstractNoticeSender {
List<String> 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);
}
}

View File

@ -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<Receiver> 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<String, Object> 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);
}
}

View File

@ -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<Receiver> 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);
}
}

View File

@ -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<Receiver> 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);

View File

@ -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<Receiver> receivers = super.getReceivers(noticeModel.getReceivers(), noticeModel.isExcludeSelf(), noticeModel.getOperator());
if (CollectionUtils.isEmpty(receivers)) {
return;
@ -28,13 +28,14 @@ public class WeComNoticeSender extends AbstractNoticeSender {
List<User> users = super.getUsers(userIds, messageDetail.getProjectId());
List<String> 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);
}
}