BAOLH
2026-11-10 4d730af0447c686133130d0116a0fa3dd82c2dca
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/temp/service/impl/WechatSendHistoryServiceImpl.java
@@ -17,6 +17,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import me.zhengjie.modules.temp.domain.WechatSendHistory;
import me.zhengjie.modules.temp.event.ChangeApprovalStatusEvent;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.temp.repository.WechatSendHistoryRepository;
@@ -25,8 +26,11 @@
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.JSONArray;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
@@ -53,6 +57,7 @@
    private final WechatSendHistoryRepository wechatSendHistoryRepository;
    private final WechatSendHistoryMapper wechatSendHistoryMapper;
    private final ApplicationEventPublisher applicationEventPublisher;
    private final String CORP_ID = "wwc0fd49735bfcf0ad";
    private final String TOKEN = "KXUDtcVs1UdN7T";
@@ -144,7 +149,7 @@
            }
            String callbackData = sb.toString();
            // 保存
            // 保存原始数据
            WechatSendHistory beforeHistory = new WechatSendHistory();
            beforeHistory.setContent(callbackData);
            wechatSendHistoryRepository.save(beforeHistory);
@@ -155,7 +160,9 @@
                dataJson = new JSONObject(callbackData);
            } else {
                JSONObject xmlJson = XML.toJSONObject(callbackData);
                // 保存
                // 修复 JSON 结构
                fixJsonStructure(xmlJson);
                // 保存解密前XML数据
                WechatSendHistory afterHistory1 = new WechatSendHistory();
                afterHistory1.setContent(xmlJson.toString());
                wechatSendHistoryRepository.save(afterHistory1);
@@ -165,7 +172,7 @@
            // 进行解密处理前将键值修改为小写
            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);
@@ -176,23 +183,48 @@
                decryptDataJson = new JSONObject(decryptedMessage);
            } else {
                JSONObject xmlJson = XML.toJSONObject(decryptedMessage);
                // 保存
                // 修复 JSON 结构
                fixJsonStructure(xmlJson);
                // 保存解密后XML数据
                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);
            WechatSendHistory afterHistorySaved = wechatSendHistoryRepository.save(afterHistory4);
            ChangeApprovalStatusEvent event = new ChangeApprovalStatusEvent(new Object(), afterHistorySaved.getId());
            applicationEventPublisher.publishEvent(event);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private Boolean isJson(String str) {
    @Override
    public void handleWechatSendHistoryBySpNo(String spNo) {
        if (StringUtils.isNotBlank(spNo)) {
            WechatSendHistoryQueryCriteria criteria = new WechatSendHistoryQueryCriteria();
            criteria.setContentInnerLike(spNo);
            List<WechatSendHistory> historyList = wechatSendHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), Sort.by(Sort.Direction.DESC, "id"));
            if (historyList.size() > 0) {
                WechatSendHistory filterData = historyList.stream().filter(h -> h.getContent().startsWith("{\"ApprovalInfo\"")).findFirst().orElseGet(WechatSendHistory::new);
                if (StringUtils.isNotBlank(filterData.getContent())) {
                    ChangeApprovalStatusEvent event = new ChangeApprovalStatusEvent(new Object(), filterData.getId());
                    applicationEventPublisher.publishEvent(event);
                }
            }
        }
    }
    /**
     * 判断字符串是不是Json格式
     *
     * @param str 字符串对象
     */
    private static Boolean isJson(String str) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.readTree(str);
@@ -201,4 +233,42 @@
            return false;
        }
    }
    /**
     * 修复 JSON 结构,确保指定字段始终是列表
     *
     * @param jsonObject JSON 对象
     */
    private static void fixJsonStructure(JSONObject jsonObject) {
        // 定义需要确保为列表的字段名
        String[] listFields = {"ProcessList", "NodeList", "Details", "Notifyer", "SubNodeList", "SpRecord", "Comments" };
        for (String fieldName : listFields) {
            if (jsonObject.has(fieldName)) {
                Object value = jsonObject.get(fieldName);
                if (!(value instanceof JSONArray)) {
                    // 如果不是列表,将其包装为列表
                    JSONArray array = new JSONArray();
                    array.put(value);
                    jsonObject.put(fieldName, array);
                }
            }
        }
        // 递归处理嵌套对象
        for (String key : jsonObject.keySet()) {
            Object value = jsonObject.get(key);
            if (value instanceof JSONObject) {
                fixJsonStructure((JSONObject) value);
            } else if (value instanceof JSONArray) {
                JSONArray array = (JSONArray) value;
                for (int i = 0; i < array.length(); i++) {
                    Object item = array.get(i);
                    if (item instanceof JSONObject) {
                        fixJsonStructure((JSONObject) item);
                    }
                }
            }
        }
    }
}