Skip to content

Commit

Permalink
Merge pull request #93 from EasyAbp/fix-wechat-event-handling
Browse files Browse the repository at this point in the history
Fix the WeChat event handling
  • Loading branch information
gdlcf88 committed Aug 5, 2023
2 parents b615bbe + df33df7 commit ce08db0
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 41 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>3.0.0-preview.4</Version>
<Version>3.0.0-preview.5</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
22 changes: 21 additions & 1 deletion docs/WeChatOfficial.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,27 @@ var result = await customMenuService.DeleteCustomMenuAsync();
* `/wechat/redirect-url/app-id/{appId}`
* `/wechat/redirect-url/tenant-id/{tenantId}/app-id/{appId}`

## 四、如何实现其他未支持接口
## 四、微信服务器事件处理

本模块提供了 HTTP API 接口接收微信服务器事件推送,如果您需要处理,请实现处理器类:

```csharp
public class MyWeChatOfficialAppEventHandler :
WeChatOfficialAppEventHandlerBase<MyWeChatOfficialAppEventHandler>,
ITransientDependency
{
public override string MsgType => "event"; // 对应微信文档的msgType值
public override int Priority => 0; // Priority大的处理器优先执行
public override async Task<AppEventHandlingResult> HandleAsync(string appId, WeChatAppEventModel model)
{
// 在这里处理您的业务
return new AppEventHandlingResult(true); // 处理后返回成功的结果
}
}
```

## 五、如何实现其他未支持接口

