From 27551af823766f1bbf224e7af6279ff4ec3b22b0 Mon Sep 17 00:00:00 2001 From: 18154758993 <605281283@qq.com> Date: Thu, 4 Dec 2025 16:37:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=8F=91=E9=80=81=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/srs/common/utils/WeChatUtil.java | 29 +++++++++++ .../NotificationManagementServiceImpl.java | 48 ++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/srs-common/src/main/java/com/srs/common/utils/WeChatUtil.java b/srs-common/src/main/java/com/srs/common/utils/WeChatUtil.java index b94a681..a6eaa5b 100644 --- a/srs-common/src/main/java/com/srs/common/utils/WeChatUtil.java +++ b/srs-common/src/main/java/com/srs/common/utils/WeChatUtil.java @@ -175,4 +175,33 @@ public class WeChatUtil { throw new RuntimeException("请求发送消息出错", e); } } + + /** + * 发送企业微信 Markdown 消息 + * @param toUser 接收用户,'|' 分隔 + * @param markdown Markdown 内容 + */ + public void sendMarkdownMessage(String toUser, String markdown) { + String accessToken = getAccessToken(); + String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken; + + JSONObject msg = new JSONObject(); + msg.put("touser", toUser); + msg.put("msgtype", "markdown"); + msg.put("agentid", weChatConfig.getAgentId()); + + JSONObject md = new JSONObject(); + md.put("content", markdown); + msg.put("markdown", md); + + try { + String response = restTemplate.postForObject(url, msg, String.class); + JSONObject jsonResponse = JSONObject.parseObject(response); + if (jsonResponse.getIntValue("errcode") != 0) { + throw new RuntimeException("发送Markdown消息失败: " + jsonResponse.getString("errmsg")); + } + } catch (Exception e) { + throw new RuntimeException("请求发送Markdown消息出错", e); + } + } } diff --git a/srs-routine/src/main/java/com/srs/routine/service/impl/NotificationManagementServiceImpl.java b/srs-routine/src/main/java/com/srs/routine/service/impl/NotificationManagementServiceImpl.java index d684ca6..629f11e 100644 --- a/srs-routine/src/main/java/com/srs/routine/service/impl/NotificationManagementServiceImpl.java +++ b/srs-routine/src/main/java/com/srs/routine/service/impl/NotificationManagementServiceImpl.java @@ -243,12 +243,56 @@ public class NotificationManagementServiceImpl extends ServiceImpl batch = studentNos.subList(i, Math.min(i + batchSize, studentNos.size())); // 拼接成"2023001|2023002|2023003"格式 String toUser = String.join("|", batch); - // 调用企业微信发送消息方法 - weChatUtil.sendTextMessage(toUser, content); + // 如果内容包含超链接标签,转换为 Markdown + String md = convertHtmlLinkToMarkdown(content); + if (md != null) { + weChatUtil.sendMarkdownMessage(toUser, md); + } else { + // 如果没有标签但包含 http 链接,直接发送文本也可点击 + weChatUtil.sendTextMessage(toUser, content); + } } } + /** + * 将单个 a 标签的 HTML 转为 Markdown 格式;不匹配返回 null + */ + private String convertHtmlLinkToMarkdown(String html) { + if (html == null) return null; + String lower = html.toLowerCase(); + if (!lower.contains("= 0 && (quoteStartSingle < 0 || quoteStart < quoteStartSingle)) { + qs = quoteStart; + qe = lower.indexOf("\"", qs + 1); + } else if (quoteStartSingle >= 0) { + qs = quoteStartSingle; + qe = lower.indexOf("'", qs + 1); + } + if (qs < 0 || qe < 0) return null; + String url = html.substring(qs + 1, qe).trim(); + int textStart = lower.indexOf(">", qe) + 1; + int textEnd = lower.indexOf("", textStart); + if (textStart <= 0 || textEnd <= textStart) return null; + String text = html.substring(textStart, textEnd).trim(); + // 其余文本去掉标签 + String before = html.substring(0, aStart).replaceAll("<[^>]+>", ""); + String after = html.substring(textEnd + 4).replaceAll("<[^>]+>", ""); + String mdLink = "[" + (text.isEmpty() ? url : text) + "](" + url + ")"; + return before + mdLink + after; + } catch (Exception e) { + return null; + } + } + /** * 根据年级获取学生学号列表 *