diff --git a/src/SimpleAdmin.Api/WeChat/WeChatMpAppService.cs b/src/SimpleAdmin.Api/WeChat/WeChatMpAppService.cs index 1baa978..7c1d8fc 100644 --- a/src/SimpleAdmin.Api/WeChat/WeChatMpAppService.cs +++ b/src/SimpleAdmin.Api/WeChat/WeChatMpAppService.cs @@ -20,6 +20,18 @@ public class WeChatMpAppService( IHttpContextAccessor httpContextAccessor, ILogger logger) : ApplicationService { + /// + /// 微信服务器接入验签(兼容 WMS 公众号菜单配置:/api/apiwechat/verification)。 + /// + [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); + /// /// 微信服务器接入验签。 /// @@ -33,22 +45,25 @@ public class WeChatMpAppService( [FromQuery] string? echostr) { EnsureAppId(appId); - 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" - }; + return ValidateCore(signature, timestamp, nonce, echostr); } + /// + /// 微信消息/事件回调(兼容 WMS:/api/apiwechat/verification)。 + /// + [HttpPost("/apiwechat/verification")] + [Consumes("text/xml", "application/xml", "text/plain")] + [Produces("application/xml")] + public Task 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); + /// /// 微信消息/事件回调。 /// @@ -66,6 +81,43 @@ public class WeChatMpAppService( CancellationToken cancellationToken = default) { 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 MessageCoreAsync( + string signature, + string timestamp, + string nonce, + string? encryptType, + CancellationToken cancellationToken) + { if (!WeChatSignatureHelper.Validate(options.Value.Token, timestamp, nonce, signature)) { logger.LogWarning("微信消息验签失败");