From ee8fb3824cc8d5f2c440e34c0d3cbe656efe00bf Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Tue, 27 Jul 2021 00:49:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(wxapi):=20=E6=96=B0=E5=A2=9E=20Credentials?= =?UTF-8?q?=20=E7=B1=BB=E5=9E=8B=EF=BC=8C=E5=AD=98=E5=82=A8=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E5=87=AD=E8=AF=81=E5=89=AF=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WechatApiClientEventExtensions.cs | 2 +- ...atApiClientExecuteCardInvoiceExtensions.cs | 2 +- ...iClientExecuteCgibinComponentExtensions.cs | 22 ++++----- ...ApiClientExecuteCgibinExpressExtensions.cs | 4 +- .../WechatApiClientExecuteCgibinExtensions.cs | 6 +-- ...piClientExecuteCgibinMaterialExtensions.cs | 2 +- ...atApiClientExecuteCgibinMidasExtensions.cs | 6 +-- .../WechatApiClientExecuteNontaxExtensions.cs | 22 ++++----- ...WechatApiClientExecuteProductExtensions.cs | 4 +- ...echatApiClientExecuteSemanticExtensions.cs | 2 +- .../WechatApiClientExecuteSnsExtensions.cs | 12 ++--- ...atApiClientExecuteWxaBusinessExtensions.cs | 2 +- .../WechatApiClientExecuteWxaExtensions.cs | 2 +- .../WechatApiClientParameterExtensions.cs | 4 +- .../Settings/Credentials.cs | 47 +++++++++++++++++++ .../WechatApiClient.cs | 38 ++------------- 16 files changed, 96 insertions(+), 81 deletions(-) create mode 100644 src/SKIT.FlurlHttpClient.Wechat.Api/Settings/Credentials.cs diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs index f18f8be5..d0016662 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs @@ -21,7 +21,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (client == null) throw new ArgumentNullException(nameof(client)); if (string.IsNullOrEmpty(callbackJson)) throw new ArgumentNullException(callbackJson); - return client.FlurlJsonSerializer.Deserialize(callbackJson); + return client.JsonSerializer.Deserialize(callbackJson); } /// diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCardInvoiceExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCardInvoiceExtensions.cs index 46798167..a4aca6f1 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCardInvoiceExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCardInvoiceExtensions.cs @@ -173,7 +173,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; if (request.CardExtra != null && string.IsNullOrEmpty(request.CardExtra.Nonce)) request.CardExtra.Nonce = Guid.NewGuid().ToString("N"); diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinComponentExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinComponentExtensions.cs index 42fb87de..4aa658a9 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinComponentExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinComponentExtensions.cs @@ -25,10 +25,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; if (string.IsNullOrEmpty(request.ComponentSecret)) - request.ComponentSecret = client.WechatAppSecret; + request.ComponentSecret = client.Credentials.AppSecret; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_start_push_ticket"); @@ -50,10 +50,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; if (string.IsNullOrEmpty(request.ComponentSecret)) - request.ComponentSecret = client.WechatAppSecret; + request.ComponentSecret = client.Credentials.AppSecret; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_component_token"); @@ -75,7 +75,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_create_preauthcode") @@ -98,7 +98,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_query_auth") @@ -121,7 +121,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_authorizer_token") @@ -144,7 +144,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_get_authorizer_info") @@ -167,7 +167,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_get_authorizer_list") @@ -190,7 +190,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_get_authorizer_option") @@ -213,7 +213,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_set_authorizer_option") diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinExpressExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinExpressExtensions.cs index b942925b..3209aac2 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinExpressExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinExpressExtensions.cs @@ -19,12 +19,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (string.IsNullOrEmpty(request.ShopId)) { - request.ShopId = client.WechatImmeDeliveryAppKey; + request.ShopId = client.Credentials.ImmeDeliveryAppKey; } if (string.IsNullOrEmpty(request.DeliverySignature)) { - string plainText = $"{request.ShopId}{request.ShopOrderId}{client.WechatImmeDeliveryAppSecret}"; + string plainText = $"{request.ShopId}{request.ShopOrderId}{client.Credentials.ImmeDeliveryAppSecret}"; request.DeliverySignature = Security.SHA1Utility.Hash(plainText).ToLower(); } diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinExtensions.cs index ba2ef432..d0879d49 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinExtensions.cs @@ -29,8 +29,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Get, "cgi-bin", "token") .SetQueryParam("grant_type", request.GrantType) - .SetQueryParam("appid", client.WechatAppId) - .SetQueryParam("secret", client.WechatAppSecret); + .SetQueryParam("appid", client.Credentials.AppId) + .SetQueryParam("secret", client.Credentials.AppSecret); return await client.SendRequestWithJsonAsync(flurlReq, data: request, cancellationToken: cancellationToken); } @@ -112,7 +112,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "cgi-bin", "clear_quota") diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinMaterialExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinMaterialExtensions.cs index d03506ce..fb990b0f 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinMaterialExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinMaterialExtensions.cs @@ -85,7 +85,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x"); using var fileContent = new ByteArrayContent(request.FileBytes ?? new byte[0]); - using var descContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.FlurlJsonSerializer.Serialize(request))); + using var descContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request))); using var httpContent = new MultipartFormDataContent(boundary); httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\""); httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary); diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinMidasExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinMidasExtensions.cs index a15228e3..cc8b8707 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinMidasExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteCgibinMidasExtensions.cs @@ -22,7 +22,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (string.IsNullOrEmpty(request.AppId)) { - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; } if (!request.Timestamp.HasValue) @@ -46,8 +46,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api string plainText = string.Join("&", paramMap.Select(e => $"{e.Key}={e.Value}")) + $"&org_loc={reqLoc}" + $"&method={method.ToUpper()}" - + $"&secret={client.WechatMidasAppKey}"; - request.Signature = Security.HMACSHA256Utility.Hash(client.WechatMidasAppKey ?? string.Empty, plainText).ToLower(); + + $"&secret={client.Credentials.MidasAppKey}"; + request.Signature = Security.HMACSHA256Utility.Hash(client.Credentials.MidasAppKey ?? string.Empty, plainText).ToLower(); } return request; diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteNontaxExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteNontaxExtensions.cs index f4ad26d9..08abcce4 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteNontaxExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteNontaxExtensions.cs @@ -70,7 +70,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "insertbill") @@ -94,7 +94,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "queryfee") @@ -117,7 +117,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "unifiedorder") @@ -140,7 +140,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "getorder") @@ -163,7 +163,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "refund") @@ -186,7 +186,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "downloadbill") @@ -209,7 +209,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "notifyinconsistentorder") @@ -232,7 +232,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "mocknotification") @@ -255,7 +255,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "mockqueryfee") @@ -278,7 +278,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "micropay") @@ -301,7 +301,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "nontax", "getorderlist") diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteProductExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteProductExtensions.cs index eb58af47..394d8d52 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteProductExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteProductExtensions.cs @@ -111,7 +111,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "product", "register", "submit_merchantinfo") @@ -134,7 +134,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "product", "register", "submit_basicinfo") diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSemanticExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSemanticExtensions.cs index de672d67..92726395 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSemanticExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSemanticExtensions.cs @@ -28,7 +28,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "semantic", "semproxy", "search") diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSnsExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSnsExtensions.cs index 6800c3f6..e9b56e39 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSnsExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSnsExtensions.cs @@ -29,8 +29,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Get, "sns", "oauth2", "access_token") .SetQueryParam("grant_type", request.GrantType) - .SetQueryParam("appid", client.WechatAppId) - .SetQueryParam("secret", client.WechatAppSecret) + .SetQueryParam("appid", client.Credentials.AppId) + .SetQueryParam("secret", client.Credentials.AppSecret) .SetQueryParam("code", request.Code); return await client.SendRequestWithJsonAsync(flurlReq, data: request, cancellationToken: cancellationToken); @@ -54,7 +54,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Get, "sns", "oauth2", "refresh_token") .SetQueryParam("grant_type", request.GrantType) - .SetQueryParam("appid", client.WechatAppId) + .SetQueryParam("appid", client.Credentials.AppId) .SetQueryParam("refresh_token", request.RefreshToken); return await client.SendRequestWithJsonAsync(flurlReq, data: request, cancellationToken: cancellationToken); @@ -124,8 +124,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Get, "sns", "jscode2session") .SetQueryParam("grant_type", request.GrantType) - .SetQueryParam("appid", client.WechatAppId) - .SetQueryParam("secret", client.WechatAppSecret) + .SetQueryParam("appid", client.Credentials.AppId) + .SetQueryParam("secret", client.Credentials.AppSecret) .SetQueryParam("js_code", request.JsCode); return await client.SendRequestWithJsonAsync(flurlReq, data: request, cancellationToken: cancellationToken); @@ -146,7 +146,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.ComponentAppId)) - request.ComponentAppId = client.WechatAppId; + request.ComponentAppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Get, "sns", "component", "jscode2session") diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteWxaBusinessExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteWxaBusinessExtensions.cs index f8cc19c3..9c649c5d 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteWxaBusinessExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteWxaBusinessExtensions.cs @@ -26,7 +26,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "wxa", "business", "getpayfororder") diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteWxaExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteWxaExtensions.cs index d6e20c1e..1a51382b 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteWxaExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteWxaExtensions.cs @@ -400,7 +400,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api if (request is null) throw new ArgumentNullException(nameof(request)); if (string.IsNullOrEmpty(request.AppId)) - request.AppId = client.WechatAppId; + request.AppId = client.Credentials.AppId; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "wxa", "getuserriskrank") diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientParameterExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientParameterExtensions.cs index 3b024f9c..6f8cee46 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientParameterExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientParameterExtensions.cs @@ -37,7 +37,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api return new ReadOnlyDictionary(new Dictionary() { - { "appId", client.WechatAppId }, + { "appId", client.Credentials.AppId }, { "timestamp", timestamp }, { "nonceStr", nonce }, { "signature", sign } @@ -63,7 +63,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api SortedSet sortedParams = new SortedSet(); sortedParams.Add(cardType); sortedParams.Add(timestamp); - sortedParams.Add(client.WechatAppId); + sortedParams.Add(client.Credentials.AppId); sortedParams.Add(nonce); sortedParams.Add(wxcardTicket); string cardSign = Security.SHA1Utility.Hash(string.Join(string.Empty, sortedParams)).ToLower(); diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Settings/Credentials.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Settings/Credentials.cs new file mode 100644 index 00000000..29208ffb --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Settings/Credentials.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SKIT.FlurlHttpClient.Wechat.Api.Settings +{ + public class Credentials + { + /// + /// 初始化客户端时 的副本。 + /// + public string AppId { get; } + + /// + /// 初始化客户端时 的副本。 + /// + public string AppSecret { get; } + + /// + /// 初始化客户端时 的副本。 + /// + public string? ImmeDeliveryAppKey { get; } + + /// + /// 初始化客户端时 的副本。 + /// + public string? ImmeDeliveryAppSecret { get; } + + /// + /// 初始化客户端时 的副本。 + /// + public string? MidasAppKey { get; } + + internal Credentials(WechatApiClientOptions options) + { + if (options == null) throw new ArgumentNullException(nameof(options)); + + AppId = options.AppId; + AppSecret = options.AppSecret; + ImmeDeliveryAppKey = options.ImmeDeliveryAppKey; + ImmeDeliveryAppSecret = options.ImmeDeliveryAppSecret; + MidasAppKey = options.MidasAppKey; + } + } +} diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/WechatApiClient.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/WechatApiClient.cs index df93e940..e07a5d3c 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/WechatApiClient.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/WechatApiClient.cs @@ -18,37 +18,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api public class WechatApiClient : WechatClientBase { /// - /// 获取当前客户端使用的微信 AppId。 + /// 获取当前客户端使用的微信公众平台凭证。 /// - public string WechatAppId { get; } - - /// - /// 获取当前客户端使用的微信 AppSecret。 - /// - internal string WechatAppSecret { get; } - - /// - /// 获取当前客户端使用的即时配送公司帐号 AppKey(用于即使配送相关接口的请求签名)。 - /// - internal string? WechatImmeDeliveryAppKey { get; } - - /// - /// 获取当前客户端使用的即时配送公司帐号 AppSecret(用于即使配送相关接口的请求签名)。 - /// - internal string? WechatImmeDeliveryAppSecret { get; } - - /// - /// 获取或设置米大师平台 AppKey(用于虚拟支付相关接口的请求签名)。 - /// - internal string? WechatMidasAppKey { get; } - - /// - /// 获取当前客户端使用的 JSON 序列化器。 - /// - internal ISerializer FlurlJsonSerializer - { - get { return FlurlClient.Settings?.JsonSerializer ?? new FlurlNewtonsoftJsonSerializer(); } - } + public Settings.Credentials Credentials { get; } /// /// 用指定的配置项初始化 类的新实例。 @@ -59,11 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api { if (options == null) throw new ArgumentNullException(nameof(options)); - WechatAppId = options.AppId; - WechatAppSecret = options.AppSecret; - WechatImmeDeliveryAppKey = options.ImmeDeliveryAppKey; - WechatImmeDeliveryAppSecret = options.ImmeDeliveryAppSecret; - WechatMidasAppKey = options.MidasAppKey; + Credentials = new Settings.Credentials(options); FlurlClient.BaseUrl = options.Endpoints ?? WechatApiEndpoints.DEFAULT; FlurlClient.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout));