From cf3136d22deb54e01503982d0a58a29609c53d9c Mon Sep 17 00:00:00 2001 From: lizhongyuan3 Date: Thu, 14 Dec 2023 21:30:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/alarm/QiYeWechatAttribute.java | 19 ++++ .../core/alarm/strategy/QiYeWechatAlarm.java | 88 +++++++++++++++++++ .../retry/common/core/util/DingDingUtils.java | 25 +----- frontend/src/utils/jobEnum.js | 4 + frontend/src/utils/retryEnum.js | 4 + frontend/src/views/job/form/JobNotifyForm.vue | 29 +++++- frontend/src/views/task/form/NotifyForm.vue | 29 +++++- 7 files changed, 174 insertions(+), 24 deletions(-) create mode 100644 easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/alarm/QiYeWechatAttribute.java create mode 100644 easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/alarm/strategy/QiYeWechatAlarm.java diff --git a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/alarm/QiYeWechatAttribute.java b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/alarm/QiYeWechatAttribute.java new file mode 100644 index 00000000..f22d97c0 --- /dev/null +++ b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/alarm/QiYeWechatAttribute.java @@ -0,0 +1,19 @@ +package com.aizuda.easy.retry.common.core.alarm; + +import lombok.Data; + +import java.util.List; + +/** + * 企业微信 + * + * @author lizhongyuan + */ +@Data +public class QiYeWechatAttribute { + + private String webhookUrl; + + private List ats; + +} diff --git a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/alarm/strategy/QiYeWechatAlarm.java b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/alarm/strategy/QiYeWechatAlarm.java new file mode 100644 index 00000000..91f7a277 --- /dev/null +++ b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/alarm/strategy/QiYeWechatAlarm.java @@ -0,0 +1,88 @@ +package com.aizuda.easy.retry.common.core.alarm.strategy; + +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.*; +import com.aizuda.easy.retry.common.core.alarm.AlarmContext; +import com.aizuda.easy.retry.common.core.alarm.QiYeWechatAttribute; +import com.aizuda.easy.retry.common.core.enums.AlarmTypeEnum; +import com.aizuda.easy.retry.common.core.log.LogUtils; +import com.aizuda.easy.retry.common.core.util.DingDingUtils; +import com.aizuda.easy.retry.common.core.util.JsonUtil; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; + + +/** + * 企业微信通知 + * + * @author lizhongyuan + */ +@Slf4j +@Component +public class QiYeWechatAlarm extends AbstractAlarm { + + public static final String atLabel = "<@{0}>"; + + @Override + public Integer getAlarmType() { + return AlarmTypeEnum.QI_YE_WECHAT.getValue(); + } + + @Override + public boolean asyncSendMessage(AlarmContext context) { + threadPoolExecutor.execute(() -> syncSendMessage(context)); + return true; + } + + @Override + public boolean syncSendMessage(AlarmContext context) { + try { + QiYeWechatAttribute qiYeWechatAttribute = JsonUtil.parseObject(context.getNotifyAttribute(), QiYeWechatAttribute.class); + String webhookUrl = qiYeWechatAttribute.getWebhookUrl(); + if (StrUtil.isBlank(webhookUrl)) { + log.error("请先配置微信机器人 webhookUrl"); + return false; + } + Map map = MapUtil.newHashMap(); + QiYeWechatMessageContent messageContent = new QiYeWechatMessageContent(); + messageContent.setContent(StrUtil.sub(DingDingUtils.getAtText(qiYeWechatAttribute.getAts(), context.getText(), atLabel), 0, 4096)); + map.put("msgtype", "markdown"); + map.put("markdown", messageContent); + HttpRequest post = HttpUtil.createPost(webhookUrl); + HttpRequest request = post.body(JsonUtil.toJsonString(map), ContentType.JSON.toString()); + HttpResponse execute = request.execute(); + LogUtils.debug(log, JsonUtil.toJsonString(execute)); + } catch (Exception e) { + log.error("发送企业微信消息失败", e); + return false; + } + return true; + + } + + @Override + public boolean asyncSendMessage(List alarmContexts) { + + for (AlarmContext alarmContext : alarmContexts) { + asyncSendMessage(alarmContext); + } + + return Boolean.TRUE; + } + + + @Data + private static class QiYeWechatMessageContent { + /** + * markdown内容,最长不超过4096个字节,必须是utf8编码 + */ + private String content; + } + +} + diff --git a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/util/DingDingUtils.java b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/util/DingDingUtils.java index 80065baa..95d1001d 100644 --- a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/util/DingDingUtils.java +++ b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/util/DingDingUtils.java @@ -8,13 +8,9 @@ import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; import java.text.MessageFormat; -import java.util.Arrays; -import java.util.HashSet; import java.util.List; -import java.util.stream.Collectors; /** * @author: www.byteblogs.com @@ -25,22 +21,6 @@ public class DingDingUtils { public static final String atLabel = "@{0}"; - /** - * 防止文本过长钉钉限流,目前最大为4000 - * - * @param text - * @return - */ - private static String subTextLength(String text) { - int length = text.length(); - - if (length > 4000) { - return text.substring(0, 4000); - } else { - return text; - } - } - /** * 组装OapiRobotSendRequest * @@ -53,7 +33,8 @@ public class DingDingUtils { request.setMsgtype("markdown"); OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); markdown.setTitle(title); - markdown.setText(subTextLength(getAtText(ats, text))); + // 防止文本过长钉钉限流,目前最大为4000 + markdown.setText(StrUtil.sub(getAtText(ats, text, atLabel), 0, 4000)); request.setMarkdown(markdown); OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); @@ -65,7 +46,7 @@ public class DingDingUtils { return request; } - public static String getAtText(List ats, String text) { + public static String getAtText(List ats, String text, String atLabel) { if (CollectionUtils.isEmpty(ats)) { return text; } diff --git a/frontend/src/utils/jobEnum.js b/frontend/src/utils/jobEnum.js index 54e62b25..127e059d 100644 --- a/frontend/src/utils/jobEnum.js +++ b/frontend/src/utils/jobEnum.js @@ -167,6 +167,10 @@ const enums = { 'name': '邮箱通知', 'color': '#1b7ee5' }, + '3': { + 'name': '企业微信', + 'color': '#0082EF' + }, '4': { 'name': '飞书', 'color': '#087da1' diff --git a/frontend/src/utils/retryEnum.js b/frontend/src/utils/retryEnum.js index a79f11ad..0f6ba803 100644 --- a/frontend/src/utils/retryEnum.js +++ b/frontend/src/utils/retryEnum.js @@ -90,6 +90,10 @@ const enums = { 'name': '邮箱通知', 'color': '#1b7ee5' }, + '3': { + 'name': '企业微信', + 'color': '#0082EF' + }, '4': { 'name': '飞书', 'color': '#087da1' diff --git a/frontend/src/views/job/form/JobNotifyForm.vue b/frontend/src/views/job/form/JobNotifyForm.vue index 6636e86c..deafef2b 100644 --- a/frontend/src/views/job/form/JobNotifyForm.vue +++ b/frontend/src/views/job/form/JobNotifyForm.vue @@ -164,7 +164,8 @@ {rules: [{ required: true, message: '请输入钉钉URL', whitespace: true}]} ]" /> - + 被@人手机号或钉钉号  + + + + + 被@人企业微信用户id  + + @@ -493,6 +516,10 @@ export default { s = '钉钉Url:' + json['webhookUrl'] + ';' + '被@人手机号:' + json['ats'] + ';' + } else if (this.notifyTypeValue === '3') { + s = + '企业微信Url:' + json['webhookUrl'] + ';' + + '被@人企业微信用户id:' + json['ats'] + ';' } else if (this.notifyTypeValue === '4') { s = '飞书Url:' + json['webhookUrl'] + ';' + diff --git a/frontend/src/views/task/form/NotifyForm.vue b/frontend/src/views/task/form/NotifyForm.vue index dd406355..551f59be 100644 --- a/frontend/src/views/task/form/NotifyForm.vue +++ b/frontend/src/views/task/form/NotifyForm.vue @@ -165,7 +165,8 @@ {rules: [{ required: true, message: '请输入钉钉URL', whitespace: true}]} ]" /> - + 被@人手机号或钉钉号  + + + + + 被@人企业微信用户id  + + @@ -493,6 +516,10 @@ export default { s = '钉钉Url:' + json['webhookUrl'] + ';' + '被@人手机号或钉钉号:' + json['ats'] + ';' + } else if (this.notifyTypeValue === '3') { + s = + '企业微信Url:' + json['webhookUrl'] + ';' + + '被@人企业微信用户id:' + json['ats'] + ';' } else if (this.notifyTypeValue === '4') { s = '飞书Url:' + json['webhookUrl'] + ';' +