Skip to content

Commit

Permalink
Merge pull request #50 from EasyAbp/refactory-3rd-platform-event-hand…
Browse files Browse the repository at this point in the history
…ling-service

Refactor `IWeChatThirdPartyPlatformEventRequestHandlingService`
  • Loading branch information
gdlcf88 committed Dec 19, 2022
2 parents 958ac1a + 10434e7 commit 7451526
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 21 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>2.0.0-rc.3</Version>
<Version>2.0.0-rc.4</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace EasyAbp.Abp.WeChat.Common.RequestHandling;
using System;

namespace EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;

[Serializable]
public class WeChatEventRequestModel
{
public string PostData { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.ComponentModel.DataAnnotations;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;

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

[Serializable]
public class NotifyAppInput
{
public string ComponentAppId { get; set; }

[Required]
public string AuthorizerAppId { get; set; }

[Required]
public WeChatEventRequestModel EventRequest { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.ComponentModel.DataAnnotations;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;

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

[Serializable]
public class NotifyAuthInput
{
public string ComponentAppId { get; set; }

[Required]
public WeChatEventRequestModel EventRequest { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.RequestHandling;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;
using EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling.Dtos;
using JetBrains.Annotations;

namespace EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling;

public interface IWeChatThirdPartyPlatformEventRequestHandlingService
{
Task<WeChatRequestHandlingResult> NotifyAuthAsync([CanBeNull] string componentAppId, WeChatEventRequestModel request);
Task<WeChatRequestHandlingResult> NotifyAuthAsync(NotifyAuthInput input);

Task<WeChatRequestHandlingResult> NotifyAppAsync([CanBeNull] string componentAppId, [NotNull] string authorizerAppId,
WeChatEventRequestModel request);
Task<WeChatRequestHandlingResult> NotifyAppAsync(NotifyAppInput input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common;
using EasyAbp.Abp.WeChat.Common.RequestHandling;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;
using EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling;
using EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling.Dtos;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
Expand All @@ -18,7 +19,8 @@ public class WeChatThirdPartyPlatformController : AbpControllerBase
{
private readonly IWeChatThirdPartyPlatformEventRequestHandlingService _requestHandlingService;

public WeChatThirdPartyPlatformController(IWeChatThirdPartyPlatformEventRequestHandlingService requestHandlingService)
public WeChatThirdPartyPlatformController(
IWeChatThirdPartyPlatformEventRequestHandlingService requestHandlingService)
{
_requestHandlingService = requestHandlingService;
}
Expand All @@ -38,7 +40,11 @@ public WeChatThirdPartyPlatformController(IWeChatThirdPartyPlatformEventRequestH
{
using var changeTenant = CurrentTenant.Change(tenantId.IsNullOrWhiteSpace() ? null : Guid.Parse(tenantId!));

var result = await _requestHandlingService.NotifyAuthAsync(componentAppId, await CreateRequestModelAsync());
var result = await _requestHandlingService.NotifyAuthAsync(new NotifyAuthInput
{
ComponentAppId = componentAppId,
EventRequest = await CreateRequestModelAsync()
});

if (!result.Success)
{
Expand All @@ -61,7 +67,13 @@ public WeChatThirdPartyPlatformController(IWeChatThirdPartyPlatformEventRequestH
{
using var changeTenant = CurrentTenant.Change(tenantId.IsNullOrWhiteSpace() ? null : Guid.Parse(tenantId!));

var result = await _requestHandlingService.NotifyAppAsync(componentAppId, appId, await CreateRequestModelAsync());
var result =
await _requestHandlingService.NotifyAppAsync(new NotifyAppInput
{
ComponentAppId = componentAppId,
AuthorizerAppId = appId,
EventRequest = await CreateRequestModelAsync()
});

if (!result.Success)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using EasyAbp.Abp.WeChat.Common.Infrastructure.Options;
using EasyAbp.Abp.WeChat.Common.Models;
using EasyAbp.Abp.WeChat.Common.RequestHandling;
using EasyAbp.Abp.WeChat.Common.RequestHandling.Dtos;
using EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling;
using EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling.Dtos;
using EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.Models;
using EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.Options;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -37,12 +39,11 @@ public class WeChatThirdPartyPlatformEventRequestHandlingService :
/// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/component_verify_ticket.html
/// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/authorize_event.html#infotype-%E8%AF%B4%E6%98%8E
/// </summary>
public virtual async Task<WeChatRequestHandlingResult> NotifyAuthAsync(string componentAppId,
WeChatEventRequestModel request)
public virtual async Task<WeChatRequestHandlingResult> NotifyAuthAsync(NotifyAuthInput input)
{
var options = await _optionsProvider.GetAsync(componentAppId);
var options = await _optionsProvider.GetAsync(input.ComponentAppId);

var model = await DecryptMsgAsync<AuthEventModel>(options, request);
var model = await DecryptMsgAsync<AuthEventModel>(options, input.EventRequest);

var handlers = _serviceProvider.GetService<IEnumerable<IWeChatThirdPartyPlatformAuthEventHandler>>();

Expand All @@ -63,18 +64,17 @@ public class WeChatThirdPartyPlatformEventRequestHandlingService :
/// 微信应用事件通知接口,开发人员需要实现 <see cref="IWeChatThirdPartyPlatformAppEventHandler"/> 处理器来处理回调请求。
/// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/operation/thirdparty/prepare.html
/// </summary>
public virtual async Task<WeChatRequestHandlingResult> NotifyAppAsync(string componentAppId,
string authorizerAppId, WeChatEventRequestModel request)
public virtual async Task<WeChatRequestHandlingResult> NotifyAppAsync(NotifyAppInput input)
{
var options = await _optionsProvider.GetAsync(componentAppId);
var options = await _optionsProvider.GetAsync(input.ComponentAppId);

var model = await DecryptMsgAsync<WeChatAppEventModel>(options, request);
var model = await DecryptMsgAsync<WeChatAppEventModel>(options, input.EventRequest);

var handlers = _serviceProvider.GetService<IEnumerable<IWeChatThirdPartyPlatformAppEventHandler>>();

foreach (var handler in handlers.Where(x => x.Event == model.Event))
{
var result = await handler.HandleAsync(options.AppId, authorizerAppId, model);
var result = await handler.HandleAsync(options.AppId, input.AuthorizerAppId, model);

if (!result.Success)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;

namespace EasyAbp.Abp.WeChat.Pay.RequestHandling.Dtos;

[Serializable]
public class GetJsSdkWeChatPayParametersInput
{
public string MchId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using EasyAbp.Abp.WeChat.Common.RequestHandling;

namespace EasyAbp.Abp.WeChat.Pay.RequestHandling.Dtos;

[Serializable]
public class GetJsSdkWeChatPayParametersResult : WeChatRequestHandlingResult
{
public string NonceStr { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;

namespace EasyAbp.Abp.WeChat.Pay.RequestHandling.Dtos;

[Serializable]
public class PaidNotifyInput
{
public string MchId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;

namespace EasyAbp.Abp.WeChat.Pay.RequestHandling.Dtos;

[Serializable]
public class RefundNotifyInput
{
public string MchId { get; set; }
Expand Down

0 comments on commit 7451526

Please sign in to comment.