diff --git a/UserClass/AsyncTcpClient.cs b/UserClass/AsyncTcpClient.cs index 7e7855d..7f08a64 100644 --- a/UserClass/AsyncTcpClient.cs +++ b/UserClass/AsyncTcpClient.cs @@ -1,10 +1,13 @@ using SunlightAggregationManager.ViewModel; using System; using System.Collections.Generic; +using System.Data; using System.Text; using System.Text.Json; using TouchSocket.Core; using TouchSocket.Sockets; +using static SunlightAggregationManager.UserClass.AsyncTcpServer; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace SunlightAggregationManager.UserClass { @@ -72,7 +75,47 @@ namespace SunlightAggregationManager.UserClass //从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。 var mes = e.Memory.Span.ToString(Encoding.UTF8); - //DataReceived.Received(mes); + try + { + Person? deserializedPerson = JsonSerializer.Deserialize(mes); + + var _user = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData, "State", "User='" + + deserializedPerson?.User + "' and Password ='" + deserializedPerson?.Password + "'"); + if (_user != null) + { + string _dat = _user?.ToString() ?? "0"; + if (_dat != "99") + { + //行为 + Dictionary myDict = new Dictionary(); + //myDict.Add("LinkID", client.Id); + myDict.Add("LastLoginIP", deserializedPerson?.IP ?? client.IP); + myDict.Add("LastActionTime", DateTime.Now); + myDict.Add("Note", deserializedPerson?.Dat ?? "unknown"); + MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + deserializedPerson?.User + "'", myDict); + + Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]Note[" + (deserializedPerson?.Dat ?? "unknown") + "]"); + CommandC(deserializedPerson?.Dat ?? "NOT", deserializedPerson!); + + } + else + {//停用账号关闭连接 + client.SendAsync("Account Deactivated"); + + } + } + else + { //无效账号关闭连接 + client.SendAsync("Invalid Account"); + + } + + } + catch (Exception ex) + {//错误 + Console.WriteLine("Tcp:" + ex.ToString()); + client.SendAsync("Target instruction parsing error manager will close connection"); + } return EasyTask.CompletedTask; }; @@ -98,5 +141,72 @@ namespace SunlightAggregationManager.UserClass Console.WriteLine("Tcp:" + ex.ToString()); } } + + public static async void CommandC(string dat, Person person)// + { + string Command = "", ret = ""; + + Dictionary data_ = new Dictionary(); + var deserialized = JsonSerializer.Deserialize>(dat)!; + if (deserialized.TryGetValue("Command", out var val) && val != null) Command = val.ToString()!; + if (Command == "Log")//回复请求的信息 + { + DataRow? _user = MainWindowViewModel.Selet_Row(MainWindowViewModel._userData, "User='" + + person?.User + "' and Password ='" + person?.Password + "'"); + + data_.Add("Enterprise", _user?.Field("Enterprise") ?? ""); + data_.Add("Department", _user?.Field("Department") ?? ""); + data_.Add("Groups", _user?.Field("Groups") ?? ""); + data_.Add("UserId", _user?.Field("UserID") ?? ""); + data_.Add("UserName", _user?.Field("Name") ?? ""); + ret = JsonSerializer.Serialize(data_); + } + if (Command == "Info")//回复info请求的信息 + { + var t = ToList(MainWindowViewModel._machines).ToJsonString(); + //格式化返回信息 + data_.Add("Info", t); + ret = JsonSerializer.Serialize(data_); + } + if (Command == "Query")//回复Query请求的信息 + { + string s_ = deserialized.GetValue("StartTime").ToString()!; + string e_ = deserialized.GetValue("EndTime").ToString()!; + string f_ = deserialized.GetValue("Field").ToString()!; + + DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " + + f_ + " FROM [dbo].[Dyelots] " + "WHERE CreationTime>'" + s_ + "'and CreationTime<'" + e_ + "'"); + //格式化返回信息 + data_.Add("Query", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable)); + ret = JsonSerializer.Serialize(data_); + } + if (Command == "QueryDyelot")//回复QueryDyelot请求的信息 + { + string d_ = deserialized.GetValue("DyelotName").ToString()!; + string f_ = deserialized.GetValue("Field").ToString()!; + + DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " + + f_ + " FROM [dbo].[Dyelots] " + "WHERE Dyelot LIKE '" + d_ + "%'"); + //格式化返回信息 + data_.Add("Query", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable)); + ret = JsonSerializer.Serialize(data_); + } + if (Command == "QueryDetail")//回复info请求的信息 + { + string d_ = deserialized.GetValue("Dyelot").ToString()!; + string r_ = deserialized.GetValue("ReDye").ToString()!; + string f_ = deserialized.GetValue("Field").ToString()!; + + DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " + + f_ + " FROM [dbo].[DyelotsBulkedRecipe] " + "WHERE Dyelot='" + d_ + "'and ReDye='" + r_ + "'"); + //格式化返回信息 + data_.Add("QueryDetail", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable)); + ret = JsonSerializer.Serialize(data_); + } + + person!.Dat = ret.ToString(); + var jsdat = JsonSerializer.Serialize(person); + await tcpClient.SendAsync(jsdat); + } } }