feat(wxapi): 升级公共组件

This commit is contained in:
Fu Diwei
2024-01-29 23:11:56 +08:00
committed by RHQYZ
parent 54e7ac8266
commit 5a110785f8
590 changed files with 7047 additions and 7220 deletions

View File

@@ -13,14 +13,14 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
public class WechatNotifyController : ControllerBase
{
private readonly ILogger _logger;
private readonly Services.HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
private readonly Services.HttpClients.IWechatApiClientFactory _wechatApiClientFactory;
public WechatNotifyController(
ILoggerFactory loggerFactory,
Services.HttpClients.IWechatApiHttpClientFactory wechatApiHttpClientFactory)
Services.HttpClients.IWechatApiClientFactory wechatApiClientFactory)
{
_logger = loggerFactory.CreateLogger(GetType());
_wechatApiHttpClientFactory = wechatApiHttpClientFactory;
_wechatApiClientFactory = wechatApiClientFactory;
}
[HttpGet]
@@ -35,8 +35,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
// 验证服务器推送
// 文档https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
var client = _wechatApiHttpClientFactory.Create(appId);
bool valid = client.VerifyEventSignatureForEcho(callbackTimestamp: timestamp, callbackNonce: nonce, callbackSignature: signature);
var client = _wechatApiClientFactory.Create(appId);
bool valid = client.VerifyEventSignatureForEcho(webhookTimestamp: timestamp, webhookNonce: nonce, webhookSignature: signature);
if (!valid)
{
return Content("fail");
@@ -57,7 +57,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
string content = await reader.ReadToEndAsync();
_logger.LogInformation("接收到微信推送的数据:{0}", content);
var client = _wechatApiHttpClientFactory.Create(appId);
var client = _wechatApiClientFactory.Create(appId);
var msgType = client.DeserializeEventFromXml(content).MessageType?.ToUpper();
switch (msgType)
{

View File

@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@@ -14,16 +14,16 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
{
private readonly ILogger _logger;
private readonly Services.Repositories.IWechatAccessTokenEntityRepository _wechatAccessTokenEntityRepository;
private readonly Services.HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
private readonly Services.HttpClients.IWechatApiClientFactory _wechatApiClientFactory;
public WechatUserController(
ILoggerFactory loggerFactory,
Services.Repositories.IWechatAccessTokenEntityRepository wechatAccessTokenEntityRepository,
Services.HttpClients.IWechatApiHttpClientFactory wechatApiHttpClientFactory)
Services.HttpClients.IWechatApiClientFactory wechatApiClientFactory)
{
_logger = loggerFactory.CreateLogger(GetType());
_wechatAccessTokenEntityRepository = wechatAccessTokenEntityRepository;
_wechatApiHttpClientFactory = wechatApiHttpClientFactory;
_wechatApiClientFactory = wechatApiClientFactory;
}
[HttpGet]
@@ -33,14 +33,14 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
[FromQuery(Name = "open_id")] string openId)
{
var entity = _wechatAccessTokenEntityRepository.FirstOrDefault(e => e.AppId == appId);
var client = _wechatApiHttpClientFactory.Create(appId);
var client = _wechatApiClientFactory.Create(appId);
var request = new CgibinUserInfoRequest() { AccessToken = entity?.AccessToken, OpenId = openId };
var response = await client.ExecuteCgibinUserInfoAsync(request, cancellationToken: HttpContext.RequestAborted);
if (!response.IsSuccessful())
{
_logger.LogWarning(
"获取用户基本信息失败(状态码:{0},错误代码:{1},错误描述:{2})。",
response.RawStatus, response.ErrorCode, response.ErrorMessage
response.GetRawStatus(), response.ErrorCode, response.ErrorMessage
);
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@@ -17,20 +17,20 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.BackgroundServices
private readonly ILogger _logger;
private readonly Options.WechatOptions _wechatOptions;
private readonly DistributedLock.IDistributedLockFactory _distributedLockFactory;
private readonly HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
private readonly HttpClients.IWechatApiClientFactory _wechatApiClientFactory;
private readonly Repositories.IWechatAccessTokenEntityRepository _wechatAccessTokenEntityRepository;
public WechatAccessTokenRefreshingBackgroundService(
ILoggerFactory loggerFactory,
IOptions<Options.WechatOptions> wechatOptions,
DistributedLock.IDistributedLockFactory distributedLockFactory,
HttpClients.IWechatApiHttpClientFactory wechatApiHttpClientFactory,
HttpClients.IWechatApiClientFactory wechatApiClientFactory,
Repositories.IWechatAccessTokenEntityRepository wechatAccessTokenEntityRepository)
{
_logger = loggerFactory.CreateLogger(GetType());
_wechatOptions = wechatOptions.Value;
_distributedLockFactory = distributedLockFactory;
_wechatApiHttpClientFactory = wechatApiHttpClientFactory;
_wechatApiClientFactory = wechatApiClientFactory;
_wechatAccessTokenEntityRepository = wechatAccessTokenEntityRepository;
}
@@ -64,14 +64,14 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.BackgroundServices
if (lockHandler == null)
return; // 未取得锁
var client = _wechatApiHttpClientFactory.Create(appId);
var client = _wechatApiClientFactory.Create(appId);
var request = new CgibinTokenRequest();
var response = await client.ExecuteCgibinTokenAsync(request, cancellationToken);
if (!response.IsSuccessful())
{
_logger.LogWarning(
"刷新 AppId 为 {0} 微信 AccessToken 失败(状态码:{1},错误代码:{2},错误描述:{3})。",
appId, response.RawStatus, response.ErrorCode, response.ErrorMessage
appId, response.GetRawStatus(), response.ErrorCode, response.ErrorMessage
);
return; // 请求失败
}

View File

@@ -0,0 +1,7 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients
{
public interface IWechatApiClientFactory
{
WechatApiClient Create(string appId);
}
}

View File

@@ -1,7 +0,0 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients
{
public interface IWechatApiHttpClientFactory
{
WechatApiClient Create(string appId);
}
}

View File

@@ -1,20 +1,16 @@
using System;
using System.Linq;
using System.Net.Http;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients.Implements
{
internal partial class WechatApiHttpClientFactory : IWechatApiHttpClientFactory
internal partial class WechatApiClientFactory : IWechatApiClientFactory
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly Options.WechatOptions _wechatOptions;
public WechatApiHttpClientFactory(
IHttpClientFactory httpClientFactory,
public WechatApiClientFactory(
IOptions<Options.WechatOptions> wechatOptions)
{
_httpClientFactory = httpClientFactory;
_wechatOptions = wechatOptions.Value;
}
@@ -36,26 +32,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients.Implements
PushToken = _wechatOptions.CallbackToken
};
var wechatApiClient = new WechatApiClient(wechatApiClientOptions);
wechatApiClient.Configure((settings) => settings.FlurlHttpClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory));
return wechatApiClient;
}
}
internal partial class WechatApiHttpClientFactory
{
internal class DelegatingFlurlClientFactory : Flurl.Http.Configuration.DefaultHttpClientFactory
{
private readonly IHttpClientFactory _httpClientFactory;
public DelegatingFlurlClientFactory(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
}
public override HttpClient CreateHttpClient(HttpMessageHandler handler)
{
return _httpClientFactory.CreateClient();
}
}
}
}

View File

@@ -29,7 +29,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample
// 注入工厂 HTTP 客户端
services.AddHttpClient();
services.AddSingleton<Services.HttpClients.IWechatApiHttpClientFactory, Services.HttpClients.Implements.WechatApiHttpClientFactory>();
services.AddSingleton<Services.HttpClients.IWechatApiClientFactory, Services.HttpClients.Implements.WechatApiClientFactory>();
// 注入后台任务
services.AddHostedService<Services.BackgroundServices.WechatAccessTokenRefreshingBackgroundService>();