From db6b01afa539da116643bccfdc83f250a37eed1d Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Fri, 15 Dec 2023 01:04:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=86=99=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=9A=84=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=9B=E4=BF=AE=E6=94=B9=E6=A0=B9=E6=8D=AE=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=97=A5=E6=9C=9F=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95=20(#71)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Utility/General.cs | 28 ++++++++++++++-------- Api/Utility/TextReader.cs | 28 ++++++++++++++++++++++ Docs/FunGame.Core.xml | 33 ++++++++++++++++++++++++-- Library/Common/Network/ServerSocket.cs | 2 -- Library/Common/Network/Socket.cs | 9 +++---- Library/Constant/TypeEnum.cs | 6 +++-- Service/JsonManager.cs | 20 ++++++++++++++++ Service/MailManager.cs | 5 ++-- 8 files changed, 109 insertions(+), 22 deletions(-) diff --git a/Api/Utility/General.cs b/Api/Utility/General.cs index 590cb18..aac9a10 100644 --- a/Api/Utility/General.cs +++ b/Api/Utility/General.cs @@ -209,7 +209,7 @@ namespace Milimoe.FunGame.Core.Api.Utility public static DateTime GetDateTime(TimeType type) { DateTime now = DateTime.Now; - if (type == TimeType.DateOnly) + if (type == TimeType.LongDateOnly || type == TimeType.ShortDateOnly) return now.Date; else return now; } @@ -221,8 +221,7 @@ namespace Milimoe.FunGame.Core.Api.Utility /// 转换失败返回当前时间 public static DateTime GetDateTime(string format) { - DateTime dt = default; - if (DateTime.TryParse(format, out dt)) + if (DateTime.TryParse(format, out DateTime dt)) { return dt; } @@ -242,8 +241,10 @@ namespace Milimoe.FunGame.Core.Api.Utility DateTime now = DateTime.Now; return type switch { - TimeType.DateOnly => now.Date.ToString("D"), - TimeType.TimeOnly => now.ToString("T"), + TimeType.LongDateOnly => now.Date.ToString("D"), + TimeType.ShortDateOnly => now.Date.ToString("d"), + TimeType.LongTimeOnly => now.ToString("T"), + TimeType.ShortTimeOnly => now.ToString("t"), TimeType.Year4 => now.Year.ToString(), TimeType.Year2 => "'" + now.ToString("yy"), TimeType.Month => now.Month.ToString(), @@ -256,9 +257,16 @@ namespace Milimoe.FunGame.Core.Api.Utility } /// - /// 获取系统短日期 + /// 获取系统时间并转为字符串 /// + /// 格式化字符串 /// + public static string GetDateTimeToString(string format) => DateTime.Now.ToString(format); + + /// + /// 获取系统短时间 + /// + /// 时:分:秒 public static string GetNowShortTime() { DateTime now = DateTime.Now; @@ -421,8 +429,8 @@ namespace Milimoe.FunGame.Core.Api.Utility private static string LetterVerifyCode(int length) { char[] Verification = new char[length]; - char[] Dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; + char[] Dictionary = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ]; Random random = new(); for (int i = 0; i < length; i++) { @@ -439,10 +447,10 @@ namespace Milimoe.FunGame.Core.Api.Utility private static string MixVerifyCode(int length) { char[] Verification = new char[length]; - char[] Dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + char[] Dictionary = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' - }; + ]; Random random = new(); for (int i = 0; i < length; i++) { diff --git a/Api/Utility/TextReader.cs b/Api/Utility/TextReader.cs index 0bde9f7..98af1f0 100644 --- a/Api/Utility/TextReader.cs +++ b/Api/Utility/TextReader.cs @@ -152,5 +152,33 @@ namespace Milimoe.FunGame.Core.Api.Utility } return ""; } + + /// + /// 写入TXT文件内容(如不存在文件会创建,反之新起一行追加) + /// + /// + /// 文件名 + /// 相对路径 + /// 内容 + public static void WriteTXT(string message, string filename, string path = "") + { + if (path.Trim() != "") + { + // 不存在文件夹将创建文件夹 + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + path = Path.Combine(path, filename); + } + else path = $@"{Environment.CurrentDirectory}\{filename}"; + // 写入内容 + StreamWriter writer = File.Exists(path) ? new(path, true, General.DefaultEncoding) : new(path, false, General.DefaultEncoding); + writer.WriteLine(message); + writer.Close(); + } + + /// + /// 追加错误日志 默认写入logs文件夹下的当日日期.log文件 + /// + /// + public static void AppendErrorLog(string msg) => WriteTXT(DateTimeUtility.GetDateTimeToString(TimeType.General) + ": " + msg + "\r\n", DateTimeUtility.GetDateTimeToString("yyyy-MM-dd") + ".log", "logs"); } } diff --git a/Docs/FunGame.Core.xml b/Docs/FunGame.Core.xml index 52a4134..9dd96c0 100644 --- a/Docs/FunGame.Core.xml +++ b/Docs/FunGame.Core.xml @@ -548,11 +548,18 @@ 格式化类型 + + + 获取系统时间并转为字符串 + + 格式化字符串 + + - 获取系统短日期 + 获取系统短时间 - + 时:分:秒 @@ -863,6 +870,21 @@ 相对路径 内容 + + + 写入TXT文件内容(如不存在文件会创建,反之新起一行追加) + + + 文件名 + 相对路径 + 内容 + + + + 追加错误日志 默认写入logs文件夹下的当日日期.log文件 + + + Aka. 2FA 双重认证 双因素身份验证 @@ -2487,6 +2509,13 @@ + + + 检查字符串是否为完整的JSON对象 + + + + 用于保存Smtp客户端 diff --git a/Library/Common/Network/ServerSocket.cs b/Library/Common/Network/ServerSocket.cs index e7892fd..c26361f 100644 --- a/Library/Common/Network/ServerSocket.cs +++ b/Library/Common/Network/ServerSocket.cs @@ -112,7 +112,5 @@ namespace Milimoe.FunGame.Core.Library.Common.Network { Instance?.Close(); } - - public static string GetTypeString(SocketMessageType type) => SocketSet.GetTypeString(type); } } diff --git a/Library/Common/Network/Socket.cs b/Library/Common/Network/Socket.cs index 6f94079..0978fa8 100644 --- a/Library/Common/Network/Socket.cs +++ b/Library/Common/Network/Socket.cs @@ -1,5 +1,6 @@ using Milimoe.FunGame.Core.Interface.Base; using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Core.Service; namespace Milimoe.FunGame.Core.Library.Common.Network @@ -66,8 +67,9 @@ namespace Milimoe.FunGame.Core.Library.Common.Network } return result; } - catch + catch (System.Exception e) { + Api.Utility.TXTHelper.AppendErrorLog(e.GetErrorInfo()); throw new SocketWrongInfoException(); } } @@ -87,8 +89,9 @@ namespace Milimoe.FunGame.Core.Library.Common.Network } return result; } - catch + catch (System.Exception e) { + Api.Utility.TXTHelper.AppendErrorLog(e.GetErrorInfo()); throw new SocketWrongInfoException(); } } @@ -178,7 +181,5 @@ namespace Milimoe.FunGame.Core.Library.Common.Network if (_HeartBeatFaileds++ >= 3) throw new LostConnectException(); } - - public static string GetTypeString(SocketMessageType type) => SocketSet.GetTypeString(type); } } diff --git a/Library/Constant/TypeEnum.cs b/Library/Constant/TypeEnum.cs index 2066da2..a997e83 100644 --- a/Library/Constant/TypeEnum.cs +++ b/Library/Constant/TypeEnum.cs @@ -223,8 +223,10 @@ namespace Milimoe.FunGame.Core.Library.Constant { None, General, - DateOnly, - TimeOnly, + LongDateOnly, + ShortDateOnly, + LongTimeOnly, + ShortTimeOnly, Year4, Year2, Month, diff --git a/Service/JsonManager.cs b/Service/JsonManager.cs index e81fff6..f9a572a 100644 --- a/Service/JsonManager.cs +++ b/Service/JsonManager.cs @@ -215,5 +215,25 @@ namespace Milimoe.FunGame.Core.Service json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组 return JsonSerializer.Deserialize>(json, options) ?? new List(); } + + /// + /// 检查字符串是否为完整的JSON对象 + /// + /// + /// + internal static bool IsCompleteJson(string json) + { + try + { + // 尝试解析JSON数据,如果成功则表示接收到完整的JSON + GetObject(json); + return true; + } + catch + { + // JSON解析失败,表示接收到的数据不完整 + return false; + } + } } } diff --git a/Service/MailManager.cs b/Service/MailManager.cs index 65b4ab3..c6c46c9 100644 --- a/Service/MailManager.cs +++ b/Service/MailManager.cs @@ -51,7 +51,7 @@ namespace Milimoe.FunGame.Core.Service SmtpClientInfo Info = Sender.SmtpClientInfo; SmtpClient Smtp; Guid MailSenderID = Sender.MailSenderID; - if (!SmtpClients.ContainsKey(MailSenderID)) + if (!SmtpClients.TryGetValue(MailSenderID, out SmtpClient? value)) { Smtp = new() { @@ -63,7 +63,7 @@ namespace Milimoe.FunGame.Core.Service }; SmtpClients.Add(MailSenderID, Smtp); } - else Smtp = SmtpClients[MailSenderID]; + else Smtp = value; MailMessage Msg = new() { Subject = Mail.Subject, @@ -92,6 +92,7 @@ namespace Milimoe.FunGame.Core.Service catch (Exception e) { ErrorMsg = e.GetErrorInfo(); + Api.Utility.TXTHelper.AppendErrorLog(ErrorMsg); return MailSendResult.Fail; } }