From e0b7163c454c33597afbba7b48e33e422badc485 Mon Sep 17 00:00:00 2001
From: qfrjava <13402782+qfrjava@user.noreply.gitee.com>
Date: 星期五, 07 三月 2025 19:53:07 +0800
Subject: [PATCH] 修复bug

---
 JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIWeixinControl.java |   89 +++++++++++++++++++++++---------------------
 1 files changed, 47 insertions(+), 42 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 e5204a0..01378d8 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
@@ -62,6 +62,7 @@
     asynchronousList.put(fromUserName, randomUuid.toString());
     String result = getString(fromUserName, toUserName, createTimeStr);
     if (reqContent.equals("鍋滄杈撳嚭")) {
+      asynchronousList.remove(fromUserName);
       _logger.info("鐢ㄦ埛鍋滄杈撳嚭");
       return null;
     }
@@ -77,6 +78,7 @@
                 answer, requestMap.get(FROM_USER_NAME), randomUuid.toString());
           } catch (Exception e) {
             _logger.error("aiReplyToTheUserASecondTime error", e);
+            asynchronousList.remove(request.getParameter("FromUserName"));
           }
         });
     _logger.info("寰俊娑堟伅杩斿弬锛�" + result);
@@ -119,8 +121,7 @@
   public void aiReplyToTheUserASecondTime(String answer, String fromUserName, String abortID)
       throws Exception {
     String accessToken = getAccessToken();
-    if (answer.isEmpty())
-      answer = "鎶辨瓑锛屾垜鏆傛椂鏃犳硶鐞嗚В鎮ㄧ殑闂銆�";
+    if (answer.isEmpty()) answer = "鎶辨瓑锛屾垜鏆傛椂鏃犳硶鐞嗚В鎮ㄧ殑闂銆�";
     SMTJsonWriter jsonWr = new SMTJsonWriter(false);
     jsonWr.addKeyValue("touser", fromUserName);
     jsonWr.addKeyValue("msgtype", "text");
@@ -145,49 +146,51 @@
   /** 鏁版嵁搴撹幏鍙� access_koen */
   public String getAccessToken() throws Exception {
     synchronized (this._lockToken) {
-      try (SMTDatabase db = SMTAIServerApp.getApp().allocDatabase()) {
-        HashMap<String, String> weixinParam = SMTWXSStatic.getWeixinParam();
-        Date curTime = new Date();
-        String appId = weixinParam.get("appId");
+      SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
+      //      try (SMTDatabase db = SMTAIServerApp.getApp().allocDatabase()) {
+      HashMap<String, String> weixinParam = SMTWXSStatic.getWeixinParam();
+      Date curTime = new Date();
+      String appId = weixinParam.get("appId");
 
-        // 鏌ヨ鏈繃鏈熺殑 access_token
-        DBRecords dbRecord =
-            db.querySQL(
-                "SELECT app_id, access_token, expires_time "
-                    + "FROM ai_weixin_token "
-                    + "WHERE app_id = ? "
-                    + "  AND ? < expires_time",
-                new Object[] {appId, curTime});
+      // 鏌ヨ鏈繃鏈熺殑 access_token
+      DBRecords dbRecord =
+          db.querySQL(
+              "SELECT app_id, access_token, expires_time "
+                  + "FROM ai_weixin_token "
+                  + "WHERE app_id = ? "
+                  + "  AND ? < expires_time",
+              new Object[] {appId, curTime});
 
-        // 鏁版嵁搴撴棤璁板綍锛屼粠寰俊鏈嶅姟鍣ㄨ幏鍙� access_token
-        List<SMTDatabase.DBRecord> records = dbRecord.getRecords();
+      // 鏁版嵁搴撴棤璁板綍锛屼粠寰俊鏈嶅姟鍣ㄨ幏鍙� access_token
+      List<SMTDatabase.DBRecord> records = dbRecord.getRecords();
 
-        if (dbRecord.getRowCount() > 0) {
-          return records.get(0).getString("access_token");
-        }
-        // 寰俊鍙栵紝杩斿洖token骞朵笖淇濆瓨鎴栬鐩栨暟鎹�
-        else {
-
-          Object[] accessToken = fetchAccessTokenFromWeixinServer(); // 浠庡井淇℃湇鍔″櫒鑾峰彇 access_token
-
-          Date expiresTime =
-              SMTStatic.calculateTime(
-                  curTime, SMTStatic.SMTCalcTime.ADD_SECOND, ((int) accessToken[1]) / 2);
-
-          String sql =
-              "INSERT INTO ai_weixin_token (app_id, access_token, expires_time) "
-                  + "VALUES (?, ?, ?) "
-                  + "ON CONFLICT (app_id) "
-                  + // 濡傛灉 app_id 鍐茬獊
-                  "DO UPDATE SET "
-                  + "    access_token = EXCLUDED.access_token, "
-                  + "    expires_time = EXCLUDED.expires_time;";
-          db.executeSQL(sql, new Object[] {appId, accessToken[0], expiresTime});
-          return (String) accessToken[0];
-        }
-      } catch (Exception e) {
-        throw new Exception("Failed to get access token", e);
+      if (dbRecord.getRowCount() > 0) {
+        return records.get(0).getString("access_token");
       }
+      // 寰俊鍙栵紝杩斿洖token骞朵笖淇濆瓨鎴栬鐩栨暟鎹�
+      else {
+
+        Object[] accessToken = fetchAccessTokenFromWeixinServer(); // 浠庡井淇℃湇鍔″櫒鑾峰彇 access_token
+
+        Date expiresTime =
+            SMTStatic.calculateTime(
+                curTime, SMTStatic.SMTCalcTime.ADD_SECOND, ((int) accessToken[1]) / 2);
+
+        String sql =
+            "INSERT INTO ai_weixin_token (app_id, access_token, expires_time) "
+                + "VALUES (?, ?, ?) "
+                + "ON CONFLICT (app_id) "
+                + // 濡傛灉 app_id 鍐茬獊
+                "DO UPDATE SET "
+                + "    access_token = EXCLUDED.access_token, "
+                + "    expires_time = EXCLUDED.expires_time;";
+        db.executeSQL(sql, new Object[] {appId, accessToken[0], expiresTime});
+        return (String) accessToken[0];
+      }
+      //      } catch (Exception e) {
+      //        throw new Exception("Failed to get access token", e);
+      //
+      //      }
     }
   }
 
@@ -216,7 +219,9 @@
     if (accessToken != null) {
       return new Object[] {accessToken, SMTStatic.toInt(expiresIn)};
     } else {
-      throw new Exception("can't get weixin token : " + json);
+      _logger.info("can't get weixin token : " + json);
+      return null;
+      //      throw new Exception("can't get weixin token : " + json);
     }
   }
 

--
Gitblit v1.9.3