目前本仓库主要由 [real-zony](https://github.com/real-zony) 进行维护,部分不支持的接口可能一时半会儿无法实现。你可以参考源码,继承 `OfficialAbpWeChatServiceBase` 调用基类的 `WeChatOfficialApiRequester` 对象发起 API 请求。

Expand Down
22 changes: 22 additions & 0 deletions docs/WeChatOpenPlatform.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,25 @@ using (currentWeChatThirdPartyPlatform.Change(componentAppId))
* `/wechat/third-party-platform/notify/app/tenant-id/{tenantId}/app-id/{appId}`
* `/wechat/third-party-platform/notify/app/component-app-id/{componentAppId}/app-id/{appId}`
* `/wechat/third-party-platform/notify/app/tenant-id/{tenantId}/component-app-id/{componentAppId}/app-id/{appId}`

## 四、微信服务器事件处理

本模块提供了 HTTP API 接口接收微信服务器事件推送,如果您需要处理,请实现处理器类:

```csharp
// 第三方平台代接收的微信应用事件
public class MyWeChatThirdPartyPlatformAppEventHandler :
WeChatThirdPartyPlatformAppEventHandlerBase<MyWeChatThirdPartyPlatformAppEventHandler>,
ITransientDependency
{
public override string MsgType => "event"; // 对应微信文档的msgType值
public override int Priority => 0; // Priority大的处理器优先执行
public override async Task<AppEventHandlingResult> HandleAsync(
string componentAppId, string authorizerAppId, WeChatAppEventModel model)
{
// 在这里处理您的业务
return new AppEventHandlingResult(true); // 处理后返回成功的结果
}
}
```
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.Models;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;
Expand All @@ -13,12 +14,17 @@ public interface IWeChatOfficialAppEventHandler
/// <summary>
/// 仅处理回调请求中,相应的 MsgType 值的事件
/// </summary>
public string MsgType { get; }
string MsgType { get; }

/// <summary>
/// Handler 执行的优先级,值更大的先执行
/// </summary>
public int Priority { get; }
int Priority { get; }

/// <summary>
/// 实现 IWeChatOfficialAppEventHandler 的类型,用于实例化
/// </summary>
Type HandlerType { get; }

/// <summary>
/// 事件处理实现。
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.Models;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;

namespace EasyAbp.Abp.WeChat.Official.RequestHandling;

public abstract class WeChatOfficialAppEventHandlerBase<THandler> : IWeChatOfficialAppEventHandler
where THandler : IWeChatOfficialAppEventHandler
{
public abstract string MsgType { get; }

public abstract int Priority { get; }

public Type HandlerType => typeof(THandler);

public abstract Task<AppEventHandlingResult> HandleAsync(string appId, WeChatAppEventModel model);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public virtual Task<List<IWeChatOfficialAppEventHandler>> GetAppEventHandlersAsy
return ResolveAppEventHandlersAsync(msgType);
}

protected virtual async Task<List<IWeChatOfficialAppEventHandler>> ResolveAppEventHandlersAsync(
string msgType)
protected virtual async Task<List<IWeChatOfficialAppEventHandler>> ResolveAppEventHandlersAsync(string msgType)
{
if (AppEventHandlerCachedTypes is null)
{
Expand All @@ -37,7 +36,7 @@ public virtual Task<List<IWeChatOfficialAppEventHandler>> GetAppEventHandlersAsy
var objs = ServiceProvider.GetServices<IWeChatOfficialAppEventHandler>().ToArray();

var cacheTypes = objs.GroupBy(obj => obj.MsgType)
.ToDictionary(x => x.Key, x => x.Select(y => y.GetType()).ToList());
.ToDictionary(x => x.Key, x => x.Select(y => y.HandlerType).ToList());

AppEventHandlerCachedTypes = cacheTypes;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.Models;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;
Expand All @@ -13,12 +14,17 @@ public interface IWeChatThirdPartyPlatformAppEventHandler
/// <summary>
/// 仅处理回调请求中,相应的 MsgType 值的事件
/// </summary>
public string MsgType { get; }
string MsgType { get; }

/// <summary>
/// Handler 执行的优先级,值更大的先执行
/// </summary>
public int Priority { get; }
int Priority { get; }

/// <summary>
/// 实现 IWeChatThirdPartyPlatformAppEventHandler 的类型,用于实例化
/// </summary>
Type HandlerType { get; }

/// <summary>
/// 事件处理实现。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.RequestHandling;
using EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.Models;
Expand All @@ -12,7 +13,12 @@ public interface IWeChatThirdPartyPlatformAuthEventHandler
/// <summary>
/// 仅处理此 InfoType 的事件,有效值参考 <see cref="WeChatThirdPartyPlatformAuthEventInfoTypes"/>
/// </summary>
public string InfoType { get; }
string InfoType { get; }

/// <summary>
/// 实现 IWeChatThirdPartyPlatformAuthEventHandler 的类型,用于实例化
/// </summary>
Type HandlerType { get; }

Task<WeChatRequestHandlingResult> HandleAsync(AuthEventModel model);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.RequestHandling;

public class ReleaseTestWeChatThirdPartyPlatformAppEventHandler : IWeChatThirdPartyPlatformAppEventHandler,
public class ReleaseTestWeChatThirdPartyPlatformAppEventHandler :
WeChatThirdPartyPlatformAppEventHandlerBase<ReleaseTestWeChatThirdPartyPlatformAppEventHandler>,
ITransientDependency
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<ReleaseTestWeChatThirdPartyPlatformAppEventHandler> _logger;

public virtual string MsgType => "text";
public int Priority => -10000;
public override string MsgType => "text";
public override int Priority => -10000;

public ReleaseTestWeChatThirdPartyPlatformAppEventHandler(
IServiceProvider serviceProvider,
Expand All @@ -35,7 +36,7 @@ public class ReleaseTestWeChatThirdPartyPlatformAppEventHandler : IWeChatThirdPa
_logger = logger;
}

public virtual async Task<AppEventHandlingResult> HandleAsync(string componentAppId, string authorizerAppId,
public override async Task<AppEventHandlingResult> HandleAsync(string componentAppId, string authorizerAppId,
WeChatAppEventModel model)
{
if (ReleaseTestConsts.OfficialAppIds.Contains(authorizerAppId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.RequestHandling;

public class TicketRecordingWeChatThirdPartyPlatformAuthEventHandler :
IWeChatThirdPartyPlatformAuthEventHandler, ITransientDependency
WeChatThirdPartyPlatformAuthEventHandlerBase<TicketRecordingWeChatThirdPartyPlatformAuthEventHandler>,
ITransientDependency
{
public string InfoType => WeChatThirdPartyPlatformAuthEventInfoTypes.ComponentVerifyTicket;
public override string InfoType => WeChatThirdPartyPlatformAuthEventInfoTypes.ComponentVerifyTicket;

private readonly ILogger<TicketRecordingWeChatThirdPartyPlatformAuthEventHandler> _logger;
private readonly IComponentVerifyTicketStore _componentVerifyTicketStore;
Expand All @@ -24,7 +25,7 @@ public class TicketRecordingWeChatThirdPartyPlatformAuthEventHandler :
_componentVerifyTicketStore = componentVerifyTicketStore;
}

public virtual async Task<WeChatRequestHandlingResult> HandleAsync(AuthEventModel model)
public override async Task<WeChatRequestHandlingResult> HandleAsync(AuthEventModel model)
{
if (model.ComponentVerifyTicket.IsNullOrWhiteSpace())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.Models;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;

namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.RequestHandling;

public abstract class WeChatThirdPartyPlatformAppEventHandlerBase<THandler> : IWeChatThirdPartyPlatformAppEventHandler
where THandler : IWeChatThirdPartyPlatformAppEventHandler
{
public abstract string MsgType { get; }

public abstract int Priority { get; }

public Type HandlerType => typeof(THandler);

public abstract Task<AppEventHandlingResult> HandleAsync(string componentAppId, string authorizerAppId,
WeChatAppEventModel model);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.Models;
using EasyAbp.Abp.WeChat.Common.RequestHandling;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;
using EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.Models;

namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.RequestHandling;

public abstract class WeChatThirdPartyPlatformAuthEventHandlerBase<THandler> : IWeChatThirdPartyPlatformAuthEventHandler
where THandler : IWeChatThirdPartyPlatformAuthEventHandler
{
public abstract string InfoType { get; }

public Type HandlerType => typeof(THandler);

public abstract Task<WeChatRequestHandlingResult> HandleAsync(AuthEventModel model);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public virtual Task<List<IWeChatThirdPartyPlatformAppEventHandler>> GetAppEventH
var objs = ServiceProvider.GetServices<IWeChatThirdPartyPlatformAuthEventHandler>().ToArray();

var cacheTypes = objs.GroupBy(obj => obj.InfoType)
.ToDictionary(x => x.Key, x => x.Select(y => y.GetType()).ToList());
.ToDictionary(x => x.Key, x => x.Select(y => y.HandlerType).ToList());

AuthEventHandlerCachedTypes = cacheTypes;

Expand All @@ -72,7 +72,7 @@ public virtual Task<List<IWeChatThirdPartyPlatformAppEventHandler>> GetAppEventH
var objs = ServiceProvider.GetServices<IWeChatThirdPartyPlatformAppEventHandler>().ToArray();

var cacheTypes = objs.GroupBy(obj => obj.MsgType)
.ToDictionary(x => x.Key, x => x.Select(y => y.GetType()).ToList());
.ToDictionary(x => x.Key, x => x.Select(y => y.HandlerType).ToList());

AppEventHandlerCachedTypes = cacheTypes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

namespace EasyAbp.Abp.WeChat.Official.Tests.EventHandling.Fakes;

public class FakeEventWeChatOfficialAppEventHandler : IWeChatOfficialAppEventHandler, ITransientDependency
public class FakeEventWeChatOfficialAppEventHandler :
WeChatOfficialAppEventHandlerBase<FakeEventWeChatOfficialAppEventHandler>,
ITransientDependency
{
public string MsgType => "event";
public int Priority => 0;
public override string MsgType => "event";
public override int Priority => 0;

public Task<AppEventHandlingResult> HandleAsync(string appId, WeChatAppEventModel model)
public override Task<AppEventHandlingResult> HandleAsync(string appId, WeChatAppEventModel model)
{
return Task.FromResult(new AppEventHandlingResult(true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

namespace EasyAbp.Abp.WeChat.Official.Tests.EventHandling.Fakes;

public class FakeTextWeChatOfficialAppEventHandler : IWeChatOfficialAppEventHandler,
public class FakeTextWeChatOfficialAppEventHandler :
WeChatOfficialAppEventHandlerBase<FakeTextWeChatOfficialAppEventHandler>,
ITransientDependency
{
public string MsgType => "text";
public int Priority => 0;
public override string MsgType => "text";
public override int Priority => 0;

public Task<AppEventHandlingResult> HandleAsync(string aAppId, WeChatAppEventModel model)
public override Task<AppEventHandlingResult> HandleAsync(string aAppId, WeChatAppEventModel model)
{
return Task.FromResult(new AppEventHandlingResult(true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace EasyAbp.Abp.WeChat.Official.Tests.EventHandling;

public class ThirdPartyPlatformEventHandlerResolverTests : AbpWeChatOfficialTestBase
public class OfficialAppEventHandlerResolverTests : AbpWeChatOfficialTestBase
{
[Fact]
public async Task Should_Resolve_Handlers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

namespace EasyAbp.Abp.WeChat.OpenPlatform.Tests.ThirdPartyPlatform.Fakes;

public class FakeEventWeChatThirdPartyPlatformAppEventHandler : IWeChatThirdPartyPlatformAppEventHandler,
public class FakeEventWeChatThirdPartyPlatformAppEventHandler :
WeChatThirdPartyPlatformAppEventHandlerBase<FakeEventWeChatThirdPartyPlatformAppEventHandler>,
ITransientDependency
{
public string MsgType => "event";
public int Priority => 0;
public override string MsgType => "event";
public override int Priority => 0;

public Task<AppEventHandlingResult> HandleAsync(
string componentAppId, string authorizerAppId, WeChatAppEventModel model)
public override Task<AppEventHandlingResult> HandleAsync(string componentAppId, string authorizerAppId,
WeChatAppEventModel model)
{
return Task.FromResult(new AppEventHandlingResult(true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

namespace EasyAbp.Abp.WeChat.OpenPlatform.Tests.ThirdPartyPlatform.Fakes;

public class FakeTextWeChatThirdPartyPlatformAppEventHandler : IWeChatThirdPartyPlatformAppEventHandler,
public class FakeTextWeChatThirdPartyPlatformAppEventHandler :
WeChatThirdPartyPlatformAppEventHandlerBase<FakeTextWeChatThirdPartyPlatformAppEventHandler>,
ITransientDependency
{
public string MsgType => "text";
public int Priority => 0;
public override string MsgType => "text";
public override int Priority => 0;

public Task<AppEventHandlingResult> HandleAsync(
string componentAppId, string authorizerAppId, WeChatAppEventModel model)
public override Task<AppEventHandlingResult> HandleAsync(string componentAppId, string authorizerAppId,
WeChatAppEventModel model)
{
return Task.FromResult(new AppEventHandlingResult(true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

namespace EasyAbp.Abp.WeChat.OpenPlatform.Tests.ThirdPartyPlatform.Fakes;

public class FakeUnauthorizedWeChatThirdPartyPlatformAuthEventHandler : IWeChatThirdPartyPlatformAuthEventHandler,
public class FakeUnauthorizedWeChatThirdPartyPlatformAuthEventHandler :
WeChatThirdPartyPlatformAuthEventHandlerBase<FakeUnauthorizedWeChatThirdPartyPlatformAuthEventHandler>,
ITransientDependency
{
public string InfoType => WeChatThirdPartyPlatformAuthEventInfoTypes.Unauthorized;
public override string InfoType => WeChatThirdPartyPlatformAuthEventInfoTypes.Unauthorized;

public Task<WeChatRequestHandlingResult> HandleAsync(AuthEventModel model)
public override Task<WeChatRequestHandlingResult> HandleAsync(AuthEventModel model)
{
return Task.FromResult(new WeChatRequestHandlingResult(true));
}
Expand Down

0 comments on commit ce08db0

Please sign in to comment.