2022-07-07 19:59:20 +08:00
|
|
|
|
using System;
|
2021-05-28 19:23:28 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
2022-07-07 19:59:20 +08:00
|
|
|
|
using System.Linq;
|
2022-01-10 11:55:33 +08:00
|
|
|
|
using Flurl;
|
2021-05-28 19:23:28 +08:00
|
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.Api
|
|
|
|
|
{
|
|
|
|
|
public static class WechatApiClientParameterExtensions
|
|
|
|
|
{
|
2022-07-07 19:59:20 +08:00
|
|
|
|
private const string BASE_URL_OPEN = "https://open.weixin.qq.com/";
|
|
|
|
|
private const string BASE_URL_MP = "https://mp.weixin.qq.com/";
|
2022-01-10 11:55:33 +08:00
|
|
|
|
|
2021-05-28 19:23:28 +08:00
|
|
|
|
/// <summary>
|
2022-01-10 12:09:48 +08:00
|
|
|
|
/// <para>生成公众号 JS-SDK `wx.config` 所需的参数字典。</para>
|
2021-05-28 19:23:28 +08:00
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#62 </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="jsapiTicket"></param>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-10-06 13:37:21 +08:00
|
|
|
|
public static IDictionary<string, string> GenerateParametersForJSSDKConfig(this WechatApiClient client, string jsapiTicket, string url)
|
2021-05-28 19:23:28 +08:00
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (jsapiTicket is null) throw new ArgumentNullException(nameof(jsapiTicket));
|
|
|
|
|
if (url is null) throw new ArgumentNullException(nameof(url));
|
|
|
|
|
|
|
|
|
|
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
|
|
|
|
|
string nonce = Guid.NewGuid().ToString("N");
|
2021-11-09 15:23:23 +08:00
|
|
|
|
string sign = Utilities.SHA1Utility.Hash($"jsapi_ticket={jsapiTicket}&noncestr={nonce}×tamp={timestamp}&url={url.Split('#')[0]}").ToLower();
|
2021-05-28 19:23:28 +08:00
|
|
|
|
|
|
|
|
|
return new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
|
|
|
|
|
{
|
2021-07-27 00:49:38 +08:00
|
|
|
|
{ "appId", client.Credentials.AppId },
|
2021-05-28 19:23:28 +08:00
|
|
|
|
{ "timestamp", timestamp },
|
|
|
|
|
{ "nonceStr", nonce },
|
|
|
|
|
{ "signature", sign }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-01-10 12:09:48 +08:00
|
|
|
|
/// <para>生成公众号 JS-SDK `wx.chooseInvoice` 接口所需的参数字典。</para>
|
2021-05-28 19:23:28 +08:00
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/WeChat_Invoice/Auto-print/API_Documentation.html#_6-4-%E5%8F%91%E7%A5%A8%E7%AD%BE%E5%90%8D%E6%96%B9%E6%B3%95 </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="wxcardTicket"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-10-06 13:37:21 +08:00
|
|
|
|
public static IDictionary<string, string> GenerateParametersForJSSDKChooseInvoice(this WechatApiClient client, string wxcardTicket)
|
2021-05-28 19:23:28 +08:00
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (wxcardTicket is null) throw new ArgumentNullException(nameof(wxcardTicket));
|
|
|
|
|
|
|
|
|
|
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
|
|
|
|
|
string nonce = Guid.NewGuid().ToString("N");
|
|
|
|
|
string cardType = "INVOICE";
|
|
|
|
|
|
2021-08-12 20:23:57 +08:00
|
|
|
|
ISet<string> sortedParams = new SortedSet<string>(StringComparer.Ordinal);
|
2021-05-28 19:23:28 +08:00
|
|
|
|
sortedParams.Add(cardType);
|
|
|
|
|
sortedParams.Add(timestamp);
|
2021-07-27 00:49:38 +08:00
|
|
|
|
sortedParams.Add(client.Credentials.AppId);
|
2021-05-28 19:23:28 +08:00
|
|
|
|
sortedParams.Add(nonce);
|
|
|
|
|
sortedParams.Add(wxcardTicket);
|
2021-11-09 15:23:23 +08:00
|
|
|
|
string cardSign = Utilities.SHA1Utility.Hash(string.Join(string.Empty, sortedParams)).ToLower();
|
2021-05-28 19:23:28 +08:00
|
|
|
|
|
|
|
|
|
return new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{ "timestamp", timestamp },
|
|
|
|
|
{ "nonceStr", nonce },
|
|
|
|
|
{ "signType", "SHA1" },
|
|
|
|
|
{ "cardType", cardType },
|
|
|
|
|
{ "cardSign", cardSign }
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-01-10 11:55:33 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>生成公众号网页授权 URL。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="redirectUrl"></param>
|
|
|
|
|
/// <param name="scope"></param>
|
|
|
|
|
/// <param name="state"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GenerateParameterizedUrlForConnectOAuth2Authorize(this WechatApiClient client, string redirectUrl, string scope, string? state = null)
|
|
|
|
|
{
|
2022-07-07 19:59:20 +08:00
|
|
|
|
return new Url(BASE_URL_OPEN)
|
2022-01-10 11:55:33 +08:00
|
|
|
|
.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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>生成代公众号网页授权 URL。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="appId"></param>
|
|
|
|
|
/// <param name="redirectUrl"></param>
|
|
|
|
|
/// <param name="scope"></param>
|
|
|
|
|
/// <param name="state"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GenerateParameterizedUrlForComponentConnectOAuth2Authorize(this WechatApiClient client, string appId, string redirectUrl, string scope, string? state = null)
|
|
|
|
|
{
|
2022-07-07 19:59:20 +08:00
|
|
|
|
return new Url(BASE_URL_OPEN)
|
2022-01-10 11:55:33 +08:00
|
|
|
|
.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();
|
|
|
|
|
}
|
2022-07-07 19:59:20 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>生成公众号管理员向第三方平台授权 URL(H5 版)。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Authorization_Process_Technical_Description.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="preAuthCode"></param>
|
|
|
|
|
/// <param name="redirectUrl"></param>
|
|
|
|
|
/// <param name="authType"></param>
|
|
|
|
|
/// <param name="businessAppId"></param>
|
|
|
|
|
/// <param name="categoryIdList"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GenerateParameterizedUrlForComponentSafeBindComponent(this WechatApiClient client, string preAuthCode, string redirectUrl, int authType = 3, string? businessAppId = null, params string[] categoryIdList)
|
|
|
|
|
{
|
|
|
|
|
return new Url(BASE_URL_OPEN)
|
|
|
|
|
.AppendPathSegments("wxaopen", "safe", "bindcomponent")
|
|
|
|
|
.SetQueryParam("action", "bindcomponent")
|
|
|
|
|
.SetQueryParam("no_scan", 1)
|
|
|
|
|
.SetQueryParam("component_appid", client.Credentials.AppId)
|
|
|
|
|
.SetQueryParam("pre_auth_code", preAuthCode)
|
|
|
|
|
.SetQueryParam("redirect_uri", redirectUrl)
|
|
|
|
|
.SetQueryParam("auth_type", authType)
|
|
|
|
|
.SetQueryParam("biz_appid", businessAppId)
|
|
|
|
|
.SetQueryParam("category_id_list", categoryIdList.Any() ? string.Join("|", categoryIdList) : null)
|
|
|
|
|
.SetFragment("wechat_redirect")
|
|
|
|
|
.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>生成公众号管理员向第三方平台授权 URL(PC 版)。</para>
|
|
|
|
|
/// <para>REF: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Authorization_Process_Technical_Description.html </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="preAuthCode"></param>
|
|
|
|
|
/// <param name="redirectUrl"></param>
|
|
|
|
|
/// <param name="authType"></param>
|
|
|
|
|
/// <param name="businessAppId"></param>
|
|
|
|
|
/// <param name="categoryIdList"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GenerateParameterizedUrlForComponentLoginPage(this WechatApiClient client, string preAuthCode, string redirectUrl, int authType = 3, string? businessAppId = null, params string[] categoryIdList)
|
|
|
|
|
{
|
|
|
|
|
return new Url(BASE_URL_MP)
|
|
|
|
|
.AppendPathSegments("cgi-bin", "componentloginpage")
|
|
|
|
|
.SetQueryParam("component_appid", client.Credentials.AppId)
|
|
|
|
|
.SetQueryParam("pre_auth_code", preAuthCode)
|
|
|
|
|
.SetQueryParam("redirect_uri", redirectUrl)
|
|
|
|
|
.SetQueryParam("auth_type", authType)
|
|
|
|
|
.SetQueryParam("biz_appid", businessAppId)
|
|
|
|
|
.SetQueryParam("category_id_list", categoryIdList.Any() ? string.Join("|", categoryIdList) : null)
|
|
|
|
|
.ToString();
|
|
|
|
|
}
|
2021-05-28 19:23:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|