qfrjava
2025-03-14 1266e430d1ef58c167ccc58a4197d703d6bdd251
JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIServerControl.java
@@ -1,12 +1,9 @@
package com.smtaiserver.smtaiserver.control;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.*;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import com.smtaiserver.smtaiserver.weixinLogin.WeChatUtils;
import org.apache.commons.text.similarity.JaccardSimilarity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -25,6 +22,7 @@
public class SMTAIServerControl 
{
   private static Logger                _logger = LogManager.getLogger(SMTAIServerControl.class);
   
    public ModelAndView swaggerConfigurationUI(SMTRequest tranReq) throws Exception {
@@ -213,6 +211,8 @@
       jsonWr.addKeyValue("sex", recUser.getString("user_sex"));
       jsonWr.addKeyValue("email", recUser.getString("user_email"));
       jsonWr.addKeyValue("note", recUser.getString("note"));
      boolean b1 = recUser.getString("weixin_openid") != null;
      jsonWr.addKeyValue("weixin_openid",b1);
       jsonWr.addKeyValue("web_login", "Y".equals(recUser.getString("web_login")));
       
         return tranReq.returnJson(jsonWr);      
@@ -277,10 +277,15 @@
    
    public ModelAndView login(SMTAIServerRequest tranReq) throws Exception 
    {
       String username = tranReq.convParamToString("user", true);
       String password = tranReq.convParamToString("pass", true);
       String username = tranReq.convParamToString("user", false);
       String password = tranReq.convParamToString("pass", false);
       String client = tranReq.convParamToString("client", false);
       String weixinCode = tranReq.convParamToString("weixin_code", false);
         if (weixinCode != null){
            username="";
            password="";
         }
       if(SMTStatic.isNullOrEmpty(client))
          client = "Web端";
       
@@ -293,7 +298,29 @@
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
       try
       {
          DBRecords recs = db.querySQL("SELECT * FROM sys_user_info WHERE user_name=? AND user_state_code='Y'", new Object[] {
         // 微信扫码登录
         if (weixinCode != null) {
            Map<String, String> accessTokenMap = WeChatUtils.getAccessToken(weixinCode);
            String openid = accessTokenMap.get("openid");
            if (openid == null || openid.isEmpty()) {
               return tranReq.returnJsonState(false, "登录失败, 无效的微信二维码", null);
            }
            DBRecords userByOpenid = db.querySQL("SELECT * FROM sys_user_info WHERE weixin_openid = ?", new Object[]{openid});
            if (userByOpenid.getRowCount() == 0) {
               return tranReq.returnJsonState(false, "登录失败, 请绑定账号", null);
            }
            // 获取用户名和密码
            username = userByOpenid.getRecord(0).getString("user_name");
            password = userByOpenid.getRecord(0).getString("user_pass");
         }
         DBRecords recs = db.querySQL("SELECT * FROM sys_user_info WHERE user_name=? AND user_state_code='Y'", new Object[] {
             username   
          });
          if(recs.getRowCount() == 0)
@@ -959,4 +986,20 @@
       }
    }
   public ModelAndView userBindingWeChatAccount(SMTAIServerRequest tranReq) throws Exception {
      String weixinCode = tranReq.convParamToString("weixin_code", true);
      String userName = tranReq.convParamToString("user_name", true);
      Map<String, String> accessTokenMap = WeChatUtils.getAccessToken(weixinCode);
      String openid = accessTokenMap.get("openid");
      if (openid == null || openid.isEmpty()) {
         return tranReq.returnJsonState(false, "绑定失败, 无效的微信二维码", null);
      }
      try (SMTDatabase db = SMTAIServerApp.getApp().allocDatabase()) {
         int executeSQL = db.executeSQL("UPDATE sys_user_info set weixin_openid =? where user_name=?", new Object[]{openid, userName});
      }
      return tranReq.returnJsonState(true, "绑定成功", null);
   }
}