qfrjava
2025-03-17 8333c077cd429de0d1fa093ff3de004646e56251
JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIServerControl.java
@@ -1,15 +1,14 @@
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 cn.hutool.http.HttpUtil;
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;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.servlet.ModelAndView;
import com.smtaiserver.smtaiserver.core.SMTAIServerApp;
@@ -25,6 +24,7 @@
public class SMTAIServerControl 
{
   private static Logger                _logger = LogManager.getLogger(SMTAIServerControl.class);
   
    public ModelAndView swaggerConfigurationUI(SMTRequest tranReq) throws Exception {
@@ -213,9 +213,12 @@
       jsonWr.addKeyValue("sex", recUser.getString("user_sex"));
       jsonWr.addKeyValue("email", recUser.getString("user_email"));
       jsonWr.addKeyValue("note", recUser.getString("note"));
      if (recUser.getString("weixin_nickname")!=null)
       jsonWr.addKeyValue("weixin_nikename",recUser.getString("weixin_nickname") );
      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);
         return tranReq.returnJson(jsonWr);
    }
    
    public ModelAndView loginSMS(SMTAIServerRequest tranReq) throws Exception 
@@ -277,10 +280,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 +301,35 @@
        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[] {
         boolean is_weixin=false;
         // 微信扫码登录
         if (weixinCode != null) {
            Map<String, String> accessTokenMap = WeChatUtils.getAccessToken(weixinCode);
            String openid = accessTokenMap.get("openid");
            String accessToken = accessTokenMap.get("access_token");
            String nikename = getUserInfo(accessToken, 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");
            db.executeSQL("UPDATE sys_user_info SET weixin_nickname=? WHERE user_name=?", new Object[] {
                  nikename,
                  username
            });
            is_weixin=true;
         }
         DBRecords recs = db.querySQL("SELECT * FROM sys_user_info WHERE user_name=? AND user_state_code='Y'", new Object[] {
             username   
          });
          if(recs.getRowCount() == 0)
@@ -308,7 +344,7 @@
          
          db.executeSQL("INSERT INTO sys_operate_log(op_id, op_type, op_time, op_user, op_note, client_ip, op_from)VALUES(?,?,?,?,?,?, ?)", new Object[] {
             SMTStatic.newUUID(),
             "密码登录",
               is_weixin?"微信登录":"密码登录",
             new Date(),
             recs.getRecord(0).getString("user_id"),
             "用户通过" + client + "用密码登录",
@@ -316,7 +352,7 @@
             client
          });
          tranReq.setSessionAttribute("login_from", client);
          return loginUser(db, recs.getRecord(0), updateLoginFlag, tranReq);
       }
       finally
@@ -325,8 +361,23 @@
       }
       
    }
    public ModelAndView  getLLMList(SMTAIServerRequest tranReq) throws Exception
  public static String getUserInfo(String accessToken, String openId) throws JSONException {
    // 拼接请求地址
    String requestUrl =
        "https://api.weixin.qq.com/sns/userinfo?access_token="
            + accessToken
            + "&openid="
            + openId
            + "&lang=zh_CN";
    String result = HttpUtil.get(requestUrl);
    Json.Reader reader = new Json.Reader();
    Json read = (Json) reader.read(result, null);
    //    JSONObject jsonObject = new JSONObject(result);
    return read.safeGetStr("nickname", null);
  }
    public ModelAndView  getLLMList(SMTAIServerRequest tranReq) throws Exception
    {
       SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
       jsonWr.beginArray("llm_list");
@@ -959,4 +1010,37 @@
       }
    }
   /**
    * 绑定微信openid和nickname
    * @param tranReq
    * @return
    * @throws Exception
    */
  public ModelAndView userBindingWeChatAccount(SMTAIServerRequest tranReq) throws Exception {
    String weixinCode = tranReq.convParamToString("weixin_code", true);
    String userName;
     String nikcname;
    try (SMTDatabase db = SMTAIServerApp.getApp().allocDatabase()) {
      String loginUserId = tranReq.getLoginUserId();
      DBRecords dbRecords =
          db.querySQL("select * from sys_user_info where user_id =?", new Object[] {loginUserId});
      if (dbRecords.getRowCount() > 0) {
        userName = (String) dbRecords.getRecords().get(0).getValue("user_name");
      } else {
        return tranReq.returnJsonState(false, "绑定失败, 用户信息异常", null);
      }
      Map<String, String> accessTokenMap = WeChatUtils.getAccessToken(weixinCode);
      String openid = accessTokenMap.get("openid");
      String accessToken = accessTokenMap.get("access_token");
       nikcname = getUserInfo(accessToken, openid);
      if (openid == null || openid.isEmpty()) {
        return tranReq.returnJsonState(false, "绑定失败, 无效的微信二维码", null);
      }
          db.executeSQL(
              "UPDATE sys_user_info set weixin_openid =?,weixin_nickname=?  where user_name=?",
              new Object[] {openid, nikcname, userName});
    }
    return tranReq.returnJsonState(true, "绑定成功", nikcname);
  }
}