Skip to content

Commit

Permalink
Merge pull request #63 from EasyAbp/multi-response-types
Browse files Browse the repository at this point in the history
Support multi-type response
  • Loading branch information
gdlcf88 committed Dec 24, 2022
2 parents cb12487 + 602896d commit 7693d4a
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 12 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.18</Version>
<Version>2.0.0-rc.19</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,4 +1,5 @@
using EasyAbp.Abp.WeChat.Common.RequestHandling;
using JetBrains.Annotations;

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

Expand All @@ -10,15 +11,19 @@ public class AppEventHandlingResult : WeChatRequestHandlingResult
/// 注意1:如果有多个 handler 设置了本值,则 Priority 更小的 handler 生效,即后执行的结果覆盖先执行的结果。
/// 注意2:如果需要返回特定格式(如 XML),目前需要自行转成文本内容后设置到本值。
/// </summary>
public string SpecifiedResponseContent { get; set; }
[CanBeNull]
public IResponseToWeChatModel ResponseToWeChatModel { get; set; }

protected AppEventHandlingResult()
{
}

public AppEventHandlingResult(bool success, string failureReason = null, string specifiedResponseContent = null) :
base(success, failureReason)
public AppEventHandlingResult(bool success, string failureReason = null) : base(success, failureReason)
{
SpecifiedResponseContent = specifiedResponseContent;
}

public AppEventHandlingResult([NotNull] IResponseToWeChatModel responseToWeChatModel) : base(true)
{
ResponseToWeChatModel = responseToWeChatModel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling.Dtos;

public interface IResponseToWeChatModel
{
string Content { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling.Dtos;

public class JsonResponseToWeChatModel : IResponseToWeChatModel
{
public string Content { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling.Dtos;

public class XmlResponseToWeChatModel : IResponseToWeChatModel
{
public string Content { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,20 @@ public class WeChatThirdPartyPlatformController : AbpControllerBase
return BadRequest();
}

return Ok(result.SpecifiedResponseContent ?? "success");
var contentType = result.ResponseToWeChatModel switch
{
JsonResponseToWeChatModel => "application/json",
XmlResponseToWeChatModel => "text/xml",
null => "text/plain",
_ => "text/plain"
};

return new ContentResult
{
ContentType = contentType,
Content = result.ResponseToWeChatModel?.Content ?? "success",
StatusCode = 200
};
}

protected virtual async Task<WeChatEventRequestModel> CreateRequestModelAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public class ReleaseTestWeChatThirdPartyPlatformAppEventHandler : IWeChatThirdPa
var encryptedXml =
await encryptor.EncryptAsync(options.Token, options.EncodingAesKey, authorizerAppId, xml);

return new AppEventHandlingResult(true, null, encryptedXml);
return new AppEventHandlingResult(new XmlResponseToWeChatModel
{
Content = encryptedXml
});
}

if (!content.StartsWith("QUERY_AUTH_CODE:"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ public virtual async Task<AppEventHandlingResult> NotifyAppAsync(NotifyAppInput

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

string specifiedResponseContent = null;
IResponseToWeChatModel responseToWeChatModel = null;

foreach (var handler in (await _thirdPartyPlatformEventHandlerResolver.GetAppEventHandlersAsync(model.MsgType))
.OrderByDescending(x => x.Priority))
{
var result = await handler.HandleAsync(options.AppId, input.AuthorizerAppId, model);

if (result.SpecifiedResponseContent != null)
if (result.ResponseToWeChatModel != null)
{
specifiedResponseContent = result.SpecifiedResponseContent;
responseToWeChatModel = result.ResponseToWeChatModel;
}

if (!result.Success)
Expand All @@ -88,7 +88,9 @@ public virtual async Task<AppEventHandlingResult> NotifyAppAsync(NotifyAppInput
}
}

return new AppEventHandlingResult(true, null, specifiedResponseContent);
return responseToWeChatModel is null
? new AppEventHandlingResult(true)
: new AppEventHandlingResult(responseToWeChatModel);
}

protected virtual async Task<T> DecryptMsgAsync<T>(AbpWeChatThirdPartyPlatformOptions options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.Models;
using EasyAbp.Abp.WeChat.OpenPlatform.RequestHandling.Dtos;
using EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.RequestHandling;
using Shouldly;
using Volo.Abp.Data;
Expand Down Expand Up @@ -29,7 +30,9 @@ public async Task Should_Handle_Release_Test_Official_User_Message()
await handler.HandleAsync(AbpWeChatOpenPlatformTestsConsts.AppId, OfficialAuthorizerAppId, eventModel);

result.Success.ShouldBeTrue();
result.SpecifiedResponseContent.ShouldStartWith("<xml><Encrypt><![CDATA[");
result.ResponseToWeChatModel.ShouldNotBeNull();
result.ResponseToWeChatModel.GetType().ShouldBe(typeof(XmlResponseToWeChatModel));
result.ResponseToWeChatModel.Content.ShouldStartWith("<xml><Encrypt><![CDATA[");
}

[Fact]
Expand Down

0 comments on commit 7693d4a

Please sign in to comment.