From a5899023b90a933e2a5a422e367be4534a0b6f60 Mon Sep 17 00:00:00 2001
From: hang <872297557@qq.com>
Date: Wed, 21 Feb 2024 14:33:38 +0800
Subject: [PATCH] =?UTF-8?q?[=E6=97=B6=E5=8C=BA=E4=BF=AE=E6=94=B9-=E8=B0=83?=
=?UTF-8?q?=E6=95=B4=E5=90=8D=E7=A7=B0]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../TimeZoneAdjustmentMiddleware.cs | 3 +++
...eConverter.cs => JSONTimeZoneConverter.cs} | 21 ++++++-------------
.../NewtonsoftJson/NewtonsoftJsonSetup.cs | 4 ++--
3 files changed, 11 insertions(+), 17 deletions(-)
rename IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/{JSONCustomDateConverter.cs => JSONTimeZoneConverter.cs} (81%)
diff --git a/IRaCIS.Core.API/Middleware/TimeZoneAdjustmentMiddleware.cs b/IRaCIS.Core.API/Middleware/TimeZoneAdjustmentMiddleware.cs
index d7975a395..c3c71da2a 100644
--- a/IRaCIS.Core.API/Middleware/TimeZoneAdjustmentMiddleware.cs
+++ b/IRaCIS.Core.API/Middleware/TimeZoneAdjustmentMiddleware.cs
@@ -7,6 +7,9 @@ using System.IO;
using System.Text;
using System.Threading.Tasks;
+///
+/// 废弃,没用
+///
public class TimeZoneAdjustmentMiddleware
{
private readonly RequestDelegate _next;
diff --git a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONCustomDateConverter.cs b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONTimeZoneConverter.cs
similarity index 81%
rename from IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONCustomDateConverter.cs
rename to IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONTimeZoneConverter.cs
index 0bb4eb356..75c5525ab 100644
--- a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONCustomDateConverter.cs
+++ b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/JSONTimeZoneConverter.cs
@@ -9,16 +9,14 @@ using System.Globalization;
namespace IRaCIS.Core.API
{
///
- /// 废弃,没用,不用这种处理方式
+ /// 序列化,反序列化的时候,处理时间 时区转换
///
- public class JSONCustomDateConverter : DateTimeConverterBase
+ public class JSONTimeZoneConverter : DateTimeConverterBase
{
private readonly IHttpContextAccessor _httpContextAccessor;
- private readonly string timeZoneId;
-
private readonly TimeZoneInfo _clientTimeZone;
- public JSONCustomDateConverter(IHttpContextAccessor httpContextAccessor)
+ public JSONTimeZoneConverter(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
@@ -37,8 +35,6 @@ namespace IRaCIS.Core.API
}
- private static readonly TimeZoneInfo ChinaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Etc/UTC");
-
public override bool CanConvert(Type objectType)
{
// 仅支持 DateTime 类型的转换
@@ -64,19 +60,14 @@ namespace IRaCIS.Core.API
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
- {
-
-
-
-
-
+ {
DateTime? nullableDateTime = value as DateTime?;
if (nullableDateTime != null && nullableDateTime.HasValue)
{
//第一个参数默认使用系统本地时区 也就是应用服务器的时区
- DateTime chinaTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, _clientTimeZone);
- writer.WriteValue(chinaTime);
+ DateTime clientZoneTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, _clientTimeZone);
+ writer.WriteValue(clientZoneTime);
}
else
{
diff --git a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/NewtonsoftJsonSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/NewtonsoftJsonSetup.cs
index 0ffcf0384..853afe9d3 100644
--- a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/NewtonsoftJsonSetup.cs
+++ b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/NewtonsoftJsonSetup.cs
@@ -11,7 +11,7 @@ namespace IRaCIS.Core.API
public static void AddNewtonsoftJsonSetup(this IMvcBuilder builder, IServiceCollection services)
{
services.AddHttpContextAccessor();
- services.AddScoped();
+ services.AddScoped();
builder.AddNewtonsoftJson(options =>
{
@@ -28,7 +28,7 @@ namespace IRaCIS.Core.API
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
//options.SerializerSettings.Converters.Add(new JSONCustomDateConverter()) ;
- options.SerializerSettings.Converters.Add(services.BuildServiceProvider().GetService());
+ options.SerializerSettings.Converters.Add(services.BuildServiceProvider().GetService());
//IsoDateTimeConverter
//options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;