diff --git a/aspnet-core/framework/common/LINGYUN.Abp.RealTime/LINGYUN/Abp/RealTime/Localization/LocalizableStringInfo.cs b/aspnet-core/framework/common/LINGYUN.Abp.RealTime/LINGYUN/Abp/RealTime/Localization/LocalizableStringInfo.cs index 10e29e245..ecdfc31c0 100644 --- a/aspnet-core/framework/common/LINGYUN.Abp.RealTime/LINGYUN/Abp/RealTime/Localization/LocalizableStringInfo.cs +++ b/aspnet-core/framework/common/LINGYUN.Abp.RealTime/LINGYUN/Abp/RealTime/Localization/LocalizableStringInfo.cs @@ -24,6 +24,7 @@ public class LocalizableStringInfo /// public LocalizableStringInfo() { + Values = new Dictionary(); } /// /// Instantiate @@ -38,6 +39,6 @@ public class LocalizableStringInfo { ResourceName = resourceName; Name = name; - Values = values; + Values = values ?? new Dictionary(); } } diff --git a/aspnet-core/modules/localization-management/LINGYUN.Abp.LocalizationManagement.Application/LINGYUN/Abp/LocalizationManagement/TextAppService.cs b/aspnet-core/modules/localization-management/LINGYUN.Abp.LocalizationManagement.Application/LINGYUN/Abp/LocalizationManagement/TextAppService.cs index 90973a94b..040a1502f 100644 --- a/aspnet-core/modules/localization-management/LINGYUN.Abp.LocalizationManagement.Application/LINGYUN/Abp/LocalizationManagement/TextAppService.cs +++ b/aspnet-core/modules/localization-management/LINGYUN.Abp.LocalizationManagement.Application/LINGYUN/Abp/LocalizationManagement/TextAppService.cs @@ -1,13 +1,13 @@ using LINGYUN.Abp.LocalizationManagement.Features; using LINGYUN.Abp.LocalizationManagement.Permissions; using Microsoft.AspNetCore.Authorization; -using System.Runtime.Versioning; using System.Threading.Tasks; +using Volo.Abp.Features; namespace LINGYUN.Abp.LocalizationManagement; [Authorize(LocalizationManagementPermissions.Text.Default)] -[RequiresPreviewFeatures(LocalizationManagementFeatures.Enable)] +[RequiresFeature(LocalizationManagementFeatures.Enable)] public class TextAppService : LocalizationAppServiceBase, ITextAppService { private readonly ITextRepository _textRepository; diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationStandardData.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationStandardData.cs new file mode 100644 index 000000000..12e3096fa --- /dev/null +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationStandardData.cs @@ -0,0 +1,21 @@ +namespace LINGYUN.Abp.Notifications; +public class NotificationStandardData +{ + public string Title { get; set; } + public string Message { get; set; } + public string Description { get; set; } + public NotificationStandardData() + { + + } + + public NotificationStandardData( + string title, + string message, + string description = null) + { + Title = title; + Message = message; + Description = description; + } +} diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Emailing/LINGYUN/Abp/Notifications/Emailing/EmailingNotificationPublishProvider.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Emailing/LINGYUN/Abp/Notifications/Emailing/EmailingNotificationPublishProvider.cs index 574475263..1ecbbf5bb 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Emailing/LINGYUN/Abp/Notifications/Emailing/EmailingNotificationPublishProvider.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Emailing/LINGYUN/Abp/Notifications/Emailing/EmailingNotificationPublishProvider.cs @@ -1,45 +1,23 @@ using LINGYUN.Abp.Identity; -using LINGYUN.Abp.RealTime.Localization; -using Markdig; -using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Emailing; -using Volo.Abp.Localization; +using Volo.Abp.Features; namespace LINGYUN.Abp.Notifications.Emailing; public class EmailingNotificationPublishProvider : NotificationPublishProvider { public const string ProviderName = NotificationProviderNames.Emailing; - public override string Name => ProviderName; - - protected AbpLocalizationOptions LocalizationOptions { get; } - - protected IEmailSender EmailSender { get; } - - protected IStringLocalizerFactory LocalizerFactory { get; } - - protected IIdentityUserRepository UserRepository { get; } - - public EmailingNotificationPublishProvider( - IEmailSender emailSender, - IStringLocalizerFactory localizerFactory, - IIdentityUserRepository userRepository, - IOptions localizationOptions) - { - EmailSender = emailSender; - LocalizerFactory = localizerFactory; - UserRepository = userRepository; - - LocalizationOptions = localizationOptions.Value; - } + protected IEmailSender EmailSender => ServiceProvider.LazyGetRequiredService(); + protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService(); + protected IIdentityUserRepository UserRepository => ServiceProvider.LazyGetRequiredService(); + protected INotificationDataSerializer NotificationDataSerializer => ServiceProvider.LazyGetRequiredService(); protected async override Task PublishAsync( NotificationInfo notification, @@ -55,56 +33,8 @@ public class EmailingNotificationPublishProvider : NotificationPublishProvider Logger.LogWarning("The subscriber did not confirm the email address and could not send email notifications!"); return; } + var notificationData = await NotificationDataSerializer.ToStandard(notification.Data); - if (!notification.Data.NeedLocalizer()) - { - var title = notification.Data.TryGetData("title").ToString(); - var message = notification.Data.TryGetData("message").ToString(); - // markdown进行处理 - if (notification.ContentType == NotificationContentType.Markdown) - { - message = Markdown.ToHtml(message); - } - - await EmailSender.SendAsync(emailAddress, title, message); - } - else - { - var titleInfo = notification.Data.TryGetData("title").As(); - var titleLocalizer = await LocalizerFactory.CreateByResourceNameAsync(titleInfo.ResourceName); - var title = titleLocalizer[titleInfo.Name].Value; - if (titleInfo.Values != null) - { - foreach (var formatTitle in titleInfo.Values) - { - if (formatTitle.Key != null && formatTitle.Value != null) - { - title = title.Replace($"{{{formatTitle.Key}}}", formatTitle.Value.ToString()); - } - } - } - - var messageInfo = notification.Data.TryGetData("message").As(); - var messageLocalizer = await LocalizerFactory.CreateByResourceNameAsync(messageInfo.ResourceName); - var message = messageLocalizer[messageInfo.Name].Value; - if (messageInfo.Values != null) - { - foreach (var formatMessage in messageInfo.Values) - { - if (formatMessage.Key != null && formatMessage.Value != null) - { - message = message.Replace($"{{{formatMessage.Key}}}", formatMessage.Value.ToString()); - } - } - } - - // markdown进行处理 - if (notification.ContentType == NotificationContentType.Markdown) - { - message = Markdown.ToHtml(message); - } - - await EmailSender.SendAsync(emailAddress, title, message); - } + await EmailSender.SendAsync(emailAddress, notificationData.Title, notificationData.Message); } } diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs index bc5fcbf33..ef5d79241 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs @@ -1,49 +1,25 @@ using LINGYUN.Abp.PushPlus.Channel; using LINGYUN.Abp.PushPlus.Features; using LINGYUN.Abp.PushPlus.Message; -using LINGYUN.Abp.RealTime.Localization; -using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using System; using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Features; -using Volo.Abp.Localization; +using Volo.Abp.Settings; namespace LINGYUN.Abp.Notifications.PushPlus; public class PushPlusNotificationPublishProvider : NotificationPublishProvider { public const string ProviderName = "PushPlus"; - public override string Name => ProviderName; - - protected IFeatureChecker FeatureChecker { get; } - - protected IPushPlusMessageSender PushPlusMessageSender { get; } - - protected IStringLocalizerFactory LocalizerFactory { get; } - - protected AbpLocalizationOptions LocalizationOptions { get; } - - protected INotificationDefinitionManager NotificationDefinitionManager { get; } - - public PushPlusNotificationPublishProvider( - IFeatureChecker featureChecker, - IPushPlusMessageSender pushPlusMessageSender, - IStringLocalizerFactory localizerFactory, - IOptions localizationOptions, - INotificationDefinitionManager notificationDefinitionManager) - { - FeatureChecker = featureChecker; - PushPlusMessageSender = pushPlusMessageSender; - LocalizerFactory = localizerFactory; - LocalizationOptions = localizationOptions.Value; - NotificationDefinitionManager = notificationDefinitionManager; - } + protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService(); + protected ISettingProvider SettingProvider => ServiceProvider.LazyGetRequiredService(); + protected IPushPlusMessageSender PushPlusMessageSender => ServiceProvider.LazyGetRequiredService(); + protected INotificationDataSerializer NotificationDataSerializer => ServiceProvider.LazyGetRequiredService(); + protected INotificationDefinitionManager NotificationDefinitionManager => ServiceProvider.LazyGetRequiredService(); protected async override Task CanPublishAsync(NotificationInfo notification, CancellationToken cancellationToken = default) { @@ -77,49 +53,16 @@ public class PushPlusNotificationPublishProvider : NotificationPublishProvider ?? PushPlusMessageTemplate.Text; var webhook = notification.Data.GetWebhookOrNull() ?? ""; var callbackUrl = notification.Data.GetCallbackUrlOrNull() ?? ""; - - if (!notification.Data.NeedLocalizer()) - { - var title = notification.Data.TryGetData("title").ToString(); - var message = notification.Data.TryGetData("message").ToString(); - - await PushPlusMessageSender.SendWithChannelAsync( - title, - message, - topic, - channelType: channel, - template: template, - webhook: webhook, - callbackUrl: callbackUrl, - cancellationToken: cancellationToken); - } - else - { - var titleInfo = notification.Data.TryGetData("title").As(); - var titleResource = GetResource(titleInfo.ResourceName); - var titleLocalizer = await LocalizerFactory.CreateByResourceNameAsync(titleResource.ResourceName); - var title = titleLocalizer[titleInfo.Name, titleInfo.Values].Value; - - var messageInfo = notification.Data.TryGetData("message").As(); - var messageResource = GetResource(messageInfo.ResourceName); - var messageLocalizer = await LocalizerFactory.CreateByResourceNameAsync(messageResource.ResourceName); - var message = messageLocalizer[messageInfo.Name, messageInfo.Values].Value; - - await PushPlusMessageSender.SendWithChannelAsync( - title, - message, - topic, - channelType: channel, - template: template, - webhook: webhook, - callbackUrl: callbackUrl, - cancellationToken: cancellationToken); - } - } - - private LocalizationResourceBase GetResource(string resourceName) - { - return LocalizationOptions.Resources.Values - .First(x => x.ResourceName.Equals(resourceName)); + var notificationData = await NotificationDataSerializer.ToStandard(notification.Data); + + await PushPlusMessageSender.SendWithChannelAsync( + notificationData.Title, + notificationData.Message, + topic, + channelType: channel, + template: template, + webhook: webhook, + callbackUrl: callbackUrl, + cancellationToken: cancellationToken); } } diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Sms/LINGYUN/Abp/Notifications/Sms/SmsNotificationPublishProvider.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Sms/LINGYUN/Abp/Notifications/Sms/SmsNotificationPublishProvider.cs index 3a5933f48..9306ddf09 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Sms/LINGYUN/Abp/Notifications/Sms/SmsNotificationPublishProvider.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Sms/LINGYUN/Abp/Notifications/Sms/SmsNotificationPublishProvider.cs @@ -9,21 +9,10 @@ namespace LINGYUN.Abp.Notifications.Sms; public class SmsNotificationPublishProvider : NotificationPublishProvider { public const string ProviderName = NotificationProviderNames.Sms; - - protected IUserPhoneFinder UserPhoneFinder => ServiceProvider.LazyGetRequiredService(); - protected ISmsNotificationSender Sender { get; } - - protected AbpNotificationsSmsOptions Options { get; } - - public SmsNotificationPublishProvider( - ISmsNotificationSender sender, - IOptions options) - { - Sender = sender; - Options = options.Value; - } - public override string Name => ProviderName; + protected IUserPhoneFinder UserPhoneFinder => ServiceProvider.LazyGetRequiredService(); + protected ISmsNotificationSender Sender => ServiceProvider.LazyGetRequiredService(); + protected IOptions Options => ServiceProvider.LazyGetRequiredService>(); protected override async Task PublishAsync( NotificationInfo notification, diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Templating/LINGYUN.Abp.Notifications.Templating.csproj b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Templating/LINGYUN.Abp.Notifications.Templating.csproj index 188573b06..50a50fc39 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Templating/LINGYUN.Abp.Notifications.Templating.csproj +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Templating/LINGYUN.Abp.Notifications.Templating.csproj @@ -4,7 +4,7 @@ - netstandard2.0;netstandard2.1;net8.0 + netstandard2.0;netstandard2.1;net8.0;net9.0 LINGYUN.Abp.Notifications.Templating LINGYUN.Abp.Notifications.Templating false diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs index bb02fbd1b..65e7e1b50 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs @@ -18,18 +18,9 @@ public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishP { public const string ProviderName = NotificationProviderNames.WechatMiniProgram; public override string Name => ProviderName; - protected IFeatureChecker FeatureChecker { get; } - protected ISubscribeMessager SubscribeMessager { get; } - protected AbpNotificationsWeChatMiniProgramOptions Options { get; } - public WeChatMiniProgramNotificationPublishProvider( - IFeatureChecker featureChecker, - ISubscribeMessager subscribeMessager, - IOptions options) - { - Options = options.Value; - FeatureChecker = featureChecker; - SubscribeMessager = subscribeMessager; - } + protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService(); + protected ISubscribeMessager SubscribeMessager => ServiceProvider.LazyGetRequiredService(); + protected IOptions Options => ServiceProvider.LazyGetRequiredService>(); protected async override Task CanPublishAsync(NotificationInfo notification, CancellationToken cancellationToken = default) { @@ -75,10 +66,10 @@ public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishP var redirect = GetOrDefault(notification.Data, "RedirectPage", null); Logger.LogDebug($"Get wechat weapp redirect page: {redirect ?? "null"}"); - var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.DefaultState); + var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.Value.DefaultState); Logger.LogDebug($"Get wechat weapp state: {weAppState ?? null}"); - var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.DefaultLanguage); + var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.Value.DefaultLanguage); Logger.LogDebug($"Get wechat weapp language: {weAppLang ?? null}"); // TODO: 如果微信端发布通知,请组装好 openid 字段在通知数据内容里面 @@ -107,7 +98,7 @@ public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishP protected string GetOrDefaultTemplateId(NotificationData data) { - return GetOrDefault(data, "TemplateId", Options.DefaultTemplateId); + return GetOrDefault(data, "TemplateId", Options.Value.DefaultTemplateId); } protected string GetOrDefault(NotificationData data, string key, string defaultValue) diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.Work/LINGYUN/Abp/Notifications/WeChat/Work/WeChatWorkNotificationPublishProvider.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.Work/LINGYUN/Abp/Notifications/WeChat/Work/WeChatWorkNotificationPublishProvider.cs index 2eaf314ac..ae84a648b 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.Work/LINGYUN/Abp/Notifications/WeChat/Work/WeChatWorkNotificationPublishProvider.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.Work/LINGYUN/Abp/Notifications/WeChat/Work/WeChatWorkNotificationPublishProvider.cs @@ -1,10 +1,8 @@ -using LINGYUN.Abp.RealTime.Localization; -using LINGYUN.Abp.WeChat.Work.Authorize; +using LINGYUN.Abp.WeChat.Work.Authorize; using LINGYUN.Abp.WeChat.Work.Features; using LINGYUN.Abp.WeChat.Work.Messages; using LINGYUN.Abp.WeChat.Work.Messages.Models; using LINGYUN.Abp.WeChat.Work.Settings; -using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -22,8 +20,8 @@ public class WeChatWorkNotificationPublishProvider : NotificationPublishProvider public override string Name => ProviderName; protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService(); protected ISettingProvider SettingProvider => ServiceProvider.LazyGetRequiredService(); - protected IStringLocalizerFactory LocalizerFactory => ServiceProvider.LazyGetRequiredService(); protected IWeChatWorkMessageSender WeChatWorkMessageSender => ServiceProvider.LazyGetRequiredService(); + protected INotificationDataSerializer NotificationDataSerializer => ServiceProvider.LazyGetRequiredService(); protected IWeChatWorkUserClaimProvider WeChatWorkInternalUserFinder => ServiceProvider.LazyGetRequiredService(); protected INotificationDefinitionManager NotificationDefinitionManager => ServiceProvider.LazyGetRequiredService(); @@ -55,65 +53,63 @@ public class WeChatWorkNotificationPublishProvider : NotificationPublishProvider Logger.LogWarning("Unable to send work weixin messages because agentId is not set."); return; } - - var title = ""; - var message = ""; - var description = ""; + var notificationData = await NotificationDataSerializer.ToStandard(notification.Data); var toTag = notification.Data.GetTagOrNull() ?? notificationDefine?.GetTagOrNull(); - var toParty = notification.Data.GetPartyOrNull() ?? notificationDefine?.GetPartyOrNull(); - - if (!notification.Data.NeedLocalizer()) + if (!toTag.IsNullOrWhiteSpace()) { - title = notification.Data.TryGetData("title").ToString(); - message = notification.Data.TryGetData("message").ToString(); - description = notification.Data.TryGetData("description")?.ToString() ?? ""; + // 指定发送标签 + await PublishToAgentAsync( + agentId, + notification, + notificationData.Title, + notificationData.Message, + notificationData.Description, + toTag: toTag, + cancellationToken: cancellationToken); + return; } - else + var toParty = notification.Data.GetPartyOrNull() ?? notificationDefine?.GetPartyOrNull(); + if (!toParty.IsNullOrWhiteSpace()) { - var titleInfo = notification.Data.TryGetData("title").As(); - var titleLocalizer = await LocalizerFactory.CreateByResourceNameAsync(titleInfo.ResourceName); - title = titleLocalizer[titleInfo.Name, titleInfo.Values].Value; - - var messageInfo = notification.Data.TryGetData("message").As(); - var messageLocalizer = await LocalizerFactory.CreateByResourceNameAsync(messageInfo.ResourceName); - message = messageLocalizer[messageInfo.Name, messageInfo.Values].Value; - - var descriptionInfo = notification.Data.TryGetData("description")?.As(); - if (descriptionInfo != null) - { - var descriptionLocalizer = await LocalizerFactory.CreateByResourceNameAsync(descriptionInfo.ResourceName); - description = descriptionLocalizer[descriptionInfo.Name, descriptionInfo.Values].Value; - } + // 指定发送部门 + await PublishToAgentAsync( + agentId, + notification, + notificationData.Title, + notificationData.Message, + notificationData.Description, + toParty: toParty, + cancellationToken: cancellationToken); + return; } var findUserList = await WeChatWorkInternalUserFinder .FindUserIdentifierListAsync(identifiers.Select(id => id.UserId)); - if (!findUserList.Any()) + if (findUserList.Count == 0) { Logger.LogWarning("Unable to send work weixin messages because findUserList is empty."); return; } + // 发送到个人 await PublishToAgentAsync( agentId, notification, - findUserList.JoinAsString("|"), - title, - message, - description, - toParty, - toTag, - cancellationToken); + notificationData.Title, + notificationData.Message, + notificationData.Description, + toUser: findUserList.JoinAsString("|"), + cancellationToken: cancellationToken); } protected async virtual Task PublishToAgentAsync( string agentId, NotificationInfo notification, - string toUser, string title, string content, string description = "", + string toUser = null, string toParty = null, string toTag = null, CancellationToken cancellationToken = default) @@ -141,6 +137,13 @@ public class WeChatWorkNotificationPublishProvider : NotificationPublishProvider return; } + if (toUser.IsNullOrWhiteSpace() && toTag.IsNullOrWhiteSpace() && toParty.IsNullOrWhiteSpace()) + { + // touser、toparty、totag不能同时为空:https://developer.work.weixin.qq.com/document/path/90236 + Logger.LogWarning("Unable to send work weixin messages because The recipient/department/label cannot be empty simultaneously."); + return; + } + message.ToUser = toUser; message.ToTag = toTag; message.ToParty = toParty; diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WxPusher/LINGYUN/Abp/Notifications/WxPusher/WxPusherNotificationPublishProvider.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WxPusher/LINGYUN/Abp/Notifications/WxPusher/WxPusherNotificationPublishProvider.cs index 04197e210..5cddd3712 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WxPusher/LINGYUN/Abp/Notifications/WxPusher/WxPusherNotificationPublishProvider.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WxPusher/LINGYUN/Abp/Notifications/WxPusher/WxPusherNotificationPublishProvider.cs @@ -1,53 +1,24 @@ -using LINGYUN.Abp.RealTime.Localization; -using LINGYUN.Abp.WxPusher.Features; +using LINGYUN.Abp.WxPusher.Features; using LINGYUN.Abp.WxPusher.Messages; using LINGYUN.Abp.WxPusher.User; -using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Features; -using Volo.Abp.Localization; namespace LINGYUN.Abp.Notifications.WxPusher; public class WxPusherNotificationPublishProvider : NotificationPublishProvider { public const string ProviderName = "WxPusher"; - public override string Name => ProviderName; - - protected IFeatureChecker FeatureChecker { get; } - - protected IWxPusherUserStore WxPusherUserStore { get; } - - protected IWxPusherMessageSender WxPusherMessageSender { get; } - - protected IStringLocalizerFactory LocalizerFactory { get; } - - protected AbpLocalizationOptions LocalizationOptions { get; } - - protected INotificationDefinitionManager NotificationDefinitionManager { get; } - - public WxPusherNotificationPublishProvider( - IFeatureChecker featureChecker, - IWxPusherUserStore wxPusherUserStore, - IWxPusherMessageSender wxPusherMessageSender, - IStringLocalizerFactory localizerFactory, - IOptions localizationOptions, - INotificationDefinitionManager notificationDefinitionManager) - { - FeatureChecker = featureChecker; - WxPusherUserStore = wxPusherUserStore; - WxPusherMessageSender = wxPusherMessageSender; - LocalizerFactory = localizerFactory; - LocalizationOptions = localizationOptions.Value; - NotificationDefinitionManager = notificationDefinitionManager; - } + protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService(); + protected IWxPusherUserStore WxPusherUserStore => ServiceProvider.LazyGetRequiredService(); + protected IWxPusherMessageSender WxPusherMessageSender => ServiceProvider.LazyGetRequiredService(); + protected INotificationDataSerializer NotificationDataSerializer => ServiceProvider.LazyGetRequiredService(); + protected INotificationDefinitionManager NotificationDefinitionManager => ServiceProvider.LazyGetRequiredService(); protected async override Task CanPublishAsync(NotificationInfo notification, CancellationToken cancellationToken = default) { @@ -83,47 +54,15 @@ public class WxPusherNotificationPublishProvider : NotificationPublishProvider } var contentType = notificationDefine?.GetContentTypeOrDefault(MessageContentType.Text) ?? MessageContentType.Text; - - if (!notification.Data.NeedLocalizer()) - { - var title = notification.Data.TryGetData("title").ToString(); - var message = notification.Data.TryGetData("message").ToString(); - - await WxPusherMessageSender.SendAsync( - content: message, - summary: title, - contentType: contentType, - topicIds: topics, - uids: uids, - url: url, - cancellationToken: cancellationToken); - } - else - { - var titleInfo = notification.Data.TryGetData("title").As(); - var titleResource = GetResource(titleInfo.ResourceName); - var titleLocalizer = await LocalizerFactory.CreateByResourceNameAsync(titleResource.ResourceName); - var title = titleLocalizer[titleInfo.Name, titleInfo.Values].Value; - - var messageInfo = notification.Data.TryGetData("message").As(); - var messageResource = GetResource(messageInfo.ResourceName); - var messageLocalizer = await LocalizerFactory.CreateByResourceNameAsync(messageResource.ResourceName); - var message = messageLocalizer[messageInfo.Name, messageInfo.Values].Value; - - await WxPusherMessageSender.SendAsync( - content: message, - summary: title, - contentType: contentType, - topicIds: topics, - uids: uids, - url: url, - cancellationToken: cancellationToken); - } - } - - private LocalizationResourceBase GetResource(string resourceName) - { - return LocalizationOptions.Resources.Values - .First(x => x.ResourceName.Equals(resourceName)); + var notificationData = await NotificationDataSerializer.ToStandard(notification.Data); + + await WxPusherMessageSender.SendAsync( + content: notificationData.Message, + summary: notificationData.Title, + contentType: contentType, + topicIds: topics, + uids: uids, + url: url, + cancellationToken: cancellationToken); } } diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs index 16ce6037f..e55ca46fc 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs @@ -1,5 +1,8 @@ using LINGYUN.Abp.RealTime.Localization; +using Microsoft.Extensions.Localization; using Newtonsoft.Json; +using System; +using System.Threading.Tasks; using Volo.Abp.DependencyInjection; namespace LINGYUN.Abp.Notifications; @@ -7,7 +10,13 @@ namespace LINGYUN.Abp.Notifications; [Dependency(TryRegister = true)] public class DefaultNotificationDataSerializer : INotificationDataSerializer, ISingletonDependency { - public NotificationData Serialize(NotificationData source) + private readonly IStringLocalizerFactory _localizerFactory; + public DefaultNotificationDataSerializer(IStringLocalizerFactory localizerFactory) + { + _localizerFactory = localizerFactory; + } + + public virtual NotificationData Serialize(NotificationData source) { if (source != null) { @@ -44,4 +53,65 @@ public class DefaultNotificationDataSerializer : INotificationDataSerializer, IS } return source; } + + public async virtual Task ToStandard(NotificationData source) + { + var title = ""; + var message = ""; + var description = ""; + + if (!source.NeedLocalizer()) + { + title = source.TryGetData("title").ToString(); + message = source.TryGetData("message").ToString(); + description = source.TryGetData("description")?.ToString() ?? ""; + } + else + { + var titleInfo = source.TryGetData("title").As(); + var titleLocalizer = await _localizerFactory.CreateByResourceNameAsync(titleInfo.ResourceName); + title = titleLocalizer[titleInfo.Name].Value; + if (titleInfo.Values != null) + { + foreach (var formatValue in titleInfo.Values) + { + if (formatValue.Key != null && formatValue.Value != null) + { + title = title.Replace($"{{{formatValue.Key}}}", formatValue.Value.ToString()); + } + } + } + var messageInfo = source.TryGetData("message").As(); + var messageLocalizer = await _localizerFactory.CreateByResourceNameAsync(messageInfo.ResourceName); + message = messageLocalizer[messageInfo.Name].Value; + if (messageInfo.Values != null) + { + foreach (var formatValue in messageInfo.Values) + { + if (formatValue.Key != null && formatValue.Value != null) + { + message = message.Replace($"{{{formatValue.Key}}}", formatValue.Value.ToString()); + } + } + } + var descriptionInfo = source.TryGetData("description")?.As(); + if (descriptionInfo != null) + { + var descriptionLocalizer = await _localizerFactory.CreateByResourceNameAsync(descriptionInfo.ResourceName); + description = descriptionLocalizer[descriptionInfo.Name].Value; + if (descriptionInfo.Values != null) + { + foreach (var formatValue in descriptionInfo.Values) + { + if (formatValue.Key != null && formatValue.Value != null) + { + description = description.Replace($"{{{formatValue.Key}}}", formatValue.Value.ToString()); + } + } + } + } + } + + return new NotificationStandardData(title, message, description); + } } diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs index 7d2cef88b..08294c878 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs @@ -1,5 +1,8 @@ -namespace LINGYUN.Abp.Notifications; +using System.Threading.Tasks; + +namespace LINGYUN.Abp.Notifications; public interface INotificationDataSerializer { NotificationData Serialize(NotificationData source); + Task ToStandard(NotificationData source); }