🍓 feat(HttpRemote): 拆分远程请求配置到业务层

This commit is contained in:
喵你个汪呀 2025-08-26 09:04:53 +08:00
parent 85bbebf7c3
commit f566543d90
7 changed files with 119 additions and 113 deletions

View File

@ -0,0 +1,25 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Furion.ConfigurableOptions;
namespace Admin.NET.Application;
/// <summary>
/// 远程请求配置
/// </summary>
public sealed class HttpRemotesOptions : IConfigurableOptions
{
/// <summary>
/// 获取Ip地址接口
/// </summary>
public HttpRemoteItem HttpBin { get; set; }
/// <summary>
/// 企业微信接口配置
/// </summary>
public HttpRemoteItem WorkWeixin { get; set; }
}

View File

@ -17,7 +17,7 @@ public static class HttpRemotesExtension {
/// <returns></returns>
public static IServiceCollection AddHttpRemoteClientService(this IServiceCollection services)
{
var options = App.GetOptions<HttpRemotesOptions>();
var options = App.GetConfig<object>("HttpRemotes") ?? throw new Exception("未正确配置HttpRemotes.json");
foreach (var prop in options.GetType().GetProperties())
{
if (prop.GetValue(options) is not HttpRemoteItem opt) continue;
@ -46,4 +46,45 @@ public static class HttpRemotesExtension {
{
return HttpLoggingHandler.SetRemoteApiAttr(builder, httpName, attr);
}
}
/// <summary>
/// 远程请求配置项
/// </summary>
public sealed class HttpRemoteItem
{
/// <summary>
/// 是否启用日志
/// </summary>
public bool EnabledLog { get; set; }
/// <summary>
/// 是否启用代理
/// </summary>
public bool EnabledProxy { get; set; }
/// <summary>
/// 服务名称
/// </summary>
public string HttpName { get; set; }
/// <summary>
/// 服务地址
/// </summary>
public string BaseAddress { get; set; }
/// <summary>
/// 请求超时时间
/// </summary>
public int Timeout { get; set; }
/// <summary>
/// 是否自动处理Cookie
/// </summary>
public bool UseCookies { get; set; }
/// <summary>
/// 请求头
/// </summary>
public Dictionary<string, string> Headers { get; set; }
}

View File

@ -23,15 +23,16 @@ public class HttpLoggingHandler : DelegatingHandler, ITransient
private readonly SysConfigService _sysConfigService;
private readonly IEventPublisher _eventPublisher;
public HttpLoggingHandler(IEventPublisher eventPublisher, SysConfigService sysConfigService, IOptions<HttpRemotesOptions> options)
public HttpLoggingHandler(IEventPublisher eventPublisher, SysConfigService sysConfigService)
{
_eventPublisher = eventPublisher;
HttpRemotesOptions httpRemotesOptions = options.Value;
_sysConfigService = sysConfigService;
_enabledLogMap = typeof(HttpRemotesOptions).GetProperties()
var options = App.GetConfig<object>("HttpRemotes") ?? throw new Exception("未正确配置HttpRemotes.json");
_enabledLogMap = options.GetType().GetProperties()
.Where(u => u.PropertyType == typeof(HttpRemoteItem))
.ToDictionary(u => u.GetValue(httpRemotesOptions) is HttpRemoteItem opt ? opt.HttpName : "",
u => u.GetValue(httpRemotesOptions) is HttpRemoteItem { EnabledLog: true });
.ToDictionary(u => u.GetValue(options) is HttpRemoteItem opt ? opt.HttpName : "",
u => u.GetValue(options) is HttpRemoteItem { EnabledLog: true });
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,

View File

@ -0,0 +1,38 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// 远程请求代理配置
/// </summary>
public sealed class HttpProxyOption : IConfigurableOptions
{
/// <summary>
/// 是否加密
/// </summary>
public bool Encrypt { get; set; }
/// <summary>
/// 最大重试次数
/// </summary>
public string MaxRetries { get; set; }
/// <summary>
/// 代理服务器地址
/// </summary>
public string Address { get; set; }
/// <summary>
/// 代理服务器认证账号
/// </summary>
public string Account { get; set; }
/// <summary>
/// 代理服务器认证账号密码
/// </summary>
public string Password { get; set; }
}

View File

@ -1,100 +0,0 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// 远程请求配置
/// </summary>
public sealed class HttpRemotesOptions : IConfigurableOptions
{
/// <summary>
/// 代理配置
/// </summary>
public HttpProxyOption Proxy { get; set; }
/// <summary>
/// 获取Ip地址接口
/// </summary>
public HttpRemoteItem HttpBin { get; set; }
/// <summary>
/// 企业微信接口配置
/// </summary>
public HttpRemoteItem WorkWeixin { get; set; }
}
/// <summary>
/// 代理配置
/// </summary>
public class HttpProxyOption
{
/// <summary>
/// 是否加密
/// </summary>
public bool Encrypt { get; set; }
/// <summary>
/// 最大重试次数
/// </summary>
public string MaxRetries { get; set; }
/// <summary>
/// 代理服务器地址
/// </summary>
public string Address { get; set; }
/// <summary>
/// 代理服务器认证账号
/// </summary>
public string Account { get; set; }
/// <summary>
/// 代理服务器认证账号密码
/// </summary>
public string Password { get; set; }
}
/// <summary>
/// 远程请求配置项
/// </summary>
public sealed class HttpRemoteItem
{
/// <summary>
/// 是否启用日志
/// </summary>
public bool EnabledLog { get; set; }
/// <summary>
/// 是否启用代理
/// </summary>
public bool EnabledProxy { get; set; }
/// <summary>
/// 服务名称
/// </summary>
public string HttpName { get; set; }
/// <summary>
/// 服务地址
/// </summary>
public string BaseAddress { get; set; }
/// <summary>
/// 请求超时时间
/// </summary>
public int Timeout { get; set; }
/// <summary>
/// 是否自动处理Cookie
/// </summary>
public bool UseCookies { get; set; }
/// <summary>
/// 请求头
/// </summary>
public Dictionary<string, string> Headers { get; set; }
}

View File

@ -4,6 +4,7 @@
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Admin.NET.Application;
using Admin.NET.Core;
using AspNetCoreRateLimit;
using Furion;

View File

@ -21,9 +21,10 @@ namespace Admin.NET.Plugin.WorkWeixin;
public class WorkWxBaseService(
SysCacheService sysCacheService,
SysConfigService sysConfigService,
IHttpRemoteService httpRemoteService,
IOptions<HttpRemotesOptions> options) : ITransient
IHttpRemoteService httpRemoteService) : ITransient
{
private static readonly Lazy<HttpRemoteItem> _options = new(() => App.GetConfig<HttpRemoteItem>("HttpRemotes.WorkWeixin"));
/// <summary>
/// 获取企业微信接口凭证
/// </summary>
@ -54,15 +55,14 @@ public class WorkWxBaseService(
/// <returns>返回结果</returns>
public async Task<R> SendAsync<T, R>(T input) where R : BaseWorkWxOutput
{
var opt = options.Value.WorkWeixin;
if (opt == null || string.IsNullOrWhiteSpace(opt.BaseAddress)) throw Oops.Oh("[企业微信] 服务配置缺失");
if (_options.Value == null || string.IsNullOrWhiteSpace(_options.Value.BaseAddress)) throw Oops.Oh("[企业微信] 服务配置缺失");
var attr = typeof(T).GetCustomAttribute<HttpRemoteApiAttribute>();
if (attr == null || string.IsNullOrWhiteSpace(attr.Action as string))
throw Oops.Oh("[企业微信] 接口入参未正确配置[HttpRemoteApi]特性");
// 拼接请求地址并设置token
var url = opt.BaseAddress + $"/cgi-bin/{attr.Action}?";
var url = _options.Value.BaseAddress + $"/cgi-bin/{attr.Action}?";
if (input is AuthWorkWxInput)
{
// 重试3次
@ -83,9 +83,9 @@ public class WorkWxBaseService(
{
HttpMethodEnum.Get => await httpRemoteService.GetAsync(
url + input.ToCustomJsonPropertyQueryString(),
builder => builder.SetRemoteApiAttr(opt.HttpName, attr)),
builder => builder.SetRemoteApiAttr(_options.Value.HttpName, attr)),
HttpMethodEnum.Post => await httpRemoteService.PostAsync(url,
builder => builder.SetRemoteApiAttr(opt.HttpName, attr)
builder => builder.SetRemoteApiAttr(_options.Value.HttpName, attr)
.SetContent(new StringContent(JSON.Serialize(input, CustomJsonPropertyConverter.Options), Encoding.UTF8, "application/json"))),
_ => throw Oops.Oh($"[企业微信] 不支持的请求方式:{attr.HttpMethod.ToString()}({typeof(T).FullName})"),
};