| | |
| | | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | |
| | | private final WechatSendHistoryRepository wechatSendHistoryRepository; |
| | | private final WechatSendHistoryMapper wechatSendHistoryMapper; |
| | | private final ApplicationEventPublisher applicationEventPublisher; |
| | | |
| | | 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){ |
| | |
| | | sb.append(line); |
| | | } |
| | | |
| | | String callbackXmlData = sb.toString(); |
| | | String callbackData = sb.toString(); |
| | | // 保存原始数据 |
| | | WechatSendHistory beforeHistory = new WechatSendHistory(); |
| | | beforeHistory.setContent(callbackXmlData); |
| | | beforeHistory.setContent(callbackData); |
| | | wechatSendHistoryRepository.save(beforeHistory); |
| | | |
| | | // 解密前处理 |
| | | JSONObject dataJson; |
| | | if (isJson(callbackXmlData)) { |
| | | dataJson = new JSONObject(callbackXmlData); |
| | | if (isJson(callbackData)) { |
| | | dataJson = new JSONObject(callbackData); |
| | | } else { |
| | | JSONObject xmlJson = XML.toJSONObject(callbackXmlData); |
| | | |
| | | JSONObject xmlJson = XML.toJSONObject(callbackData); |
| | | // 修复 JSON 结构 |
| | | fixJsonStructure(xmlJson); |
| | | // 保存解密前XML数据 |
| | | 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 xmlJson = XML.toJSONObject(callbackXmlData); |
| | | // if (xmlJson.has("xml")) { |
| | | // WechatSendHistory afterHistory1 = new WechatSendHistory(); |
| | | // afterHistory1.setContent(xmlJson.toString()); |
| | | // wechatSendHistoryRepository.save(afterHistory1); |
| | | // |
| | | // // 处理数据 |
| | | // JSONObject 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); |
| | | // } else { |
| | | // String decryptedMessage = wxcpt.DecryptMsg(msgSignature, timestamp, nonce, xmlJson.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); |
| | | // 修复 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()); |
| | | 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); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |