From 937f0bb78bb61946703ac4e77349fbe47152c1a5 Mon Sep 17 00:00:00 2001 From: zzh <1368735060@qq.com> Date: Wed, 9 Sep 2026 09:17:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=BE=AE=E4=BF=A1=20?= =?UTF-8?q?OAuth=20=E5=85=AC=E5=BC=80=E9=85=8D=E7=BD=AE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 从其他配置 wechat_mp 返回 AppId 与 OAuthRedirectBase,供 H5 网页授权使用。 Co-authored-by: Cursor --- .../WeChatRouteIntegrationTests.cs | 1 + .../Auth/AuthService.WeChatMp.cs | 25 +++++++++++++++++++ src/SimpleAdmin.Api/WeChat/Dtos/WeChatDtos.cs | 13 ++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/SimpleAdmin.Api.IntegrationTests/WeChatRouteIntegrationTests.cs b/src/SimpleAdmin.Api.IntegrationTests/WeChatRouteIntegrationTests.cs index 5c4f785..db21717 100644 --- a/src/SimpleAdmin.Api.IntegrationTests/WeChatRouteIntegrationTests.cs +++ b/src/SimpleAdmin.Api.IntegrationTests/WeChatRouteIntegrationTests.cs @@ -32,6 +32,7 @@ public class WeChatRouteIntegrationTests host.RoutePatterns.Should().Contain("apiwechat/verification"); host.RoutePatterns.Should().Contain("wechat/mp/{appId}"); host.RoutePatterns.Should().Contain("wechat/mp/login-qrcode"); + host.RoutePatterns.Should().Contain("wechat/mp/oauth-config"); host.RoutePatterns.Should().Contain("wechat/mp/follow-qrcode"); host.RoutePatterns.Should().Contain("wechat/oauth/user-info"); host.RoutePatterns.Should().Contain("wechat/customer/register"); diff --git a/src/SimpleAdmin.Api/Auth/AuthService.WeChatMp.cs b/src/SimpleAdmin.Api/Auth/AuthService.WeChatMp.cs index 05bc350..850bac3 100644 --- a/src/SimpleAdmin.Api/Auth/AuthService.WeChatMp.cs +++ b/src/SimpleAdmin.Api/Auth/AuthService.WeChatMp.cs @@ -159,6 +159,31 @@ public partial class AuthService cancellationToken); } + /// + /// H5 网页授权公开配置:AppId + OAuth 回调根(来自其他配置 wechat_mp)。 + /// + [AllowAnonymous] + [HttpGet("/wechat/mp/oauth-config")] + [ApiExplorerSettings(GroupName = "WeChat")] + public async Task GetWeChatOAuthConfigAsync( + CancellationToken cancellationToken = default) + { + var options = await WeChatMpConfigProvider.GetAsync(cancellationToken); + if (string.IsNullOrWhiteSpace(options.AppId)) + { + throw new BusinessException( + "未配置微信公众号 AppId。请在「其他配置」维护 wechat_mp,AppKey 填公众号 AppId。"); + } + + return new WxOAuthConfigOutput + { + AppId = options.AppId.Trim(), + OAuthRedirectBase = string.IsNullOrWhiteSpace(options.OAuthRedirectBase) + ? null + : options.OAuthRedirectBase.Trim().TrimEnd('/') + }; + } + /// /// 获取 PC 扫码登录二维码。 /// diff --git a/src/SimpleAdmin.Api/WeChat/Dtos/WeChatDtos.cs b/src/SimpleAdmin.Api/WeChat/Dtos/WeChatDtos.cs index f5c09ba..d254466 100644 --- a/src/SimpleAdmin.Api/WeChat/Dtos/WeChatDtos.cs +++ b/src/SimpleAdmin.Api/WeChat/Dtos/WeChatDtos.cs @@ -12,6 +12,19 @@ public class WxLoginQrcodeOutput public int ExpiresIn { get; set; } = 600; } +/// H5 网页授权所需公开配置(不含 Secret/Token)。 +public class WxOAuthConfigOutput +{ + /// 公众号 AppId(来自其他配置 wechat_mp.AppKey)。 + public string AppId { get; set; } = string.Empty; + + /// + /// OAuth 回调根地址(来自 wechat_mp.BaseUrl),如 https://ideship.com。 + /// 为空时前端回退到当前页面 origin。 + /// + public string? OAuthRedirectBase { get; set; } +} + /// 引导关注公众号:优先返回可直接跳转的公众号链接。 public class WxFollowQrcodeOutput {