From 60be6d79c1471c627c214f85347244fd789cc3a5 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sun, 21 Jun 2020 22:22:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=9A=84=E9=80=9A=E7=9F=A5=E6=98=A0=E5=B0=84(=E4=B8=8D?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=9A=84=E6=83=B3=E6=B3=95,=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E7=94=A8ObjectMapper)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WeChatWeAppNotificationPublishProvider.cs | 2 -- .../Notifications/AbpNotificationOptions.cs | 3 ++ .../Internal/DefaultNotificationDispatcher.cs | 16 +++++++-- .../NotificationDataMappingDictionary.cs | 35 +++++++++++++++++++ .../NotificationDataMappingDictionaryItem.cs | 16 +++++++++ .../Notifications/NotificationPublishJob.cs | 11 +++++- .../LINGYUN.Abp.WeChat.Authorization.csproj | 7 ++++ .../Distributed/NotificationEventHandler.cs | 18 +++++++--- 8 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionary.cs create mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionaryItem.cs diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications.WeChat/LINGYUN/Abp/Notifications/WeChat/WeApp/WeChatWeAppNotificationPublishProvider.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications.WeChat/LINGYUN/Abp/Notifications/WeChat/WeApp/WeChatWeAppNotificationPublishProvider.cs index 6b71643f6..441086346 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications.WeChat/LINGYUN/Abp/Notifications/WeChat/WeApp/WeChatWeAppNotificationPublishProvider.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications.WeChat/LINGYUN/Abp/Notifications/WeChat/WeApp/WeChatWeAppNotificationPublishProvider.cs @@ -55,10 +55,8 @@ namespace LINGYUN.Abp.Notifications.WeChat.WeApp var weChatWeAppNotificationData = new WeChatWeAppSendNotificationData(identifier.UserName, templateId, redirect, weAppState, weAppLang); - // 写入模板数据 weChatWeAppNotificationData.WriteStandardData(NotificationData.ToStandardData(Options.DefaultMsgPrefix, notification.Data)); - // weChatWeAppNotificationData.WriteData(Options.DefaultMsgPrefix, notification.Data.Properties); Logger.LogDebug($"Sending wechat weapp notification: {notification.Name}"); // 发送小程序订阅消息 diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationOptions.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationOptions.cs index 45289f231..92b052240 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationOptions.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationOptions.cs @@ -7,10 +7,13 @@ namespace LINGYUN.Abp.Notifications public ITypeList DefinitionProviders { get; } public ITypeList PublishProviders { get; } + + public NotificationDataMappingDictionary NotificationDataMappings { get; } public AbpNotificationOptions() { PublishProviders = new TypeList(); DefinitionProviders = new TypeList(); + NotificationDataMappings = new NotificationDataMappingDictionary(); } } } diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/DefaultNotificationDispatcher.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/DefaultNotificationDispatcher.cs index b4802b2bd..3548976f2 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/DefaultNotificationDispatcher.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/DefaultNotificationDispatcher.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; @@ -23,6 +24,10 @@ namespace LINGYUN.Abp.Notifications.Internal /// public IDistributedEventBus DistributedEventBus { get; set; } /// + /// Reference to . + /// + private readonly AbpNotificationOptions _notificationOptions; + /// /// Reference to . /// private readonly IBackgroundJobManager _backgroundJobManager; @@ -43,13 +48,13 @@ namespace LINGYUN.Abp.Notifications.Internal /// public DefaultNotificationDispatcher( IBackgroundJobManager backgroundJobManager, - + IOptions options, INotificationStore notificationStore, INotificationDefinitionManager notificationDefinitionManager, INotificationPublishProviderManager notificationPublishProviderManager) { _backgroundJobManager = backgroundJobManager; - + _notificationOptions = options.Value; _notificationStore = notificationStore; _notificationDefinitionManager = notificationDefinitionManager; _notificationPublishProviderManager = notificationPublishProviderManager; @@ -184,7 +189,12 @@ namespace LINGYUN.Abp.Notifications.Internal try { Logger.LogDebug($"Sending notification with provider {provider.Name}"); - + var notifacationDataMapping = _notificationOptions.NotificationDataMappings + .GetMapItemOrNull(provider.Name, notificationInfo.CateGory); + if (notifacationDataMapping != null) + { + notificationInfo.Data = notifacationDataMapping.MappingFunc(notificationInfo.Data); + } // 发布 await provider.PublishAsync(notificationInfo, subscriptionUserIdentifiers); diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionary.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionary.cs new file mode 100644 index 000000000..008355e2a --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionary.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace LINGYUN.Abp.Notifications +{ + public class NotificationDataMappingDictionary : Dictionary> + { + public void Mapping(string cateGory, string provider, Func func) + { + if (ContainsKey(cateGory)) + { + this[cateGory] = new List(); + } + this[cateGory].Add(new NotificationDataMappingDictionaryItem(provider, func)); + } + + public void MappingAll(string provider, Func func) + { + foreach(var mapping in this) + { + Mapping(mapping.Key, provider, func); + } + } + + public NotificationDataMappingDictionaryItem GetMapItemOrNull(string cateGory, string provider) + { + if (ContainsKey(cateGory)) + { + return this[cateGory].FirstOrDefault(map => map.Provider.Equals(provider)); + } + return null; + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionaryItem.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionaryItem.cs new file mode 100644 index 000000000..24a82a7ad --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionaryItem.cs @@ -0,0 +1,16 @@ +using System; + +namespace LINGYUN.Abp.Notifications +{ + public class NotificationDataMappingDictionaryItem + { + public string Provider { get; } + + public Func MappingFunc { get; } + public NotificationDataMappingDictionaryItem(string prodiver, Func func) + { + Provider = prodiver; + MappingFunc = func; + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJob.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJob.cs index 2ae41faff..194739e49 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJob.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJob.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using System; using System.Threading.Tasks; using Volo.Abp.BackgroundJobs; @@ -8,14 +9,17 @@ namespace LINGYUN.Abp.Notifications { public class NotificationPublishJob : AsyncBackgroundJob, ITransientDependency { + protected AbpNotificationOptions Options { get; } protected INotificationStore Store { get; } protected IServiceProvider ServiceProvider { get; } public NotificationPublishJob( + IOptions options, INotificationStore store, IServiceProvider serviceProvider) { Store = store; + Options = options.Value; ServiceProvider = serviceProvider; } @@ -26,7 +30,12 @@ namespace LINGYUN.Abp.Notifications if (ServiceProvider.GetRequiredService(providerType) is INotificationPublishProvider publishProvider) { var notification = await Store.GetNotificationOrNullAsync(args.TenantId, args.NotificationId); - + var notifacationDataMapping = Options.NotificationDataMappings + .GetMapItemOrNull(publishProvider.Name, notification.CateGory); + if (notifacationDataMapping != null) + { + notification.Data = notifacationDataMapping.MappingFunc(notification.Data); + } await publishProvider.PublishAsync(notification, args.UserIdentifiers); } } diff --git a/aspnet-core/modules/common/LINGYUN.Abp.WeChat.Authorization/LINGYUN.Abp.WeChat.Authorization.csproj b/aspnet-core/modules/common/LINGYUN.Abp.WeChat.Authorization/LINGYUN.Abp.WeChat.Authorization.csproj index 95b04ed53..46b1f68bd 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.WeChat.Authorization/LINGYUN.Abp.WeChat.Authorization.csproj +++ b/aspnet-core/modules/common/LINGYUN.Abp.WeChat.Authorization/LINGYUN.Abp.WeChat.Authorization.csproj @@ -3,6 +3,13 @@ netstandard2.0 + true + 2.9.0 + LINGYUN + + + + D:\LocalNuget diff --git a/aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs b/aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs index a8623dea1..a3ffed6e2 100644 --- a/aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs +++ b/aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs @@ -1,6 +1,7 @@ using LINGYUN.Abp.Notifications; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; @@ -26,9 +27,13 @@ namespace LINGYUN.Abp.MessageService.EventBus.Distributed /// public ILogger Logger { get; set; } /// + /// Reference to . + /// + protected AbpNotificationOptions Options { get; } + /// /// Reference to . /// - protected IBackgroundJobManager BackgroundJobManager; + protected IBackgroundJobManager BackgroundJobManager { get; } /// /// Reference to . /// @@ -43,12 +48,12 @@ namespace LINGYUN.Abp.MessageService.EventBus.Distributed /// public NotificationEventHandler( IBackgroundJobManager backgroundJobManager, - + IOptions options, INotificationStore notificationStore, INotificationPublishProviderManager notificationPublishProviderManager) { BackgroundJobManager = backgroundJobManager; - + Options = options.Value; NotificationStore = notificationStore; NotificationPublishProviderManager = notificationPublishProviderManager; @@ -117,7 +122,12 @@ namespace LINGYUN.Abp.MessageService.EventBus.Distributed try { Logger.LogDebug($"Sending notification with provider {provider.Name}"); - + var notifacationDataMapping = Options.NotificationDataMappings + .GetMapItemOrNull(provider.Name, notificationInfo.CateGory); + if (notifacationDataMapping != null) + { + notificationInfo.Data = notifacationDataMapping.MappingFunc(notificationInfo.Data); + } // 发布 await provider.PublishAsync(notificationInfo, subscriptionUserIdentifiers);