Skip to content

Commit

Permalink
Make NotifyAsync support the HTTP GET method
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlcf88 committed Jul 31, 2023
1 parent 18c348b commit 4b15d21
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 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.2</Version>
<Version>3.0.0-preview.3</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,21 @@ public virtual async Task<ActionResult> GetJsSdkConfigParametersAsync(
/// <summary>
/// 微信应用事件通知接口,开发人员需要实现 <see cref="IWeChatOfficialAppEventHandler"/> 处理器来处理回调请求。
/// </summary>
[HttpGet]
[HttpPost]
[Route("notify")]
public virtual async Task<ActionResult> NotifyAsync([CanBeNull] string tenantId, [CanBeNull] string appId)
{
using var changeTenant = CurrentTenant.Change(tenantId.IsNullOrWhiteSpace() ? null : Guid.Parse(tenantId!));

var result = await _eventRequestHandlingService.NotifyAsync(await CreateRequestModelAsync(), appId);
var model = await CreateRequestModelAsync();

if (model is null)
{
return BadRequest();
}

var result = await _eventRequestHandlingService.NotifyAsync(model, appId);

if (!result.Success)
{
Expand Down Expand Up @@ -219,6 +227,7 @@ public virtual async Task<ActionResult> NotifyAsync([CanBeNull] string tenantId,
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="NotifyAsync"/>
/// </summary>
[HttpGet]
[HttpPost]
[Route("notify/tenant-id/{tenantId}")]
public virtual Task<ActionResult> Notify2Async([CanBeNull] string tenantId, [NotNull] string appId)
Expand All @@ -230,6 +239,7 @@ public virtual Task<ActionResult> Notify2Async([CanBeNull] string tenantId, [Not
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="NotifyAsync"/>
/// </summary>
[HttpGet]
[HttpPost]
[Route("notify/app-id/{appId}")]
public virtual Task<ActionResult> Notify3Async([CanBeNull] string tenantId, [NotNull] string appId)
Expand All @@ -241,15 +251,24 @@ public virtual Task<ActionResult> Notify3Async([CanBeNull] string tenantId, [Not
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="NotifyAsync"/>
/// </summary>
[HttpGet]
[HttpPost]
[Route("notify/tenant-id/{tenantId}/app-id/{appId}")]
public virtual Task<ActionResult> Notify4Async([CanBeNull] string tenantId, [NotNull] string appId)
{
return NotifyAsync(tenantId, appId);
}

[ItemCanBeNull]
protected virtual async Task<WeChatOfficialEventRequestModel> CreateRequestModelAsync()
{
var echostr = Request.Query["echostr"].FirstOrDefault();

if (!echostr.IsNullOrWhiteSpace() && Request.Method != "GET")
{
return null;
}

Request.EnableBuffering();

using var streamReader = new StreamReader(Request.Body);
Expand All @@ -258,14 +277,19 @@ protected virtual async Task<WeChatOfficialEventRequestModel> CreateRequestModel

Request.Body.Position = 0;

if (!postData.IsNullOrWhiteSpace() && Request.Method != "POST")
{
return null;
}

return new WeChatOfficialEventRequestModel
{
PostData = postData,
MsgSignature = Request.Query["msg_signature"].FirstOrDefault() ??
Request.Query["signature"].FirstOrDefault(),
Timestamp = Request.Query["timestamp"].FirstOrDefault(),
Nonce = Request.Query["nonce"].FirstOrDefault(),
EchoStr = Request.Query["echostr"].FirstOrDefault()
EchoStr = echostr
};
}
}
Expand Down

0 comments on commit 4b15d21

Please sign in to comment.