From 8333c077cd429de0d1fa093ff3de004646e56251 Mon Sep 17 00:00:00 2001
From: qfrjava <13402782+qfrjava@user.noreply.gitee.com>
Date: 星期一, 17 三月 2025 13:38:13 +0800
Subject: [PATCH] fix(smtaiserver): 修正微信昵称字段名

---
 JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIServerControl.java |  114 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 99 insertions(+), 15 deletions(-)

diff --git a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIServerControl.java b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIServerControl.java
index 37703c4..cd146ab 100644
--- a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTAIServerControl.java
+++ b/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鍜宯ickname
+	 * @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);
+  }
 }

--
Gitblit v1.9.3