diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientParameterExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientParameterExtensions.cs index 6f823fa4..8c4fcce0 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientParameterExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientParameterExtensions.cs @@ -1,14 +1,14 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using Flurl; namespace SKIT.FlurlHttpClient.Wechat.Api { - /// - /// 为 提供客户端调起 JS-SDK 签名的扩展方法。 - /// public static class WechatApiClientParameterExtensions { + private const string BASE_URL = "https://open.weixin.qq.com/"; + /// /// 生成客户端 JS-SDK `wx.config` 所需的参数。 /// REF: https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#62 @@ -69,5 +69,51 @@ namespace SKIT.FlurlHttpClient.Wechat.Api { "cardSign", cardSign } }); } + + /// + /// 生成公众号网页授权 URL。 + /// REF: https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html + /// + /// + /// + /// + /// + /// + public static string GenerateParameterizedUrlForConnectOAuth2Authorize(this WechatApiClient client, string redirectUrl, string scope, string? state = null) + { + return new Url(BASE_URL) + .AppendPathSegments("connect", "oauth2", "authorize") + .SetQueryParam("appid", client.Credentials.AppId) + .SetQueryParam("redirect_uri", redirectUrl) + .SetQueryParam("response_type", "code") + .SetQueryParam("scope", scope) + .SetQueryParam("state", state) + .SetFragment("wechat_redirect") + .ToString(); + } + + /// + /// 生成代公众号网页授权 URL。 + /// REF: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html + /// + /// + /// + /// + /// + /// + /// + public static string GenerateParameterizedUrlForComponentConnectOAuth2Authorize(this WechatApiClient client, string appId, string redirectUrl, string scope, string? state = null) + { + return new Url(BASE_URL) + .AppendPathSegments("connect", "oauth2", "authorize") + .SetQueryParam("appid", appId) + .SetQueryParam("component_appid", client.Credentials.AppId) + .SetQueryParam("redirect_uri", redirectUrl) + .SetQueryParam("response_type", "code") + .SetQueryParam("scope", scope) + .SetQueryParam("state", state) + .SetFragment("wechat_redirect") + .ToString(); + } } }