qfrjava
2025-03-07 e0b7163c454c33597afbba7b48e33e422badc485
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);
    }
  }