From 3e9448b47ba7905f237f569696d60db6e77deb64 Mon Sep 17 00:00:00 2001 From: TangCheng <tangchengmail@163.com> Date: 星期三, 05 三月 2025 17:10:56 +0800 Subject: [PATCH] 去掉不兼容的代码 --- JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIWeixinControl.java | 69 ++++++++++++++++++++-------------- 1 files changed, 40 insertions(+), 29 deletions(-) diff --git a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIWeixinControl.java b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIWeixinControl.java index fdf56d3..e30e375 100644 --- a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIWeixinControl.java +++ b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIWeixinControl.java @@ -11,6 +11,7 @@ import com.smtaiserver.smtaiserver.vo.WechatMessageVO; import com.smtservlet.core.SMTRequest; import com.smtservlet.util.Json; +import com.smtservlet.util.SMTJsonWriter; import com.smtservlet.util.SMTStatic; import org.apache.logging.log4j.LogManager; @@ -229,18 +230,27 @@ public ModelAndView aiReplyToTheUserASecondTime(String answer, String fromUserName) throws Exception { String accessToken = getAccessToken(); - JSONObject jsonObject = new JSONObject(); - jsonObject.put("touser", fromUserName); - jsonObject.put("msgtype", "text"); - JSONObject jsonObject1 = new JSONObject(); - jsonObject1.put("content", answer); - jsonObject.put("text", jsonObject1); - _logger.info("jsonObject: {}", jsonObject); + + SMTJsonWriter jsonWr = new SMTJsonWriter(false); + jsonWr.addKeyValue("touser", fromUserName); + jsonWr.addKeyValue("msgtype", "text"); + jsonWr.beginMap("text"); + { + jsonWr.addKeyValue("content", answer); + } + jsonWr.endMap(); +// JSONObject jsonObject = new JSONObject(); +// jsonObject.put("touser", fromUserName); +// jsonObject.put("msgtype", "text"); +// JSONObject jsonObject1 = new JSONObject(); +// jsonObject1.put("content", answer); +// jsonObject.put("text", jsonObject1); +// _logger.info("jsonObject: {}", jsonObject); String url = String.format( "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s", accessToken); - Map<String, Object> stringObjectMap = jsonObjectToMap(jsonObject); - String s = sendPost(url, stringObjectMap); + //Map<String, Object> stringObjectMap = jsonObjectToMap(jsonWr.getRootJson()); + String s = sendPost(url, jsonWr.getRootJson()); _logger.info("涓婁紶缁撴灉: {}", s); return null; } @@ -294,24 +304,25 @@ } } - public static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) throws JSONException { - Map<String, Object> map = new HashMap<>(); - Iterator<String> keys = jsonObject.keys(); +// public static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) throws JSONException { +// Map<String, Object> map = new HashMap<>(); +// Iterator<String> keys = jsonObject.keys(); +// +// while (keys.hasNext()) { +// String key = keys.next(); +// Object value = jsonObject.get(key); +// +// // 濡傛灉鍊兼槸 JSONObject锛岄�掑綊杞崲涓� Map +// if (value instanceof JSONObject) { +// value = jsonObjectToMap((JSONObject) value); +// } +// +// map.put(key, value); +// } +// +// return map; +// } - while (keys.hasNext()) { - String key = keys.next(); - Object value = jsonObject.get(key); - - // 濡傛灉鍊兼槸 JSONObject锛岄�掑綊杞崲涓� Map - if (value instanceof JSONObject) { - value = jsonObjectToMap((JSONObject) value); - } - - map.put(key, value); - } - - return map; - } /** * 楠岃瘉绛惧悕util @@ -408,10 +419,10 @@ return s; } - public static String sendPost(String urlString, Map<String, Object> params) throws Exception { + public static String sendPost(String urlString, Json jsonParam) throws Exception { // 灏嗗弬鏁拌浆鎹负 JSON 鏍煎紡瀛楃涓� - JSONObject jsonParams = new JSONObject(params); - String payload = jsonParams.toString(); + // JSONObject jsonParams = new JSONObject(params); + String payload = jsonParam.toString(); // 鍒涘缓杩炴帴 URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); -- Gitblit v1.9.3