From 5543b5bcde31d1f4830679118fae4a1c8314a12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E6=AC=A7=EF=BC=88=E6=9E=97=E5=B9=B3=E5=87=A1?= =?UTF-8?q?=EF=BC=89?= Date: Thu, 4 Sep 2025 18:00:43 +0800 Subject: [PATCH] 1 --- .../business/mq/MessageConsumerService.java | 59 +++++++---------- .../java/cn/van/business/util/WXUtil.java | 4 +- .../java/cn/van/business/util/WxtsUtil.java | 65 ++++++++++++++++++- 3 files changed, 92 insertions(+), 36 deletions(-) diff --git a/src/main/java/cn/van/business/mq/MessageConsumerService.java b/src/main/java/cn/van/business/mq/MessageConsumerService.java index 206bff8..0e8b226 100644 --- a/src/main/java/cn/van/business/mq/MessageConsumerService.java +++ b/src/main/java/cn/van/business/mq/MessageConsumerService.java @@ -1,10 +1,7 @@ package cn.van.business.mq; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.http.HttpRequest; import cn.van.business.util.WxtsUtil; import com.alibaba.fastjson2.JSONObject; -import com.google.common.util.concurrent.RateLimiter; import org.apache.rocketmq.spring.annotation.ConsumeMode; import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; import org.apache.rocketmq.spring.core.RocketMQListener; @@ -13,11 +10,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.nio.charset.StandardCharsets; -import java.util.concurrent.TimeUnit; - -import static cn.van.business.util.WXUtil.WX_BASE_URL; - /** * @author Leo * @version 1.0 @@ -46,35 +38,34 @@ public class MessageConsumerService implements RocketMQListener { public void onMessage(JSONObject message) { try { logger.info("消费消息:{}", message); - //logger.info("[RateLimiter] 开始处理消息,当前时间:{}", System.currentTimeMillis()); - //rateLimiter.acquire(); - //logger.info("[RateLimiter] 获得令牌,当前时间:{}", System.currentTimeMillis()); + + // 解析消息类型和数据 + //String type = message.getString("type"); + JSONObject data = message.getJSONObject("data"); + // + //if (data == null) { + // logger.error("消息数据为空:{}", message); + // return; + //} + // + String wxid = data.getString("wxid"); + //if (wxid == null || wxid.isEmpty()) { + // logger.error("消息缺少wxid字段:{}", message); + // return; + //} + + // 根据消息类型调用不同的wxts接口 + + // 发送文本消息 + String content = data.getString("msg"); + Integer msgType = data.getInteger("msgType"); + String fromWxid = data.getString("fromWxid"); + Boolean hiddenTime = data.getBoolean("hiddenTime"); + + wxtsUtil.sendWxTextMessage(wxid, content, msgType, fromWxid, hiddenTime); - //logger.debug("构造完成的消息结构:{}", requestBody.toJSONString()); - // 4. 发送请求(保持原有) - String responseStr; - -// 修改onMessage方法中的HTTP请求部分 - responseStr = HttpRequest.post(WX_BASE_URL).header("Content-Type", "application/json; charset=UTF-8") // 明确指定编码 - .body(message.toJSONString().getBytes(StandardCharsets.UTF_8)) // 显式转为UTF-8字节 - .execute().charset("UTF-8") // 强制响应体使用UTF-8解码 - .body(); - // ... [保持原有响应处理逻辑] - if (ObjectUtil.isNotEmpty(responseStr)) { - JSONObject response = JSONObject.parseObject(responseStr); - logger.info("消息成功发送并得到响应:{}", response); - if (response.getInteger("code") != 200) { - // TODO: 如果需要处理错误,您可以在这里添加逻辑 - wxtsUtil.sendNotify("消息发送失败: " + responseStr); - throw new RuntimeException("消息发送失败: " + responseStr); - } - //logger.info("消息成功发送并得到响应:{}", response); - } else { - wxtsUtil.sendNotify("消息发送失败,没有收到响应"); - throw new RuntimeException("消息发送失败,没有收到响应"); - } } catch (Exception e) { logger.error("消息处理失败,原始消息:{}", message, e); wxtsUtil.sendNotify("系统异常:" + e.getMessage()); diff --git a/src/main/java/cn/van/business/util/WXUtil.java b/src/main/java/cn/van/business/util/WXUtil.java index 1fccffc..0e928f0 100644 --- a/src/main/java/cn/van/business/util/WXUtil.java +++ b/src/main/java/cn/van/business/util/WXUtil.java @@ -22,7 +22,6 @@ import org.springframework.stereotype.Component; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; -import java.util.concurrent.CompletableFuture; /** * @author Leo @@ -262,6 +261,9 @@ public class WXUtil { }*/ data.put("msg", string); data.put("wxid", wxid); + data.put("msgType", msgType); + data.put("fromWxid", fromwxid); + data.put("hiddenTime", hiddenTime); wxReqDate.setData(data); // wxReqDate 转成 JSONObject JSONObject message = JSON.parseObject(JSON.toJSONString(wxReqDate)); diff --git a/src/main/java/cn/van/business/util/WxtsUtil.java b/src/main/java/cn/van/business/util/WxtsUtil.java index 19c2084..3262192 100644 --- a/src/main/java/cn/van/business/util/WxtsUtil.java +++ b/src/main/java/cn/van/business/util/WxtsUtil.java @@ -31,7 +31,7 @@ public class WxtsUtil { String common = "192.168.8.88 (微信机器人), 信息 : "; content = common + content + "

