From cf6e0ecfbba6f349293e910c5e22556b3a6d1657 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Mon, 10 Jan 2022 11:55:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(wxapi):=20=E6=96=B0=E5=A2=9E=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=85=AC=E4=BC=97=E5=8F=B7=E7=BD=91=E9=A1=B5=E6=8E=88?= =?UTF-8?q?=E6=9D=83=20URL=20=E7=9A=84=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WechatApiClientParameterExtensions.cs | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) 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(); + } } }