From c654b091abf8ce2460ba43caaacdfb8f65254864 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Tue, 28 Feb 2023 12:05:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(notifications):=20=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=BA=8F=E5=88=97=E5=8C=96=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Abp/Notifications/NotificationData.cs | 14 +++++-- .../DefaultNotificationDataSerializer.cs | 41 +++++++++++++++++++ .../INotificationDataSerializer.cs | 5 +++ .../NotificationDataConverter.cs | 39 ------------------ .../Distributed/NotificationEventHandler.cs | 9 +++- 5 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs create mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs delete mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataConverter.cs diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationData.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationData.cs index 60e0bb311..cb2aa2ac1 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationData.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationData.cs @@ -1,5 +1,6 @@ using LINGYUN.Abp.RealTime.Localization; using System; +using System.Globalization; using Volo.Abp.Data; using Volo.Abp.EventBus; @@ -20,6 +21,10 @@ namespace LINGYUN.Abp.Notifications /// 用来标识是否需要本地化的信息 /// public const string LocalizerKey = "L"; + /// + /// 用于本地化文化 + /// + public const string CultureKey = "C"; public virtual string Type => GetType().FullName; @@ -58,13 +63,15 @@ namespace LINGYUN.Abp.Notifications LocalizableStringInfo message, DateTime createTime, string formUser, - LocalizableStringInfo description = null) + LocalizableStringInfo description = null, + string culture = null) { TrySetData("title", title); TrySetData("message", message); TrySetData("formUser", formUser); TrySetData("createTime", createTime); TrySetData(LocalizerKey, true); + TrySetData(CultureKey, culture ?? "en"); if (description != null) { TrySetData("description", description); @@ -155,9 +162,10 @@ namespace LINGYUN.Abp.Notifications public bool NeedLocalizer() { var localizer = TryGetData(LocalizerKey); - if (localizer != null && localizer is bool needLocalizer) + if (localizer != null && + bool.TryParse(localizer.ToString(), out var isLocalizer)) { - return needLocalizer; + return isLocalizer; } return false; } diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs new file mode 100644 index 000000000..b53072331 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs @@ -0,0 +1,41 @@ +using LINGYUN.Abp.RealTime.Localization; +using Newtonsoft.Json; +using Volo.Abp.DependencyInjection; + +namespace LINGYUN.Abp.Notifications; + +[Dependency(TryRegister = true)] +public class DefaultNotificationDataSerializer : INotificationDataSerializer, ISingletonDependency +{ + public NotificationData Serialize(NotificationData source) + { + if (source != null) + { + if (source.NeedLocalizer()) + { + // 潜在的空对象引用修复 + if (source.ExtraProperties.TryGetValue("title", out var title) && title != null) + { + var titleObj = JsonConvert.DeserializeObject(title.ToString()); + source.TrySetData("title", titleObj); + } + if (source.ExtraProperties.TryGetValue("message", out var message) && message != null) + { + var messageObj = JsonConvert.DeserializeObject(message.ToString()); + source.TrySetData("message", messageObj); + } + + if (source.ExtraProperties.TryGetValue("description", out var description) && description != null) + { + var descriptionObj = JsonConvert.DeserializeObject(description.ToString()); + source.TrySetData("description", descriptionObj); + } + } + } + else + { + source = new NotificationData(); + } + return source; + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs new file mode 100644 index 000000000..bef764b6c --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs @@ -0,0 +1,5 @@ +namespace LINGYUN.Abp.Notifications; +public interface INotificationDataSerializer +{ + NotificationData Serialize(NotificationData source); +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataConverter.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataConverter.cs deleted file mode 100644 index 3bba55679..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataConverter.cs +++ /dev/null @@ -1,39 +0,0 @@ -using LINGYUN.Abp.RealTime.Localization; -using Newtonsoft.Json; - -namespace LINGYUN.Abp.Notifications -{ - public class NotificationDataConverter - { - public static NotificationData Convert(NotificationData notificationData) - { - if (notificationData != null) - { - if (notificationData.NeedLocalizer()) - { - // 潜在的空对象引用修复 - if (notificationData.ExtraProperties.TryGetValue("title", out object title) && title != null) - { - var titleObj = JsonConvert.DeserializeObject(title.ToString()); - notificationData.TrySetData("title", titleObj); - } - if (notificationData.ExtraProperties.TryGetValue("message", out object message) && message != null) - { - var messageObj = JsonConvert.DeserializeObject(message.ToString()); - notificationData.TrySetData("message", messageObj); - } - - if (notificationData.ExtraProperties.TryGetValue("description", out object description) && description != null) - { - notificationData.TrySetData("description", JsonConvert.DeserializeObject(description.ToString())); - } - } - } - else - { - notificationData = new NotificationData(); - } - return notificationData; - } - } -} diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs index a51f8fe8c..0c61e997a 100644 --- a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs @@ -68,6 +68,10 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed /// protected IStringLocalizerFactory StringLocalizerFactory { get; } /// + /// Reference to . + /// + protected INotificationDataSerializer NotificationDataSerializer { get; } + /// /// Reference to . /// protected INotificationDefinitionManager NotificationDefinitionManager { get; } @@ -92,6 +96,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed IStringLocalizerFactory stringLocalizerFactory, IOptions options, INotificationStore notificationStore, + INotificationDataSerializer notificationDataSerializer, INotificationDefinitionManager notificationDefinitionManager, INotificationSubscriptionManager notificationSubscriptionManager, INotificationPublishProviderManager notificationPublishProviderManager) @@ -104,6 +109,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed BackgroundJobManager = backgroundJobManager; StringLocalizerFactory = stringLocalizerFactory; NotificationStore = notificationStore; + NotificationDataSerializer = notificationDataSerializer; NotificationDefinitionManager = notificationDefinitionManager; NotificationSubscriptionManager = notificationSubscriptionManager; NotificationPublishProviderManager = notificationPublishProviderManager; @@ -289,8 +295,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed }; notificationInfo.SetId(eventData.Id); - // TODO: 可以做成一个接口来序列化消息 - notificationInfo.Data = NotificationDataConverter.Convert(notificationInfo.Data); + notificationInfo.Data = NotificationDataSerializer.Serialize(notificationInfo.Data); // 获取用户订阅 var subscriptionUsers = await GerSubscriptionUsersAsync(