sc 4 days ago
committed by Administrator
parent
commit
07b73a7185
  1. 37
      Class/TcpServer.cs
  2. 10
      Models/AppModels.cs
  3. 10
      View/LogPage.xaml.cs

37
Class/TcpServer.cs

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using TouchSocket.Core; using TouchSocket.Core;
@ -18,12 +20,25 @@ namespace SunlightAggregationTerminal.Class
public required string Dat { get; set; } 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 ServerIP = "";
public static string ServerName = ""; public static string ServerName = "";
public static string ServerPassword = ""; public static string ServerPassword = "";
public static string ServerTerminal = ""; 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) public static void Configuration(string ip, string Name, string Password, string Terminal)
{ {
@ -42,7 +57,7 @@ namespace SunlightAggregationTerminal.Class
{ {
User = ServerName, User = ServerName,
Password = ServerPassword, Password = ServerPassword,
IP = ServerIP, IP = GetLocalIpSimple(),
Terminal = ServerTerminal, Terminal = ServerTerminal,
Dat = Dat Dat = Dat
}; };
@ -53,17 +68,19 @@ namespace SunlightAggregationTerminal.Class
{ {
await tcpClient.SendAsync(JsonSerializer.Serialize(dat)); 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.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.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。 tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
#region Tcp客户端使用Received异步委托接收数据 #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("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("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("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("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)) if (data.TryGetValue("UserData", out var value_UserData))
{ {
@ -144,7 +165,7 @@ namespace SunlightAggregationTerminal.Class
.SetRemoteIPHost(ip)); .SetRemoteIPHost(ip));
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
} }
catch (Exception) catch (Exception ex)
{ {
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "不合法的访问地址", "是"); await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "不合法的访问地址", "是");
} }

10
Models/AppModels.cs

@ -17,15 +17,17 @@ namespace SunlightAggregationTerminal.Models
if(sqliteHelper != null) if(sqliteHelper != null)
{//获取配置信息 {//获取配置信息
sqliteHelper.Open(); sqliteHelper.Open();
var userdata= sqliteHelper.ExecuteDataSet("select * from Users Where LogNo = 1", null); DataSet? userdata = sqliteHelper.ExecuteDataSet("select * from Users ", null);
sqliteHelper.Close(); sqliteHelper.Close();
if (userdata != null) if (userdata != null)
{ {
var dat = userdata.Tables[0].AsEnumerable().FirstOrDefault(); // DataRow dat = userdata.Tables[0].Rows[0];
if (dat != null) // if (dat != null)
// {
foreach (DataRow dat in userdata.Tables[0].Rows)
{ {
App.GlobalData.UserName = dat.Field<string>("UserName") ?? ""; App.GlobalData.UserName = dat.Field<string>("UserName") ?? "";
App.GlobalData.UserId = dat.Field<string>("UserId") ?? ""; App.GlobalData.UserId = dat.Field<string>("UserID") ?? "";
App.GlobalData.User = dat.Field<string>("User") ?? ""; App.GlobalData.User = dat.Field<string>("User") ?? "";
App.GlobalData.UserPassword = dat.Field<string>("UserPassword") ?? ""; App.GlobalData.UserPassword = dat.Field<string>("UserPassword") ?? "";
App.GlobalData.Enterprise = dat.Field<string>("Enterprise") ?? ""; App.GlobalData.Enterprise = dat.Field<string>("Enterprise") ?? "";

10
View/LogPage.xaml.cs

@ -74,10 +74,12 @@ public partial class LogPage : ContentPage
TcpServer.Configuration(SERVERIP.Text, UsernameEntry.Text, PasswordEntry.Text, TcpServer.Configuration(SERVERIP.Text, UsernameEntry.Text, PasswordEntry.Text,
Models.AppModels.GetAppUniqueId()); Models.AppModels.GetAppUniqueId());
TcpServer.TcpTransmit("Log");
LoadingIndicator.IsVisible = true; LoadingIndicator.IsVisible = true;
//TcpServer.TcpTransmit("Log");
if (await AppModels.LogIn()) if (await AppModels.LogIn())
{ {
var temp_name = AppModels.Select("SELECT * FROM Users WHERE User='" + UsernameEntry.Text + "' LIMIT 1;")!.Tables[0]; 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 string sql = @"UPDATE Users SET ServerID = '" + SERVERIP.Text
+ "', UserPassword = '" + PasswordEntry.Text + + "', UserPassword = '" + PasswordEntry.Text +
"', LogNo = true WHERE User='" + UsernameEntry.Text + "';"; "', LogNo = 1 WHERE User='" + UsernameEntry.Text + "';";
AppModels.Updata(sql); AppModels.Updata(sql);
} }
else else
@ -98,7 +100,7 @@ public partial class LogPage : ContentPage
MessageNotificationMode ,GUID MessageNotificationMode ,GUID
)VALUES('" + UsernameEntry.Text + "','SUNLIGHT','SUNLIGHT','" + PasswordEntry.Text + )VALUES('" + UsernameEntry.Text + "','SUNLIGHT','SUNLIGHT','" + PasswordEntry.Text +
"','SUNLIGHT','ENGINEER','" + SERVERIP.Text + "','ENGINEER'," + "','SUNLIGHT','ENGINEER','" + SERVERIP.Text + "','ENGINEER'," +
" '',0,0,0,0,'" + Models.AppModels.GetAppUniqueId() + "');"; " '',1,0,0,0,'" + Models.AppModels.GetAppUniqueId() + "');";
AppModels.INSERT(sql); AppModels.INSERT(sql);
} }
@ -122,7 +124,7 @@ public partial class LogPage : ContentPage
await Navigation.PopModalAsync(); //await Navigation.PopModalAsync();
} }
} }
Loading…
Cancel
Save