diff --git a/Class/TcpServer.cs b/Class/TcpServer.cs index 182c58f..9811f34 100644 --- a/Class/TcpServer.cs +++ b/Class/TcpServer.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; using System.Text; using System.Text.Json; using TouchSocket.Core; @@ -18,12 +20,25 @@ namespace SunlightAggregationTerminal.Class public required string Dat { get; set; } } + public static string GetLocalIpSimple() + { + var host = Dns.GetHostEntry(Dns.GetHostName()); + foreach (var ip in host.AddressList) + { + if (ip.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(ip)) + { + return ip.ToString(); + } + } + return "No network connection"; + } + public static string ServerIP = ""; public static string ServerName = ""; public static string ServerPassword = ""; public static string ServerTerminal = ""; - public static TcpClient tcpClient = new TcpClient(); + public static TouchSocket.Sockets.TcpClient tcpClient = new TouchSocket.Sockets.TcpClient(); public static void Configuration(string ip, string Name, string Password, string Terminal) { @@ -42,7 +57,7 @@ namespace SunlightAggregationTerminal.Class { User = ServerName, Password = ServerPassword, - IP = ServerIP, + IP = GetLocalIpSimple(), Terminal = ServerTerminal, Dat = Dat }; @@ -53,17 +68,19 @@ namespace SunlightAggregationTerminal.Class { await tcpClient.SendAsync(JsonSerializer.Serialize(dat)); } - catch (Exception) + catch (Exception ex) { - await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "请求异常", "是"); + await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", ex.ToString(), "是"); } } - public static async Task TcpClient(TcpClient tcpClient, string ip) + public static async Task TcpClient(TouchSocket.Sockets.TcpClient tcpClient, string ip) { tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp - tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器 + tcpClient.Connected = (client, e) => { + TcpServer.TcpTransmit("Log"); + return EasyTask.CompletedTask; };//成功连接到服务器 tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。 tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。 #region Tcp客户端使用Received异步委托接收数据 @@ -82,8 +99,12 @@ namespace SunlightAggregationTerminal.Class if (data.TryGetValue("Enterprise", out var val) && val != null) App.GlobalData.Enterprise = val.ToString() ?? ""; if (data.TryGetValue("Department", out val) && val != null) App.GlobalData.Department = val.ToString() ?? ""; if (data.TryGetValue("Groups", out val) && val != null) App.GlobalData.Groups = val.ToString() ?? ""; - if (data.TryGetValue("UserId", out val) && val != null) App.GlobalData.UserId = val.ToString() ?? ""; if (data.TryGetValue("UserName", out val) && val != null) App.GlobalData.UserName = val.ToString() ?? ""; + if (data.TryGetValue("UserId", out val) && val != null) + { + App.GlobalData.UserId = val.ToString() ?? ""; + App.GlobalData.LogNo = true; + } if (data.TryGetValue("UserData", out var value_UserData)) { @@ -144,7 +165,7 @@ namespace SunlightAggregationTerminal.Class .SetRemoteIPHost(ip)); await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 } - catch (Exception) + catch (Exception ex) { await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "不合法的访问地址", "是"); } diff --git a/Models/AppModels.cs b/Models/AppModels.cs index a4c496c..6abe2e7 100644 --- a/Models/AppModels.cs +++ b/Models/AppModels.cs @@ -17,15 +17,17 @@ namespace SunlightAggregationTerminal.Models if(sqliteHelper != null) {//获取配置信息 sqliteHelper.Open(); - var userdata= sqliteHelper.ExecuteDataSet("select * from Users Where LogNo = 1", null); + DataSet? userdata = sqliteHelper.ExecuteDataSet("select * from Users ", null); sqliteHelper.Close(); if (userdata != null) { - var dat = userdata.Tables[0].AsEnumerable().FirstOrDefault(); - if (dat != null) + // DataRow dat = userdata.Tables[0].Rows[0]; + // if (dat != null) + // { + foreach (DataRow dat in userdata.Tables[0].Rows) { App.GlobalData.UserName = dat.Field("UserName") ?? ""; - App.GlobalData.UserId = dat.Field("UserId") ?? ""; + App.GlobalData.UserId = dat.Field("UserID") ?? ""; App.GlobalData.User = dat.Field("User") ?? ""; App.GlobalData.UserPassword = dat.Field("UserPassword") ?? ""; App.GlobalData.Enterprise = dat.Field("Enterprise") ?? ""; diff --git a/View/LogPage.xaml.cs b/View/LogPage.xaml.cs index d3013a2..61867d6 100644 --- a/View/LogPage.xaml.cs +++ b/View/LogPage.xaml.cs @@ -74,10 +74,12 @@ public partial class LogPage : ContentPage TcpServer.Configuration(SERVERIP.Text, UsernameEntry.Text, PasswordEntry.Text, Models.AppModels.GetAppUniqueId()); - TcpServer.TcpTransmit("Log"); LoadingIndicator.IsVisible = true; + //TcpServer.TcpTransmit("Log"); + + if (await AppModels.LogIn()) { var temp_name = AppModels.Select("SELECT * FROM Users WHERE User='" + UsernameEntry.Text + "' LIMIT 1;")!.Tables[0]; @@ -86,7 +88,7 @@ public partial class LogPage : ContentPage //存在更新账号状态 string sql = @"UPDATE Users SET ServerID = '" + SERVERIP.Text + "', UserPassword = '" + PasswordEntry.Text + - "', LogNo = true WHERE User='" + UsernameEntry.Text + "';"; + "', LogNo = 1 WHERE User='" + UsernameEntry.Text + "';"; AppModels.Updata(sql); } else @@ -98,7 +100,7 @@ public partial class LogPage : ContentPage MessageNotificationMode ,GUID )VALUES('" + UsernameEntry.Text + "','SUNLIGHT','SUNLIGHT','" + PasswordEntry.Text + "','SUNLIGHT','ENGINEER','" + SERVERIP.Text + "','ENGINEER'," + - " '',0,0,0,0,'" + Models.AppModels.GetAppUniqueId() + "');"; + " '',1,0,0,0,'" + Models.AppModels.GetAppUniqueId() + "');"; AppModels.INSERT(sql); } @@ -122,7 +124,7 @@ public partial class LogPage : ContentPage - await Navigation.PopModalAsync(); + //await Navigation.PopModalAsync(); } } \ No newline at end of file