admin/src/Oms.Application/Integrations/Itfx/ItfxGatewayConfigProvider.cs
zzh 0fdc9e29f4
All checks were successful
Build and Deploy / deploy (push) Successful in 1m52s
feat: API中心其他配置与面单 COS 流代理
将 itfx 网关优先读其他配置 api_center(含 AppKey/AppSecret 回退),面单预览经服务端代理拉取 COS。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-12 17:40:52 +08:00

58 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Fake.DependencyInjection;
using Microsoft.Extensions.Options;
using Oms.Domain.SystemAggregate.Consts;
using Oms.Domain.SystemAggregate.Repositories;
namespace Oms.Application.Integrations.Itfx;
/// <summary>
/// itfx / API 中心运行时配置(优先「其他配置」api_center,回退 appsettings Oms:Itfx)。
/// </summary>
public interface IItfxGatewayConfigProvider
{
Task<ItfxGatewayOptions> GetAsync(CancellationToken cancellationToken = default);
}
internal sealed class ItfxGatewayConfigProvider(
ISystemOtherConfigRepository repository,
IOptions<ItfxGatewayOptions> options) : IItfxGatewayConfigProvider, IScopedDependency
{
public async Task<ItfxGatewayOptions> GetAsync(CancellationToken cancellationToken = default)
{
var fallback = options.Value;
var entity = await repository.GetByConfigCodeAsync(SystemOtherConfigCodes.ApiCenter, cancellationToken);
if (entity == null || !entity.Enabled)
{
return Clone(fallback);
}
return new ItfxGatewayOptions
{
BaseUrl = Coalesce(entity.BaseUrl, fallback.BaseUrl),
AppKey = Coalesce(entity.AppKey, fallback.AppKey),
AppSecret = Coalesce(entity.AppSecret, fallback.AppSecret),
TimeoutSeconds = entity.TimeoutSeconds > 0 ? entity.TimeoutSeconds : fallback.TimeoutSeconds,
Customs9610StandardCode = fallback.Customs9610StandardCode,
Customs9610ManifestStandardCode = fallback.Customs9610ManifestStandardCode,
Customs9610DeclarationFormStandardCode = fallback.Customs9610DeclarationFormStandardCode,
Customs9610SupplierCode = fallback.Customs9610SupplierCode
};
}
private static ItfxGatewayOptions Clone(ItfxGatewayOptions src) =>
new()
{
BaseUrl = src.BaseUrl,
AppKey = src.AppKey,
AppSecret = src.AppSecret,
TimeoutSeconds = src.TimeoutSeconds,
Customs9610StandardCode = src.Customs9610StandardCode,
Customs9610ManifestStandardCode = src.Customs9610ManifestStandardCode,
Customs9610DeclarationFormStandardCode = src.Customs9610DeclarationFormStandardCode,
Customs9610SupplierCode = src.Customs9610SupplierCode
};
private static string Coalesce(string? primary, string fallback) =>
string.IsNullOrWhiteSpace(primary) ? fallback : primary.Trim();
}