TangCheng
2025-03-05 3e9448b47ba7905f237f569696d60db6e77deb64
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();