BAOLH
2025-03-14 4fa526092b61ba33a7fc7662048288691d7bb9b0
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/temp/service/impl/WechatSendHistoryServiceImpl.java
@@ -15,10 +15,9 @@
*/
package me.zhengjie.modules.temp.service.impl;
import ch.ethz.ssh2.crypto.digest.SHA1;
import com.fasterxml.jackson.databind.ObjectMapper;
import me.zhengjie.modules.temp.domain.WechatSendHistory;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.temp.repository.WechatSendHistoryRepository;
import me.zhengjie.modules.temp.service.WechatSendHistoryService;
@@ -26,17 +25,19 @@
import me.zhengjie.modules.temp.service.dto.WechatSendHistoryQueryCriteria;
import me.zhengjie.modules.temp.service.mapstruct.WechatSendHistoryMapper;
import me.zhengjie.utils.weixin.WXBizJsonMsgCrypt;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -56,6 +57,7 @@
    private final String CORP_ID = "wwc0fd49735bfcf0ad";
    private final String TOKEN = "KXUDtcVs1UdN7T";
    private final String ENCODING_AES_KEY = "FnqE1jnu1LkqkfZzzsgU6WgIRvj1FTo8a7LgOmHqKgM";
    private final String APPROVAL_AGENT_ID = "1000005";
    @Override
    public Map<String,Object> queryAll(WechatSendHistoryQueryCriteria criteria, Pageable pageable){
@@ -111,14 +113,22 @@
    }
    @Override
    public String approvalBack(String msgSignature, String timestamp, String nonce, String echostr, HttpServletResponse response) {
    public void approvalBack(String msgSignature, String timestamp, String nonce, String echostr, HttpServletResponse response) {
        try {
            WXBizJsonMsgCrypt wxcpt = new WXBizJsonMsgCrypt(TOKEN, ENCODING_AES_KEY, CORP_ID);
            return wxcpt.VerifyURL(msgSignature, timestamp, nonce, echostr);
            // 设置响应内容类型为纯文本
            response.setContentType("text/plain; charset=UTF-8");
            // 获取PrintWriter对象
            PrintWriter out = response.getWriter();
            // 直接返回
            String message = wxcpt.VerifyURL(msgSignature, timestamp, nonce, echostr);
            out.print(message);
            // 刷新并关闭PrintWriter
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
@@ -132,13 +142,63 @@
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            String callbackXmlData = sb.toString();
            String decryptedMessage = wxcpt.DecryptMsg(msgSignature, timestamp, nonce, callbackXmlData);
            WechatSendHistory wechatSendHistory = new WechatSendHistory();
            wechatSendHistory.setContent(decryptedMessage);
            wechatSendHistoryRepository.save(wechatSendHistory);
            String callbackData = sb.toString();
            // 保存
            WechatSendHistory beforeHistory = new WechatSendHistory();
            beforeHistory.setContent(callbackData);
            wechatSendHistoryRepository.save(beforeHistory);
            // 解密前处理
            JSONObject dataJson;
            if (isJson(callbackData)) {
                dataJson = new JSONObject(callbackData);
            } else {
                JSONObject xmlJson = XML.toJSONObject(callbackData);
                // 保存
                WechatSendHistory afterHistory1 = new WechatSendHistory();
                afterHistory1.setContent(xmlJson.toString());
                wechatSendHistoryRepository.save(afterHistory1);
                dataJson = xmlJson.getJSONObject("xml");
            }
            // 进行解密处理前将键值修改为小写
            JSONObject finalDataJson = new JSONObject(dataJson.keySet().stream().collect(Collectors.toMap(key -> key.toLowerCase(), val -> dataJson.get(val))));
            String decryptedMessage = wxcpt.DecryptMsg(msgSignature, timestamp, nonce, finalDataJson.toString());
            // 保存
            WechatSendHistory afterHistory2 = new WechatSendHistory();
            afterHistory2.setContent(decryptedMessage);
            wechatSendHistoryRepository.save(afterHistory2);
            // 解密后处理
            JSONObject decryptDataJson;
            if (isJson(decryptedMessage)) {
                decryptDataJson = new JSONObject(decryptedMessage);
            } else {
                JSONObject xmlJson = XML.toJSONObject(decryptedMessage);
                // 保存
                WechatSendHistory afterHistory3 = new WechatSendHistory();
                afterHistory3.setContent(xmlJson.toString());
                wechatSendHistoryRepository.save(afterHistory3);
                decryptDataJson = xmlJson.getJSONObject("xml");
            }
            // 保存
            WechatSendHistory afterHistory4 = new WechatSendHistory();
            afterHistory4.setContent(decryptDataJson.toString());
            wechatSendHistoryRepository.save(afterHistory4);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private Boolean isJson(String str) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.readTree(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}