"; paramMap.put("text", content); - HttpResponse execute = HttpRequest.post(url).header("vanToken", TOKEN).header("source", "XZJ_UBUNTU").body(JSON.toJSONString(paramMap)).execute(); + HttpRequest.post(url).header("vanToken", TOKEN).header("source", "XZJ_UBUNTU").body(JSON.toJSONString(paramMap)).execute(); //logger.info("企业微信推送结果:{}", execute); } catch (Exception e) { logger.error("企业微信推送失败:{}", e.getMessage()); @@ -49,4 +49,67 @@ public class WxtsUtil { // - 触发短信通知 sendNotify(formattedMsg); // 复用原有通知方法 } + + /** + * 发送微信文本消息到wxts接口 + * @param wxid 接收者微信ID + * @param content 消息内容 + * @param msgType 消息类型 + * @param fromWxid 发送者微信ID + * @param hiddenTime 是否隐藏时间戳 + */ + public void sendWxTextMessage(String wxid, String content, Integer msgType, String fromWxid, Boolean hiddenTime) { + try { + String url = SERVER_URL + "/wx/send/jd"; + HashMap paramMap = new HashMap<>(); + paramMap.put("text", content); + + + HttpResponse execute = HttpRequest.post(url) + .header("vanToken", TOKEN) + .header("source", "XZJ_UBUNTU") + .body(JSON.toJSONString(paramMap)) + .execute(); + + if (execute.getStatus() == 200) { + logger.info("微信文本消息发送成功:wxid={}, content={}", wxid, content); + } else { + logger.error("微信文本消息发送失败:status={}, response={}", execute.getStatus(), execute.body()); + } + } catch (Exception e) { + logger.error("微信文本消息发送异常:{}", e.getMessage(), e); + } + } + + /** + * 发送微信图片消息到wxts接口 + * @param wxid 接收者微信ID + * @param imagePath 图片路径 + */ + public void sendWxImageMessage(String wxid, String imagePath) { + try { + String url = SERVER_URL + "/send/jd"; + HashMap paramMap = new HashMap<>(); + paramMap.put("wxid", wxid); + paramMap.put("imagePath", imagePath); + + // 提取文件名 + String[] split = imagePath.split("/"); + paramMap.put("fileName", split[split.length - 1]); + + HttpResponse execute = HttpRequest.post(url) + .header("vanToken", TOKEN) + .header("source", "XZJ_UBUNTU") + .body(JSON.toJSONString(paramMap)) + .execute(); + + if (execute.getStatus() == 200) { + logger.info("微信图片消息发送成功:wxid={}, imagePath={}", wxid, imagePath); + } else { + logger.error("微信图片消息发送失败:status={}, response={}", execute.getStatus(), execute.body()); + } + } catch (Exception e) { + logger.error("微信图片消息发送异常:{}", e.getMessage(), e); + } + } }