3 changed files with 106 additions and 204 deletions
@ -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); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -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; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,120 +1,106 @@ |
|||||
using LINGYUN.Abp.Notifications.WeChat.MiniProgram.Features; |
using LINGYUN.Abp.WeChat.MiniProgram.Messages; |
||||
using LINGYUN.Abp.WeChat.MiniProgram.Messages; |
using LINGYUN.Abp.WeChat.Security.Claims; |
||||
using LINGYUN.Abp.WeChat.Security.Claims; |
using Microsoft.Extensions.Logging; |
||||
using Microsoft.Extensions.Logging; |
using Microsoft.Extensions.Options; |
||||
using Microsoft.Extensions.Options; |
using System; |
||||
using System; |
using System.Collections.Generic; |
||||
using System.Collections.Generic; |
using System.Threading; |
||||
using System.Threading; |
using System.Threading.Tasks; |
||||
using System.Threading.Tasks; |
|
||||
using Volo.Abp.Features; |
namespace LINGYUN.Abp.Notifications.WeChat.MiniProgram |
||||
|
{ |
||||
namespace LINGYUN.Abp.Notifications.WeChat.MiniProgram |
/// <summary>
|
||||
{ |
/// 微信小程序消息推送提供者
|
||||
/// <summary>
|
/// </summary>
|
||||
/// 微信小程序消息推送提供者
|
public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishProvider |
||||
/// </summary>
|
{ |
||||
public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishProvider |
public const string ProviderName = "WeChat.MiniProgram"; |
||||
{ |
public override string Name => ProviderName; |
||||
public const string ProviderName = "WeChat.MiniProgram"; |
protected ISubscribeMessager SubscribeMessager { get; } |
||||
public override string Name => ProviderName; |
protected AbpNotificationsWeChatMiniProgramOptions Options { get; } |
||||
|
public WeChatMiniProgramNotificationPublishProvider( |
||||
private IFeatureChecker _featureChecker; |
IServiceProvider serviceProvider, |
||||
protected IFeatureChecker FeatureChecker => LazyGetRequiredService(ref _featureChecker); |
ISubscribeMessager subscribeMessager, |
||||
protected ISubscribeMessager SubscribeMessager { get; } |
IOptions<AbpNotificationsWeChatMiniProgramOptions> options) |
||||
protected AbpNotificationsWeChatMiniProgramOptions Options { get; } |
: base(serviceProvider) |
||||
public WeChatMiniProgramNotificationPublishProvider( |
{ |
||||
IServiceProvider serviceProvider, |
Options = options.Value; |
||||
ISubscribeMessager subscribeMessager, |
SubscribeMessager = subscribeMessager; |
||||
IOptions<AbpNotificationsWeChatMiniProgramOptions> options) |
} |
||||
: base(serviceProvider) |
|
||||
{ |
protected override async Task PublishAsync(NotificationInfo notification, IEnumerable<UserIdentifier> identifiers, CancellationToken cancellationToken = default) |
||||
Options = options.Value; |
{ |
||||
SubscribeMessager = subscribeMessager; |
// step1 默认微信openid绑定的就是username,
|
||||
} |
// 如果不是,需要自行处理openid获取逻辑
|
||||
|
|
||||
protected override async Task PublishAsync(NotificationInfo notification, IEnumerable<UserIdentifier> identifiers, CancellationToken cancellationToken = default) |
// step2 调用微信消息推送接口
|
||||
{ |
|
||||
|
// 微信不支持推送到所有用户
|
||||
// 先检测微信小程序的功能限制
|
// 在小程序里用户订阅消息后通过 api/subscribes/subscribe 接口订阅对应模板消息
|
||||
var publishEnabled = await FeatureChecker.GetAsync(WeChatMiniProgramFeatures.Notifications.Default, false); |
foreach (var identifier in identifiers) |
||||
|
{ |
||||
if (!publishEnabled) |
await SendWeChatTemplateMessagAsync(notification, identifier, cancellationToken); |
||||
{ |
} |
||||
return; |
} |
||||
} |
|
||||
|
protected virtual async Task SendWeChatTemplateMessagAsync(NotificationInfo notification, UserIdentifier identifier, CancellationToken cancellationToken = default) |
||||
// step1 默认微信openid绑定的就是username,
|
{ |
||||
// 如果不是,需要自行处理openid获取逻辑
|
var templateId = GetOrDefaultTemplateId(notification.Data); |
||||
|
if (templateId.IsNullOrWhiteSpace()) |
||||
// step2 调用微信消息推送接口
|
{ |
||||
|
Logger.LogWarning("Wechat weapp template id be empty, can not send notification!"); |
||||
// 微信不支持推送到所有用户
|
return; |
||||
// 在小程序里用户订阅消息后通过 api/subscribes/subscribe 接口订阅对应模板消息
|
} |
||||
foreach (var identifier in identifiers) |
|
||||
{ |
Logger.LogDebug($"Get wechat weapp template id: {templateId}"); |
||||
await SendWeChatTemplateMessagAsync(notification, identifier, cancellationToken); |
|
||||
} |
var redirect = GetOrDefault(notification.Data, "RedirectPage", null); |
||||
} |
Logger.LogDebug($"Get wechat weapp redirect page: {redirect ?? "null"}"); |
||||
|
|
||||
protected virtual async Task SendWeChatTemplateMessagAsync(NotificationInfo notification, UserIdentifier identifier, CancellationToken cancellationToken = default) |
var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.DefaultState); |
||||
{ |
Logger.LogDebug($"Get wechat weapp state: {weAppState ?? null}"); |
||||
var templateId = GetOrDefaultTemplateId(notification.Data); |
|
||||
if (templateId.IsNullOrWhiteSpace()) |
var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.DefaultLanguage); |
||||
{ |
Logger.LogDebug($"Get wechat weapp language: {weAppLang ?? null}"); |
||||
Logger.LogWarning("Wechat weapp template id be empty, can not send notification!"); |
|
||||
return; |
// TODO: 如果微信端发布通知,请组装好 openid 字段在通知数据内容里面
|
||||
} |
string openId = GetOrDefault(notification.Data, AbpWeChatClaimTypes.OpenId, ""); |
||||
|
|
||||
Logger.LogDebug($"Get wechat weapp template id: {templateId}"); |
if (openId.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
var redirect = GetOrDefault(notification.Data, "RedirectPage", null); |
// 发送小程序订阅消息
|
||||
Logger.LogDebug($"Get wechat weapp redirect page: {redirect ?? "null"}"); |
await SubscribeMessager |
||||
|
.SendAsync( |
||||
var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.DefaultState); |
identifier.UserId, templateId, redirect, weAppLang, |
||||
Logger.LogDebug($"Get wechat weapp state: {weAppState ?? null}"); |
weAppState, notification.Data.Properties, cancellationToken); |
||||
|
} |
||||
var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.DefaultLanguage); |
else |
||||
Logger.LogDebug($"Get wechat weapp language: {weAppLang ?? null}"); |
{ |
||||
|
var weChatWeAppNotificationData = new SubscribeMessage(templateId, redirect, weAppState, weAppLang); |
||||
// TODO: 如果微信端发布通知,请组装好 openid 字段在通知数据内容里面
|
// 写入模板数据
|
||||
string openId = GetOrDefault(notification.Data, AbpWeChatClaimTypes.OpenId, ""); |
weChatWeAppNotificationData.WriteData(notification.Data.Properties); |
||||
|
|
||||
if (openId.IsNullOrWhiteSpace()) |
Logger.LogDebug($"Sending wechat weapp notification: {notification.Name}"); |
||||
{ |
|
||||
// 发送小程序订阅消息
|
// 发送小程序订阅消息
|
||||
await SubscribeMessager |
await SubscribeMessager.SendAsync(weChatWeAppNotificationData, cancellationToken); |
||||
.SendAsync( |
} |
||||
identifier.UserId, templateId, redirect, weAppLang, |
} |
||||
weAppState, notification.Data.Properties, cancellationToken); |
|
||||
} |
protected string GetOrDefaultTemplateId(NotificationData data) |
||||
else |
{ |
||||
{ |
return GetOrDefault(data, "TemplateId", Options.DefaultTemplateId); |
||||
var weChatWeAppNotificationData = new SubscribeMessage(templateId, redirect, weAppState, weAppLang); |
} |
||||
// 写入模板数据
|
|
||||
weChatWeAppNotificationData.WriteData(notification.Data.Properties); |
protected string GetOrDefault(NotificationData data, string key, string defaultValue) |
||||
|
{ |
||||
Logger.LogDebug($"Sending wechat weapp notification: {notification.Name}"); |
if (data.Properties.TryGetValue(key, out object value)) |
||||
|
{ |
||||
// 发送小程序订阅消息
|
// 取得了数据就删除对应键值
|
||||
await SubscribeMessager.SendAsync(weChatWeAppNotificationData, cancellationToken); |
// data.Properties.Remove(key);
|
||||
} |
return value.ToString(); |
||||
} |
} |
||||
|
return defaultValue; |
||||
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…
Reference in new issue