From 18c071965505406deb9ed45e38bd6cbecd2df702 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Sat, 31 Jul 2021 20:30:04 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E5=AE=8C=E5=96=84=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=95=86=E6=88=B7=E5=B9=B3=E5=8F=B0=E6=A8=A1=E5=9D=97=E7=9A=84?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/DistributedLockFactory.cs | 1 + .../{WechatOptions.cs => WxpayOptions.cs} | 6 +- ...yCertificateRefreshingBackgroundService.cs | 77 +++++++++++++++++++ .../IWechatTenpayHttpClientFactory.cs | 2 +- .../WechatTenpayHttpClientFactory.cs | 4 +- .../Startup.cs | 5 +- .../appsettings.json | 2 +- 7 files changed, 89 insertions(+), 8 deletions(-) rename samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/{WechatOptions.cs => WxpayOptions.cs} (82%) create mode 100644 samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/WxpayCertificateRefreshingBackgroundService.cs diff --git a/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Services/DistributedLock/Implements/DistributedLockFactory.cs b/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Services/DistributedLock/Implements/DistributedLockFactory.cs index 7f416b73..5ffb84d3 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Services/DistributedLock/Implements/DistributedLockFactory.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Services/DistributedLock/Implements/DistributedLockFactory.cs @@ -14,6 +14,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.DistributedLock.I public IDistributedLock Create(string lockName) { + // NOTICE: 单机演示基于文件实现分布式锁,生产项目请替换成其他实现 return new FileDistributedLock(_lockFileDirectory, lockName); } } diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WechatOptions.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs similarity index 82% rename from samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WechatOptions.cs rename to samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs index 7b9c2113..76d47027 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WechatOptions.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs @@ -6,16 +6,16 @@ using Microsoft.Extensions.Options; namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Options { - public partial class WechatOptions : IOptions + public partial class WxpayOptions : IOptions { - WechatOptions IOptions.Value => this; + WxpayOptions IOptions.Value => this; public WechatMerchant[] Merchants { get; set; } = Array.Empty(); public string CallbackEntry { get; set; } = string.Empty; } - partial class WechatOptions + partial class WxpayOptions { public class WechatMerchant { diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/WxpayCertificateRefreshingBackgroundService.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/WxpayCertificateRefreshingBackgroundService.cs new file mode 100644 index 00000000..dc2b8d26 --- /dev/null +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/WxpayCertificateRefreshingBackgroundService.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SKIT.FlurlHttpClient.Wechat.TenpayV3; +using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models; +using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings; + +namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundServices +{ + class WxpayCertificateRefreshingBackgroundService : BackgroundService + { + private readonly ILogger _logger; + private readonly Options.WxpayOptions _wxpayOptions; + private readonly CertificateManager _certificateManager; + private readonly HttpClients.IWechatTenpayHttpClientFactory _wechatTenpayHttpClientFactory; + + public WxpayCertificateRefreshingBackgroundService( + ILoggerFactory loggerFactory, + IOptions wxpayOptions, + CertificateManager certificateManager, + HttpClients.IWechatTenpayHttpClientFactory wechatTenpayHttpClientFactory) + { + _logger = loggerFactory.CreateLogger(GetType()); + _wxpayOptions = wxpayOptions.Value; + _certificateManager = certificateManager; + _wechatTenpayHttpClientFactory = wechatTenpayHttpClientFactory; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + var wxpayMerchant = _wxpayOptions.Merchants.FirstOrDefault(); + if (wxpayMerchant == null) + { + _logger.LogWarning("未找到微信商户配置项。"); + break; + } + + try + { + var client = _wechatTenpayHttpClientFactory.Create(wxpayMerchant.MerchantId); + var request = new QueryCertificatesRequest(); + var response = await client.ExecuteQueryCertificatesAsync(request, cancellationToken: stoppingToken); + if (response.IsSuccessful()) + { + client.DecryptResponseEncryptedData(ref response); + foreach (var cert in response.CertificateList) + { + _certificateManager.SetCertificate(cert.SerialNumber, cert.EncryptCertificate.CipherText); + } + + _logger.LogInformation("刷新微信商户平台证书成功。"); + } + else + { + _logger.LogWarning( + "刷新微信商户平台证书失败(状态码:{0},错误代码:{1},错误描述:{2})。", + response.RawStatus, response.ErrorCode, response.ErrorMessage + ); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "刷新微信商户平台证书遇到异常。"); + } + + await Task.Delay(TimeSpan.FromDays(1)); // 每隔 1 天轮询刷新 + } + } + } +} diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/IWechatTenpayHttpClientFactory.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/IWechatTenpayHttpClientFactory.cs index 54378a81..0b1fc5a8 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/IWechatTenpayHttpClientFactory.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/IWechatTenpayHttpClientFactory.cs @@ -7,6 +7,6 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients { public interface IWechatTenpayHttpClientFactory { - WechatTenpayClient Create(string appId); + WechatTenpayClient Create(string merchantId); } } diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/Implements/WechatTenpayHttpClientFactory.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/Implements/WechatTenpayHttpClientFactory.cs index 9c44c96e..6996656b 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/Implements/WechatTenpayHttpClientFactory.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/Implements/WechatTenpayHttpClientFactory.cs @@ -14,12 +14,12 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients. partial class WechatTenpayHttpClientFactory : IWechatTenpayHttpClientFactory { private readonly System.Net.Http.IHttpClientFactory _httpClientFactory; - private readonly Options.WechatOptions _wechatOptions; + private readonly Options.WxpayOptions _wechatOptions; private readonly CertificateManager _certificateManager; public WechatTenpayHttpClientFactory( System.Net.Http.IHttpClientFactory httpClientFactory, - IOptions wechatOptions, + IOptions wechatOptions, CertificateManager certificateManager) { _httpClientFactory = httpClientFactory; diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Startup.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Startup.cs index 117bbac9..020b89fc 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Startup.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Startup.cs @@ -27,12 +27,15 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5 // ע services.AddOptions(); - services.Configure(Configuration.GetSection(nameof(Options.WechatOptions))); + services.Configure(Configuration.GetSection(nameof(Options.WxpayOptions))); // ע빤 HTTP ͻ services.AddHttpClient(); services.AddSingleton(); services.AddSingleton(); + + // ע̨ + services.AddHostedService(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/appsettings.json b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/appsettings.json index c0ef8455..f4d7cdd3 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/appsettings.json +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/appsettings.json @@ -9,7 +9,7 @@ "AllowedHosts": "*", - "WechatOptions": { + "WxpayOptions": { "Merchants": [ { "MerchantId": "д̻"