4 changed files with 221 additions and 62 deletions
@ -0,0 +1,205 @@ |
|||
using SunlightAggregationManager.ViewModel; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Data; |
|||
using System.Text; |
|||
using System.Text.Json; |
|||
using TouchSocket.Core; |
|||
using TouchSocket.Http; |
|||
using TouchSocket.Http.WebSockets; |
|||
using TouchSocket.Sockets; |
|||
using static SunlightAggregationManager.UserClass.AsyncTcpServer; |
|||
|
|||
|
|||
namespace SunlightAggregationManager.UserClass |
|||
{ |
|||
public class AsyncHttpServer |
|||
{ |
|||
public static HttpService httpservice = new HttpService(); |
|||
|
|||
public static async Task HttpMain(int port1) |
|||
{ |
|||
NetFwManger.AllowPort(port1, "TCP");//开放端口
|
|||
await httpservice.SetupAsync(new TouchSocketConfig()//加载配置
|
|||
.SetListenIPHosts(port1) |
|||
.ConfigureContainer(a => |
|||
{ |
|||
a.AddConsoleLogger(); |
|||
}) |
|||
.ConfigurePlugins(a => |
|||
{ |
|||
a.Add<MyHttpPlug1>(); |
|||
//default插件应该最后添加,其作用是
|
|||
//1、为找不到的路由返回404
|
|||
//2、处理 header 为Option的探视跨域请求。
|
|||
a.UseDefaultHttpServicePlugin(); |
|||
})); |
|||
await httpservice.StartAsync(); |
|||
Console.WriteLine("HTTP Port:" + port1.ToString()); |
|||
} |
|||
|
|||
public class MyHttpPlug1 : PluginBase, IHttpPlugin |
|||
{ |
|||
public async Task OnHttpRequest(IHttpSessionClient client, HttpContextEventArgs e) |
|||
{ |
|||
#region HttpContext生命周期
|
|||
//var request = e.Context.Request;//http请求体
|
|||
var response = e.Context.Response;//http响应体
|
|||
#endregion
|
|||
|
|||
var bodyString = await e.Context.Request.GetBodyAsync(); |
|||
try |
|||
{ |
|||
Person? deserializedPerson = JsonSerializer.Deserialize<Person>(bodyString); |
|||
|
|||
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<string, object> myDict = new Dictionary<string, object>(); |
|||
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") + "]"); |
|||
|
|||
var dt = await Command(deserializedPerson!); |
|||
|
|||
e.Context.Response.Headers.Add("SUNLIGHT", "SUNLIGHT"); |
|||
e.Context.Response.SetStatus(200, "success"); |
|||
e.Context.Response.SetContent(Encoding.UTF8.GetBytes(dt.ToCharArray())); |
|||
await e.Context.Response.AnswerAsync(); |
|||
} |
|||
else |
|||
{//停用账号关闭连接
|
|||
//client.SendAsync("Account Deactivated");
|
|||
e.Context.Response.Headers.Add("SUNLIGHT", "SUNLIGHT"); |
|||
e.Context.Response.SetStatus(200, "success"); |
|||
e.Context.Response.SetContent("Account Deactivated"); |
|||
await e.Context.Response.AnswerAsync(); |
|||
} |
|||
} |
|||
else |
|||
{ //无效账号关闭连接
|
|||
//client.SendAsync("Invalid Account");
|
|||
e.Context.Response.Headers.Add("SUNLIGHT", "SUNLIGHT"); |
|||
e.Context.Response.SetStatus(200, "success"); |
|||
e.Context.Response.SetContent("Invalid Account"); |
|||
await e.Context.Response.AnswerAsync(); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{//错误
|
|||
Console.WriteLine("Http:" + ex.ToString()); |
|||
//client.SendAsync("Target instruction parsing error manager will close connection");
|
|||
} |
|||
|
|||
|
|||
/* if (request.IsGet() && request.UrlEquals("/success")) |
|||
{ |
|||
//直接响应文字
|
|||
await response |
|||
.SetStatus(200, "success") |
|||
.FromText("Success") |
|||
.AnswerAsync();//直接回应
|
|||
Console.WriteLine("处理/success"); |
|||
return; |
|||
} |
|||
|
|||
|
|||
//无法处理,调用下一个插件
|
|||
await e.InvokeNext();*/ |
|||
} |
|||
} |
|||
|
|||
public static async Task<string> Command(Person deserializedPerson)//
|
|||
{ |
|||
string Command = "", ret = ""; |
|||
|
|||
Dictionary<string, object> data_ = new Dictionary<string, object>(); |
|||
var deserialized = JsonSerializer.Deserialize<Dictionary<string, object>>(deserializedPerson.Dat)!; |
|||
if (deserialized.TryGetValue("Command", out var val) && val != null) Command = val.ToString()!; |
|||
if (Command == "Log")//回复请求的信息
|
|||
{ |
|||
DataRow? _user = MainWindowViewModel.Selet_Row(MainWindowViewModel._userData, "User='" + |
|||
deserializedPerson?.User + "' and Password ='" + deserializedPerson?.Password + "'"); |
|||
|
|||
data_.Add("Enterprise", _user?.Field<object>("Enterprise") ?? ""); |
|||
data_.Add("Department", _user?.Field<object>("Department") ?? ""); |
|||
data_.Add("Groups", _user?.Field<object>("Groups") ?? ""); |
|||
data_.Add("UserId", _user?.Field<object>("UserID") ?? ""); |
|||
data_.Add("UserName", _user?.Field<object>("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_); |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
public static List<Dictionary<string, object?>>? ToList(DataTable table)//datatable转list
|
|||
{ |
|||
if (table == null) return null; |
|||
|
|||
// 使用 AsEnumerable() 将 DataTable 转换为 IEnumerable<DataRow>
|
|||
// 然后投影为 Dictionary
|
|||
return table.AsEnumerable().Select(row => |
|||
table.Columns.Cast<DataColumn>().ToDictionary( |
|||
col => col.ColumnName, |
|||
col => { |
|||
var value = row[col]; |
|||
// 核心修改:如果是数据库空值 DBNull.Value,则转为 C# 的 null,否则保持原值
|
|||
return value == DBNull.Value ? null : value; |
|||
} |
|||
) |
|||
).ToList(); |
|||
} |
|||
|
|||
} |
|||
} |
|||
Loading…
Reference in new issue