feat: 兼容 WMS 公众号回调路径 /apiwechat/verification
支持公众平台配置 oms.nanafa.com/api/apiwechat/verification 的验签与消息推送。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
e4894f9627
commit
26ccc69dc6
@ -20,6 +20,18 @@ public class WeChatMpAppService(
|
|||||||
IHttpContextAccessor httpContextAccessor,
|
IHttpContextAccessor httpContextAccessor,
|
||||||
ILogger<WeChatMpAppService> logger) : ApplicationService
|
ILogger<WeChatMpAppService> logger) : ApplicationService
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 微信服务器接入验签(兼容 WMS 公众号菜单配置:/api/apiwechat/verification)。
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("/apiwechat/verification")]
|
||||||
|
[Produces("text/plain")]
|
||||||
|
public ContentResult ValidateLegacyAsync(
|
||||||
|
[FromQuery] string? signature,
|
||||||
|
[FromQuery] string? timestamp,
|
||||||
|
[FromQuery] string? nonce,
|
||||||
|
[FromQuery] string? echostr)
|
||||||
|
=> ValidateCore(signature, timestamp, nonce, echostr);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 微信服务器接入验签。
|
/// 微信服务器接入验签。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -33,21 +45,24 @@ public class WeChatMpAppService(
|
|||||||
[FromQuery] string? echostr)
|
[FromQuery] string? echostr)
|
||||||
{
|
{
|
||||||
EnsureAppId(appId);
|
EnsureAppId(appId);
|
||||||
if (string.IsNullOrWhiteSpace(signature)
|
return ValidateCore(signature, timestamp, nonce, echostr);
|
||||||
|| string.IsNullOrWhiteSpace(timestamp)
|
|
||||||
|| string.IsNullOrWhiteSpace(nonce)
|
|
||||||
|| string.IsNullOrWhiteSpace(echostr))
|
|
||||||
{
|
|
||||||
return new ContentResult { Content = "error", ContentType = "text/plain; charset=utf-8" };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var valid = WeChatSignatureHelper.Validate(options.Value.Token, timestamp, nonce, signature);
|
/// <summary>
|
||||||
return new ContentResult
|
/// 微信消息/事件回调(兼容 WMS:/api/apiwechat/verification)。
|
||||||
{
|
/// </summary>
|
||||||
Content = valid ? echostr : "error",
|
[HttpPost("/apiwechat/verification")]
|
||||||
ContentType = "text/plain; charset=utf-8"
|
[Consumes("text/xml", "application/xml", "text/plain")]
|
||||||
};
|
[Produces("application/xml")]
|
||||||
}
|
public Task<ContentResult> MessageLegacyAsync(
|
||||||
|
[FromQuery] string signature,
|
||||||
|
[FromQuery] string timestamp,
|
||||||
|
[FromQuery] string nonce,
|
||||||
|
[FromQuery] string? openid,
|
||||||
|
[FromQuery(Name = "encrypt_type")] string? encryptType,
|
||||||
|
[FromQuery(Name = "msg_signature")] string? msgSignature,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
=> MessageCoreAsync(signature, timestamp, nonce, encryptType, cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 微信消息/事件回调。
|
/// 微信消息/事件回调。
|
||||||
@ -66,6 +81,43 @@ public class WeChatMpAppService(
|
|||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
EnsureAppId(appId);
|
EnsureAppId(appId);
|
||||||
|
return await MessageCoreAsync(signature, timestamp, nonce, encryptType, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ContentResult ValidateCore(
|
||||||
|
string? signature,
|
||||||
|
string? timestamp,
|
||||||
|
string? nonce,
|
||||||
|
string? echostr)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(options.Value.Token))
|
||||||
|
{
|
||||||
|
throw new BusinessException("未配置微信公众号 Token");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(signature)
|
||||||
|
|| string.IsNullOrWhiteSpace(timestamp)
|
||||||
|
|| string.IsNullOrWhiteSpace(nonce)
|
||||||
|
|| string.IsNullOrWhiteSpace(echostr))
|
||||||
|
{
|
||||||
|
return new ContentResult { Content = "error", ContentType = "text/plain; charset=utf-8" };
|
||||||
|
}
|
||||||
|
|
||||||
|
var valid = WeChatSignatureHelper.Validate(options.Value.Token, timestamp, nonce, signature);
|
||||||
|
return new ContentResult
|
||||||
|
{
|
||||||
|
Content = valid ? echostr : "error",
|
||||||
|
ContentType = "text/plain; charset=utf-8"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<ContentResult> MessageCoreAsync(
|
||||||
|
string signature,
|
||||||
|
string timestamp,
|
||||||
|
string nonce,
|
||||||
|
string? encryptType,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
if (!WeChatSignatureHelper.Validate(options.Value.Token, timestamp, nonce, signature))
|
if (!WeChatSignatureHelper.Validate(options.Value.Token, timestamp, nonce, signature))
|
||||||
{
|
{
|
||||||
logger.LogWarning("微信消息验签失败");
|
logger.LogWarning("微信消息验签失败");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user