BAOLH
2026-11-10 c5dfc09145e4e10f99dbf388a4a1433b5c2973e5
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/temp/service/impl/WechatRobotServiceImpl.java
@@ -1,8 +1,10 @@
package me.zhengjie.modules.temp.service.impl;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.temp.service.WechatRobotService;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.weixin.AesException;
import me.zhengjie.utils.weixin.WXBizJsonMsgCrypt;
import org.springframework.stereotype.Service;
@@ -12,6 +14,8 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
@RequiredArgsConstructor
@@ -21,6 +25,11 @@
    private final String TOKEN = "WTAgLRQif";
    private final String ENCODING_AES_KEY = "w2FEuoDg2D7DxnmJn4DL6YiD47OSHwB34WXVwa2eriY";
    private final String APPROVAL_AGENT_ID = "1000005";
    private final String ROBOT_ID = "aiboW1Wpr5Mg_9WNlzyBtIjtf-ETL6pTEFd";
    private final String TEXT_MSG_TEMPLATE = "{\"msgtype\": \"text\",\"text\": {\"content\": \"%s\"}}";
    private final String STREAM_MSG_TEMPLATE = "{\"msgtype\": \"stream\",\"stream\": {\"id\": \"%s\", \"finish\": \"%s\", \"content\": \"%s\"}}";
    @Override
    public void robotBack(String msgSignature, String timestamp, String nonce, String echostr, HttpServletResponse response) {
@@ -61,11 +70,33 @@
            System.out.println("接收到机器人发送的加密消息:" + jsonObject.toJSONString());
            System.out.println("接收到机器人发送的解密后消息:" + decryptedMessage);
            JSONObject decryptedMessageJson =  JSONObject.parseObject(decryptedMessage);
            System.out.println("接收到机器人发送的消息是否存在msgtype:" + decryptedMessageJson.containsKey("msgtype"));
            if (decryptedMessageJson.getString("msgtype").equals("text")) {
                System.out.println("接收到机器人发送的消息中的msgtype:" + decryptedMessageJson.getString("msgtype"));
//                return wxcpt.EncryptRobotMsg("{\"msgtype\": \"text\",\"text\": {\"content\": \"消息已收到\"}}", "", nonce);
                return wxcpt.EncryptRobotJsonMsg("{\"msgtype\": \"text\",\"text\": {\"content\": \"消息已收到\"}}", "", nonce);
            if (decryptedMessageJson.containsKey("response_url") && StringUtils.isNotBlank(decryptedMessageJson.getString("response_url"))) {
                String responseUrl = decryptedMessageJson.getString("response_url");
                if (decryptedMessageJson.getString("msgtype").equals("text")) {
                    String textContent = decryptedMessageJson.getJSONObject("text").getString("content");
                    if (textContent.contains("流式消息")) {
                        // 流式消息
                        StringBuilder streamContentBuilder = new StringBuilder();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                        String streamId = decryptedMessageJson.getString("msgid") + "_" + sdf.format(new Date());
                        for (int i = 0; i < 10; i++) {
                            streamContentBuilder.append(i);
                            String replyPlainText = String.format(STREAM_MSG_TEMPLATE, streamId, i + 1 >= 10, streamContentBuilder.toString());
                            String responseStr = HttpUtil.post(responseUrl, replyPlainText);
                            System.out.println("接收到回复消息的响应:" + responseStr);
                        }
                    } else {
                        // 普通消息
                        String replyPlainText = String.format(TEXT_MSG_TEMPLATE, "消息已收到");
                        String responseStr = HttpUtil.post(responseUrl, replyPlainText);
                        System.out.println("接收到回复消息的响应:" + responseStr);
                    }
                } else {
                    // 不支持的消息
                    String replyPlainText = String.format(TEXT_MSG_TEMPLATE, "不支持的消息类型:抱歉当前仅支持文字信息");
                    String responseStr = HttpUtil.post(responseUrl, replyPlainText);
                    System.out.println("接收到回复消息的响应:" + responseStr);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();