From 6ef7aca8aee2b909f48c14932dd9922efd02414b Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 16 Apr 2024 06:29:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86record=E5=88=87=E6=8D=A2=E5=9B=9Eclass?= =?UTF-8?q?,=20=E4=B8=8D=E5=86=8D=E4=BD=BF=E7=94=A8CST=E6=97=B6=E9=97=B4,?= =?UTF-8?q?=E4=BD=BF=E7=94=A8DateTime.Now?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Communications/SimApiBaseRequest.cs | 25 ++++++++++++++++++++----- Communications/SimApiBaseResponse.cs | 27 ++++++++++++++++----------- Controllers/SimApiBaseController.cs | 2 +- Controllers/SimApiCommonController.cs | 7 +++++-- Helpers/SimApiUtil.cs | 18 ++++++++++++++++++ Logger/SimApiLogger.cs | 3 ++- Models/SimApiBaseModel.cs | 11 ++++------- Synapse/RpcServer.cs | 10 +++++----- 8 files changed, 71 insertions(+), 32 deletions(-) diff --git a/Communications/SimApiBaseRequest.cs b/Communications/SimApiBaseRequest.cs index d84f76b..982d423 100644 --- a/Communications/SimApiBaseRequest.cs +++ b/Communications/SimApiBaseRequest.cs @@ -1,22 +1,37 @@ -namespace SimApi.Communications; +using System.ComponentModel.DataAnnotations; + +namespace SimApi.Communications; /// /// 只有ID的请求 /// -public record SimApiIdOnlyRequest(int Id); +public class SimApiIdOnlyRequest +{ + [Required] public int Id { get; set; } +} /// /// 只有ID的请求(字符串) /// -public record SimApiStringIdOnlyRequest(string Id); +public class SimApiStringIdOnlyRequest +{ + [Required] public string Id { get; set; } +} /// /// 动态类型单字段请求 /// /// -public record SimApiOneFieldRequest(T Data); +public class SimApiOneFieldRequest +{ + [Required] public T Data { get; set; } +} /// /// 基础分页请求 /// -public record SimApiBasePageRequest(int Page, int Count); \ No newline at end of file +public class SimApiBasePageRequest +{ + [Required] public int Page { get; set; } + [Required] public int Count { get; set; } +} \ No newline at end of file diff --git a/Communications/SimApiBaseResponse.cs b/Communications/SimApiBaseResponse.cs index 5ff9d68..460cc03 100644 --- a/Communications/SimApiBaseResponse.cs +++ b/Communications/SimApiBaseResponse.cs @@ -7,8 +7,11 @@ namespace SimApi.Communications; /// /// 基础相应 /// -public record SimApiBaseResponse(int Code = 200, string Message = "成功") +public class SimApiBaseResponse(int code = 200, string message = "成功") { + public int Code { get; set; } = code; + public string Message { get; set; } = message; + /// /// 默认错误代码对应提示信息 /// @@ -23,7 +26,7 @@ public record SimApiBaseResponse(int Code = 200, string Message = "成功") { 500, "服务器错误" } }; - public SimApiBaseResponse(int code) : this(code, MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误") + public SimApiBaseResponse(int code) : this(code, MsgBox.GetValueOrDefault(code, "未知错误")) { } @@ -49,17 +52,19 @@ public record SimApiBaseResponse(int Code = 200, string Message = "成功") /// 动态内容分页 /// /// -public record SimApiBasePageResponse( - T List, - int Page = 1, - int Count = 1, - int Total = 1, - int Code = 200, - string Message = "成功") : SimApiBaseResponse(Code, Message); +public class SimApiBasePageResponse : SimApiBaseResponse +{ + public T List { get; set; } + public int Page { get; set; } = 1; + public int Count { get; set; } = 20; + public int Total { get; set; } +} /// /// 动态Data返回 /// /// -public record SimApiBaseResponse(T Data, int Code = 200, string Message = "成功") : SimApiBaseResponse(Code, - Message); \ No newline at end of file +public class SimApiBaseResponse : SimApiBaseResponse +{ + public T Data { get; set; } +} \ No newline at end of file diff --git a/Controllers/SimApiBaseController.cs b/Controllers/SimApiBaseController.cs index e3137a0..b9a35e2 100644 --- a/Controllers/SimApiBaseController.cs +++ b/Controllers/SimApiBaseController.cs @@ -104,6 +104,6 @@ public class SimApiBaseController : Controller /// protected SimApiBaseResponse UploadFile() { - return new SimApiBaseResponse(null); + return new SimApiBaseResponse(); } } \ No newline at end of file diff --git a/Controllers/SimApiCommonController.cs b/Controllers/SimApiCommonController.cs index 73f4078..c8167d5 100644 --- a/Controllers/SimApiCommonController.cs +++ b/Controllers/SimApiCommonController.cs @@ -28,7 +28,10 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController public SimApiBaseResponse CheckLogin() { ErrorWhenNull(LoginInfo, 401); - return new SimApiBaseResponse(LoginInfo.Id); + return new SimApiBaseResponse + { + Data = LoginInfo.Id + }; } /// @@ -45,7 +48,7 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController token = value; } - auth.Logout(token); + auth.Logout(token!); return new SimApiBaseResponse(); } } \ No newline at end of file diff --git a/Helpers/SimApiUtil.cs b/Helpers/SimApiUtil.cs index 2f19282..c659380 100644 --- a/Helpers/SimApiUtil.cs +++ b/Helpers/SimApiUtil.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.Encodings.Web; @@ -71,4 +72,21 @@ public static class SimApiUtil { return JsonSerializer.Serialize(obj, JsonOption); } + + /// + /// 分页 + /// + /// + /// 页码 + /// 每页数量 + /// + /// + public static IQueryable Paginate(this IQueryable query, int page, int count) + { + if (page < 1) + page = 1; + if (count <= 0) + count = 10; + return query.Skip((page - 1) * count).Take(count); + } } \ No newline at end of file diff --git a/Logger/SimApiLogger.cs b/Logger/SimApiLogger.cs index a663791..c2ea796 100644 --- a/Logger/SimApiLogger.cs +++ b/Logger/SimApiLogger.cs @@ -23,11 +23,12 @@ public class SimApiLogger(string name) : ILogger _ => ConsoleColor.White }; var message = - $"[ {name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n"; + $"[ {name} ][ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n"; if (exception != null) { message += $"{exception}\n"; } + Console.WriteLine(message); } } \ No newline at end of file diff --git a/Models/SimApiBaseModel.cs b/Models/SimApiBaseModel.cs index 6c42a57..cb0fea4 100644 --- a/Models/SimApiBaseModel.cs +++ b/Models/SimApiBaseModel.cs @@ -7,14 +7,11 @@ namespace SimApi.Models; public class SimApiBaseModel { - [Column(Order = 1)] - public string Id { get; set; } = Guid.NewGuid().ToString(); + [Column(Order = 1)] public string Id { get; set; } = Guid.NewGuid().ToString(); - [Column(Order = 9998)] - public DateTime UpdatedAt { get; set; } = SimApiUtil.CstNow; + [Column(Order = 9998)] public DateTime UpdatedAt { get; set; } = DateTime.Now; - [Column(Order = 9999)] - public DateTime CreatedAt { get; set; } = SimApiUtil.CstNow; + [Column(Order = 9999)] public DateTime CreatedAt { get; set; } = DateTime.Now; protected virtual string[] MapperIgnoreField { get; set; } = { "Id", "CreatedAt", "UpdatedAt" }; @@ -84,6 +81,6 @@ public class SimApiBaseModel /// public void UpdateTime() { - GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow); + GetType().GetProperty(UpdatedTimeField)?.SetValue(this, DateTime.Now); } } \ No newline at end of file diff --git a/Synapse/RpcServer.cs b/Synapse/RpcServer.cs index cc857bb..4981640 100644 --- a/Synapse/RpcServer.cs +++ b/Synapse/RpcServer.cs @@ -2,10 +2,6 @@ using System; using System.Linq; using System.Reflection; using System.Text; -using System.Text.Encodings.Web; -using System.Text.Json; -using System.Text.Json.Serialization; -using System.Text.Unicode; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using RabbitMQ.Client.Events; @@ -52,8 +48,12 @@ public partial class Synapse var paramObj = JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption); param = new[] { paramObj }; } + var ret = mt.Invoke(callClass, param); - res = new SimApiBaseResponse(ret); + res = new SimApiBaseResponse + { + Data = ret + }; } catch (TargetInvocationException e) {