Browse Source

fix(wechat): fixed duplicate features definition of wechat mini program

pull/321/head
cKey 4 years ago
parent
commit
4c2eaef2d1
  1. 54
      aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatureDefinitionProvider.cs
  2. 30
      aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatures.cs
  3. 226
      aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs

54
aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatureDefinitionProvider.cs

@ -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<WeChatResource>(name);
}
}
}

30
aspnet-core/modules/wechat/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/Features/WeChatMiniProgramFeatures.cs

@ -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";
/// <summary>
/// 发布次数上限
/// </summary>
public const string PublishLimit = Default + ".PublishLimit";
/// <summary>
/// 发布次数上限时长
/// </summary>
public const string PublishLimitInterval = Default + ".PublishLimitInterval";
/// <summary>
/// 默认发布次数上限
/// </summary>
public const int DefaultPublishLimit = 1000;
/// <summary>
/// 默认发布次数上限时长
/// </summary>
public const int DefaultPublishLimitInterval = 1;
}
}
}

226
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
{
/// <summary>
/// 微信小程序消息推送提供者
/// </summary>
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<AbpNotificationsWeChatMiniProgramOptions> options)
: base(serviceProvider)
{
Options = options.Value;
SubscribeMessager = subscribeMessager;
}
protected override async Task PublishAsync(NotificationInfo notification, IEnumerable<UserIdentifier> 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
{
/// <summary>
/// 微信小程序消息推送提供者
/// </summary>
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<AbpNotificationsWeChatMiniProgramOptions> options)
: base(serviceProvider)
{
Options = options.Value;
SubscribeMessager = subscribeMessager;
}
protected override async Task PublishAsync(NotificationInfo notification, IEnumerable<UserIdentifier> 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;
}
}
}

Loading…
Cancel
Save