diff --git a/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatureDefinitionProvider.cs b/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatureDefinitionProvider.cs deleted file mode 100644 index 6c89e59c1..000000000 --- a/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatureDefinitionProvider.cs +++ /dev/null @@ -1,54 +0,0 @@ -using LINGYUN.Abp.WeChat.Features; -using LINGYUN.Abp.WeChat.Localization; -using Volo.Abp.Features; -using Volo.Abp.Localization; -using Volo.Abp.Validation.StringValues; - -namespace LINGYUN.Abp.Notifications.WeChat.MiniProgram.Features -{ - public class WeChatMiniProgramFeatureDefinitionProvider : FeatureDefinitionProvider - { - public override void Define(IFeatureDefinitionContext context) - { - var wechatGroup = context.GetGroupOrNull(WeChatFeatures.GroupName); - if (wechatGroup != null) - { - var weappFeature = wechatGroup - .AddFeature( - WeChatMiniProgramFeatures.GroupName, - true.ToString(), - L("Features:MiniProgram"), - L("Features:MiniProgramDescription"), - new ToggleStringValueType(new BooleanValueValidator())); - - - var weappNofitication = weappFeature - .CreateChild( - WeChatMiniProgramFeatures.Notifications.Default, - true.ToString(), - L("Features:Notifications"), - L("Features:Notifications"), - new ToggleStringValueType(new BooleanValueValidator())); - weappNofitication - .CreateChild( - WeChatMiniProgramFeatures.Notifications.PublishLimit, - WeChatMiniProgramFeatures.Notifications.DefaultPublishLimit.ToString(), - L("Features:PublishLimit"), - L("Features:PublishLimitDescription"), - new ToggleStringValueType(new NumericValueValidator(0, 100000))); - weappNofitication - .CreateChild( - WeChatMiniProgramFeatures.Notifications.PublishLimitInterval, - WeChatMiniProgramFeatures.Notifications.DefaultPublishLimitInterval.ToString(), - L("Features:PublishLimitInterval"), - L("Features:PublishLimitIntervalDescription"), - new ToggleStringValueType(new NumericValueValidator(1, 12))); - } - } - - protected LocalizableString L(string name) - { - return LocalizableString.Create(name); - } - } -} diff --git a/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatures.cs b/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatures.cs deleted file mode 100644 index eb972be84..000000000 --- a/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatures.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LINGYUN.Abp.WeChat.Features; - -namespace LINGYUN.Abp.Notifications.WeChat.MiniProgram.Features -{ - public static class WeChatMiniProgramFeatures - { - public const string GroupName = WeChatFeatures.GroupName + ".MiniProgram"; - - public static class Notifications - { - public const string Default = GroupName + ".Notifications"; - /// - /// 发布次数上限 - /// - public const string PublishLimit = Default + ".PublishLimit"; - /// - /// 发布次数上限时长 - /// - public const string PublishLimitInterval = Default + ".PublishLimitInterval"; - /// - /// 默认发布次数上限 - /// - public const int DefaultPublishLimit = 1000; - /// - /// 默认发布次数上限时长 - /// - public const int DefaultPublishLimitInterval = 1; - } - } -} diff --git a/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs b/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs index 5aeb80716..de6562aae 100644 --- a/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs +++ b/aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs @@ -1,120 +1,106 @@ -using LINGYUN.Abp.Notifications.WeChat.MiniProgram.Features; -using LINGYUN.Abp.WeChat.MiniProgram.Messages; -using LINGYUN.Abp.WeChat.Security.Claims; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using Volo.Abp.Features; - -namespace LINGYUN.Abp.Notifications.WeChat.MiniProgram -{ - /// - /// 微信小程序消息推送提供者 - /// - public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishProvider - { - public const string ProviderName = "WeChat.MiniProgram"; - public override string Name => ProviderName; - - private IFeatureChecker _featureChecker; - protected IFeatureChecker FeatureChecker => LazyGetRequiredService(ref _featureChecker); - protected ISubscribeMessager SubscribeMessager { get; } - protected AbpNotificationsWeChatMiniProgramOptions Options { get; } - public WeChatMiniProgramNotificationPublishProvider( - IServiceProvider serviceProvider, - ISubscribeMessager subscribeMessager, - IOptions options) - : base(serviceProvider) - { - Options = options.Value; - SubscribeMessager = subscribeMessager; - } - - protected override async Task PublishAsync(NotificationInfo notification, IEnumerable identifiers, CancellationToken cancellationToken = default) - { - - // 先检测微信小程序的功能限制 - var publishEnabled = await FeatureChecker.GetAsync(WeChatMiniProgramFeatures.Notifications.Default, false); - - if (!publishEnabled) - { - return; - } - - // step1 默认微信openid绑定的就是username, - // 如果不是,需要自行处理openid获取逻辑 - - // step2 调用微信消息推送接口 - - // 微信不支持推送到所有用户 - // 在小程序里用户订阅消息后通过 api/subscribes/subscribe 接口订阅对应模板消息 - foreach (var identifier in identifiers) - { - await SendWeChatTemplateMessagAsync(notification, identifier, cancellationToken); - } - } - - protected virtual async Task SendWeChatTemplateMessagAsync(NotificationInfo notification, UserIdentifier identifier, CancellationToken cancellationToken = default) - { - var templateId = GetOrDefaultTemplateId(notification.Data); - if (templateId.IsNullOrWhiteSpace()) - { - Logger.LogWarning("Wechat weapp template id be empty, can not send notification!"); - return; - } - - Logger.LogDebug($"Get wechat weapp template id: {templateId}"); - - var redirect = GetOrDefault(notification.Data, "RedirectPage", null); - Logger.LogDebug($"Get wechat weapp redirect page: {redirect ?? "null"}"); - - var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.DefaultState); - Logger.LogDebug($"Get wechat weapp state: {weAppState ?? null}"); - - var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.DefaultLanguage); - Logger.LogDebug($"Get wechat weapp language: {weAppLang ?? null}"); - - // TODO: 如果微信端发布通知,请组装好 openid 字段在通知数据内容里面 - string openId = GetOrDefault(notification.Data, AbpWeChatClaimTypes.OpenId, ""); - - if (openId.IsNullOrWhiteSpace()) - { - // 发送小程序订阅消息 - await SubscribeMessager - .SendAsync( - identifier.UserId, templateId, redirect, weAppLang, - weAppState, notification.Data.Properties, cancellationToken); - } - else - { - var weChatWeAppNotificationData = new SubscribeMessage(templateId, redirect, weAppState, weAppLang); - // 写入模板数据 - weChatWeAppNotificationData.WriteData(notification.Data.Properties); - - Logger.LogDebug($"Sending wechat weapp notification: {notification.Name}"); - - // 发送小程序订阅消息 - await SubscribeMessager.SendAsync(weChatWeAppNotificationData, cancellationToken); - } - } - - protected string GetOrDefaultTemplateId(NotificationData data) - { - return GetOrDefault(data, "TemplateId", Options.DefaultTemplateId); - } - - protected string GetOrDefault(NotificationData data, string key, string defaultValue) - { - if (data.Properties.TryGetValue(key, out object value)) - { - // 取得了数据就删除对应键值 - // data.Properties.Remove(key); - return value.ToString(); - } - return defaultValue; - } - } -} +using LINGYUN.Abp.WeChat.MiniProgram.Messages; +using LINGYUN.Abp.WeChat.Security.Claims; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace LINGYUN.Abp.Notifications.WeChat.MiniProgram +{ + /// + /// 微信小程序消息推送提供者 + /// + public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishProvider + { + public const string ProviderName = "WeChat.MiniProgram"; + public override string Name => ProviderName; + protected ISubscribeMessager SubscribeMessager { get; } + protected AbpNotificationsWeChatMiniProgramOptions Options { get; } + public WeChatMiniProgramNotificationPublishProvider( + IServiceProvider serviceProvider, + ISubscribeMessager subscribeMessager, + IOptions options) + : base(serviceProvider) + { + Options = options.Value; + SubscribeMessager = subscribeMessager; + } + + protected override async Task PublishAsync(NotificationInfo notification, IEnumerable identifiers, CancellationToken cancellationToken = default) + { + // step1 默认微信openid绑定的就是username, + // 如果不是,需要自行处理openid获取逻辑 + + // step2 调用微信消息推送接口 + + // 微信不支持推送到所有用户 + // 在小程序里用户订阅消息后通过 api/subscribes/subscribe 接口订阅对应模板消息 + foreach (var identifier in identifiers) + { + await SendWeChatTemplateMessagAsync(notification, identifier, cancellationToken); + } + } + + protected virtual async Task SendWeChatTemplateMessagAsync(NotificationInfo notification, UserIdentifier identifier, CancellationToken cancellationToken = default) + { + var templateId = GetOrDefaultTemplateId(notification.Data); + if (templateId.IsNullOrWhiteSpace()) + { + Logger.LogWarning("Wechat weapp template id be empty, can not send notification!"); + return; + } + + Logger.LogDebug($"Get wechat weapp template id: {templateId}"); + + var redirect = GetOrDefault(notification.Data, "RedirectPage", null); + Logger.LogDebug($"Get wechat weapp redirect page: {redirect ?? "null"}"); + + var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.DefaultState); + Logger.LogDebug($"Get wechat weapp state: {weAppState ?? null}"); + + var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.DefaultLanguage); + Logger.LogDebug($"Get wechat weapp language: {weAppLang ?? null}"); + + // TODO: 如果微信端发布通知,请组装好 openid 字段在通知数据内容里面 + string openId = GetOrDefault(notification.Data, AbpWeChatClaimTypes.OpenId, ""); + + if (openId.IsNullOrWhiteSpace()) + { + // 发送小程序订阅消息 + await SubscribeMessager + .SendAsync( + identifier.UserId, templateId, redirect, weAppLang, + weAppState, notification.Data.Properties, cancellationToken); + } + else + { + var weChatWeAppNotificationData = new SubscribeMessage(templateId, redirect, weAppState, weAppLang); + // 写入模板数据 + weChatWeAppNotificationData.WriteData(notification.Data.Properties); + + Logger.LogDebug($"Sending wechat weapp notification: {notification.Name}"); + + // 发送小程序订阅消息 + await SubscribeMessager.SendAsync(weChatWeAppNotificationData, cancellationToken); + } + } + + protected string GetOrDefaultTemplateId(NotificationData data) + { + return GetOrDefault(data, "TemplateId", Options.DefaultTemplateId); + } + + protected string GetOrDefault(NotificationData data, string key, string defaultValue) + { + if (data.Properties.TryGetValue(key, out object value)) + { + // 取得了数据就删除对应键值 + // data.Properties.Remove(key); + return value.ToString(); + } + return defaultValue; + } + } +}