Browse Source

增加自定义的通知映射(不完善的想法,应该用ObjectMapper)

pull/8/head
cKey 6 years ago
parent
commit
60be6d79c1
  1. 2
      aspnet-core/modules/common/LINGYUN.Abp.Notifications.WeChat/LINGYUN/Abp/Notifications/WeChat/WeApp/WeChatWeAppNotificationPublishProvider.cs
  2. 3
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationOptions.cs
  3. 16
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/DefaultNotificationDispatcher.cs
  4. 35
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionary.cs
  5. 16
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataMappingDictionaryItem.cs
  6. 11
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationPublishJob.cs
  7. 7
      aspnet-core/modules/common/LINGYUN.Abp.WeChat.Authorization/LINGYUN.Abp.WeChat.Authorization.csproj
  8. 18
      aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs

2
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}");
// 发送小程序订阅消息

3
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationOptions.cs

@ -7,10 +7,13 @@ namespace LINGYUN.Abp.Notifications
public ITypeList<INotificationDefinitionProvider> DefinitionProviders { get; }
public ITypeList<INotificationPublishProvider> PublishProviders { get; }
public NotificationDataMappingDictionary NotificationDataMappings { get; }
public AbpNotificationOptions()
{
PublishProviders = new TypeList<INotificationPublishProvider>();
DefinitionProviders = new TypeList<INotificationDefinitionProvider>();
NotificationDataMappings = new NotificationDataMappingDictionary();
}
}
}

16
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
/// </summary>
public IDistributedEventBus DistributedEventBus { get; set; }
/// <summary>
/// Reference to <see cref="AbpNotificationOptions"/>.
/// </summary>
private readonly AbpNotificationOptions _notificationOptions;
/// <summary>
/// Reference to <see cref="IBackgroundJobManager"/>.
/// </summary>
private readonly IBackgroundJobManager _backgroundJobManager;
@ -43,13 +48,13 @@ namespace LINGYUN.Abp.Notifications.Internal
/// </summary>
public DefaultNotificationDispatcher(
IBackgroundJobManager backgroundJobManager,
IOptions<AbpNotificationOptions> 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);

35
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<string, List<NotificationDataMappingDictionaryItem>>
{
public void Mapping(string cateGory, string provider, Func<NotificationData, NotificationData> func)
{
if (ContainsKey(cateGory))
{
this[cateGory] = new List<NotificationDataMappingDictionaryItem>();
}
this[cateGory].Add(new NotificationDataMappingDictionaryItem(provider, func));
}
public void MappingAll(string provider, Func<NotificationData, NotificationData> 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;
}
}
}

16
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<NotificationData, NotificationData> MappingFunc { get; }
public NotificationDataMappingDictionaryItem(string prodiver, Func<NotificationData, NotificationData> func)
{
Provider = prodiver;
MappingFunc = func;
}
}
}

11
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<NotificationPublishJobArgs>, ITransientDependency
{
protected AbpNotificationOptions Options { get; }
protected INotificationStore Store { get; }
protected IServiceProvider ServiceProvider { get; }
public NotificationPublishJob(
IOptions<AbpNotificationOptions> 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);
}
}

7
aspnet-core/modules/common/LINGYUN.Abp.WeChat.Authorization/LINGYUN.Abp.WeChat.Authorization.csproj

@ -3,6 +3,13 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace />
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.9.0</Version>
<Authors>LINGYUN</Authors>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>D:\LocalNuget</OutputPath>
</PropertyGroup>
<ItemGroup>

18
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
/// </summary>
public ILogger<NotificationEventHandler> Logger { get; set; }
/// <summary>
/// Reference to <see cref="AbpNotificationOptions"/>.
/// </summary>
protected AbpNotificationOptions Options { get; }
/// <summary>
/// Reference to <see cref="IBackgroundJobManager"/>.
/// </summary>
protected IBackgroundJobManager BackgroundJobManager;
protected IBackgroundJobManager BackgroundJobManager { get; }
/// <summary>
/// Reference to <see cref="INotificationStore"/>.
/// </summary>
@ -43,12 +48,12 @@ namespace LINGYUN.Abp.MessageService.EventBus.Distributed
/// </summary>
public NotificationEventHandler(
IBackgroundJobManager backgroundJobManager,
IOptions<AbpNotificationOptions> 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);

Loading…
Cancel
Save