diff --git a/src/Oms.Application/Dtos/Customers/CustomerOpenApiCredentialDtos.cs b/src/Oms.Application/Dtos/Customers/CustomerOpenApiCredentialDtos.cs index e9b8d70..e5b11c3 100644 --- a/src/Oms.Application/Dtos/Customers/CustomerOpenApiCredentialDtos.cs +++ b/src/Oms.Application/Dtos/Customers/CustomerOpenApiCredentialDtos.cs @@ -41,26 +41,3 @@ public class CustomerOpenApiCredentialAdminSetEnabledInput : CustomerOpenApiCred { public bool Enabled { get; set; } } - -public class CustomerOpenApiWhoAmIInput -{ - public string? AppSecret { get; set; } -} - -public class CustomerOpenApiWhoAmIResult -{ - public int CustomerId { get; set; } - - public string CustomerCode { get; set; } = string.Empty; - - public string CustomerName { get; set; } = string.Empty; -} - -public class CustomerOpenApiEnvelope -{ - public string Code { get; set; } = "200"; - - public string Msg { get; set; } = "成功"; - - public CustomerOpenApiWhoAmIResult? Data { get; set; } -} diff --git a/src/Oms.Application/Dtos/Orders/OpenApiParcelCreateInput.cs b/src/Oms.Application/Dtos/Orders/OpenApiParcelCreateInput.cs new file mode 100644 index 0000000..c48882c --- /dev/null +++ b/src/Oms.Application/Dtos/Orders/OpenApiParcelCreateInput.cs @@ -0,0 +1,17 @@ +namespace Oms.Application.Dtos.Orders; + +/// +/// 开放接口创建订单包裹:在标准下单入参上增加 AppKey/AppSecret(Body,非自定义 Header)。 +/// +public class OpenApiParcelCreateInput : OrderParcelCreateInput +{ + /// + /// 客户开放接口 AppKey。 + /// + public string? AppKey { get; set; } + + /// + /// 客户开放接口 AppSecret。 + /// + public string? AppSecret { get; set; } +} diff --git a/src/Oms.Application/Dtos/Orders/PickupOrderDtos.cs b/src/Oms.Application/Dtos/Orders/PickupOrderDtos.cs index 8828139..ab046bc 100644 --- a/src/Oms.Application/Dtos/Orders/PickupOrderDtos.cs +++ b/src/Oms.Application/Dtos/Orders/PickupOrderDtos.cs @@ -360,15 +360,6 @@ public class PickupCarrierValueFeeOutput public decimal? FeeAmount { get; set; } } -public class PickupOpenApiEnvelope -{ - public string Code { get; set; } = "200"; - - public string Msg { get; set; } = "成功"; - - public PickupOpenApiPushFeeResult? Data { get; set; } -} - public class PickupOpenApiPushFeeResult { public string PickupNo { get; set; } = string.Empty; diff --git a/src/Oms.Application/Services/Customers/CustomerOpenApiService.cs b/src/Oms.Application/Services/Customers/CustomerOpenApiService.cs deleted file mode 100644 index 3b44e75..0000000 --- a/src/Oms.Application/Services/Customers/CustomerOpenApiService.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Fake.Application; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Oms.Application.Dtos.Customers; -using Oms.Application.Shared; - -namespace Oms.Application.Services.Customers; - -/// -/// 客户 OpenAPI 机机入口(AllowAnonymous,AppKey + AppSecret)。 -/// -[ApiExplorerSettings(GroupName = "OMS-Customer")] -public class CustomerOpenApiService : ApplicationService -{ - private CustomerOpenApiAuthHelper AuthHelper => - LazyServiceProvider.GetRequiredService(); - - /// - /// 探针:校验凭证并返回客户身份。 - /// - [AllowAnonymous] - [HttpPost("/openapi/customer/whoami")] - public async Task WhoAmIAsync( - [FromQuery] string? appKey, - [FromHeader(Name = "appSecret")] string? appSecretHeader, - [FromBody] CustomerOpenApiWhoAmIInput? body) - { - try - { - var appSecret = !string.IsNullOrWhiteSpace(appSecretHeader) - ? appSecretHeader - : body?.AppSecret; - var (customer, _) = await AuthHelper.ResolveAsync(appKey, appSecret); - return new CustomerOpenApiEnvelope - { - Code = "200", - Msg = "成功", - Data = new CustomerOpenApiWhoAmIResult - { - CustomerId = customer.Id, - CustomerCode = customer.CustomerCode, - CustomerName = customer.CustomerName - } - }; - } - catch (BusinessException ex) - { - return Fail(ex.Message); - } - catch (Exception ex) - { - return Fail(string.IsNullOrWhiteSpace(ex.Message) ? "系统异常" : ex.Message, "500"); - } - } - - private static CustomerOpenApiEnvelope Fail(string msg, string code = "401") => - new() - { - Code = code, - Msg = msg, - Data = null - }; -} diff --git a/src/Oms.Application/Services/OpenApi/OpenApiService.cs b/src/Oms.Application/Services/OpenApi/OpenApiService.cs new file mode 100644 index 0000000..189b4ef --- /dev/null +++ b/src/Oms.Application/Services/OpenApi/OpenApiService.cs @@ -0,0 +1,52 @@ +using Fake.Application; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Oms.Application.Dtos.Orders; +using Oms.Application.Services.Orders; +using Oms.Application.Shared; + +namespace Oms.Application.Services.OpenApi; + +/// +/// 对外开放 API(Swagger 文档分组:openApi)。 +/// 客户机机:AppKey/AppSecret 放在 JSON Body;供应商推费由上游 itfx 鉴权,OMS 不再验 key。 +/// +[ApiExplorerSettings(GroupName = "openApi")] +public class OpenApiService : ApplicationService +{ + private CustomerOpenApiAuthHelper AuthHelper => + LazyServiceProvider.GetRequiredService(); + + private PickupOrderFeePushService FeePush => + LazyServiceProvider.GetRequiredService(); + + private OrderService OrderService => + LazyServiceProvider.GetRequiredService(); + + /// + /// 推送揽收件重尺并按当前产品计算应收应付(上游 itfx 鉴权后转发,无需 AppKey)。 + /// + [AllowAnonymous] + [HttpPost("/openapi/pickup-order/push-fee")] + public Task PushFeeAsync([FromBody] PickupOpenApiPushFeeInput input) => + FeePush.PushFeeAsync(input); + + /// + /// 创建订单包裹(客户机机:Body 传 AppKey、AppSecret)。 + /// + [AllowAnonymous] + [HttpPost("/openapi/parcel/create")] + public async Task CreateParcelAsync([FromBody] OpenApiParcelCreateInput input) + { + ArgumentNullException.ThrowIfNull(input); + + var (customer, _) = await AuthHelper.ResolveAsync(input.AppKey, input.AppSecret); + if (input.CustomerId is > 0 && input.CustomerId.Value != customer.Id) + { + throw new BusinessException("凭证无效"); + } + + input.CustomerId = customer.Id; + return await OrderService.CreateParcelAsync(input); + } +} diff --git a/src/Oms.Application/Services/Orders/PickupOpenApiService.cs b/src/Oms.Application/Services/Orders/PickupOpenApiService.cs deleted file mode 100644 index a5a2dd4..0000000 --- a/src/Oms.Application/Services/Orders/PickupOpenApiService.cs +++ /dev/null @@ -1,76 +0,0 @@ -using Fake.Application; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Oms.Application.Dtos.Orders; - -namespace Oms.Application.Services.Orders; - -/// -/// 国内揽收开放接口(承运商推送件重尺)。 -/// -[ApiExplorerSettings(GroupName = "OMS-Order")] -public class PickupOpenApiService : ApplicationService -{ - private PickupOrderFeePushService FeePush => - LazyServiceProvider.GetRequiredService(); - - /// - /// 开放平台路由:method=open.api.openCommon.pushFee,仅校验 appKey。 - /// - [AllowAnonymous] - [HttpPost("/openapi")] - public Task InvokeAsync( - [FromQuery] string? appKey, - [FromQuery] string? method, - [FromBody] PickupOpenApiPushFeeInput? body) => - HandlePushFeeAsync(appKey, method, body); - - /// - /// 推送揽收重量/尺寸并按当前产品计算应收应付。 - /// - [AllowAnonymous] - [HttpPost("/openapi/openCommon/pushFee")] - public Task PushFeeAsync( - [FromQuery] string? appKey, - [FromBody] PickupOpenApiPushFeeInput? body) => - HandlePushFeeAsync(appKey, PickupOrderFeePushService.PushFeeMethod, body); - - private async Task HandlePushFeeAsync( - string? appKey, - string? method, - PickupOpenApiPushFeeInput? body) - { - try - { - if (!string.IsNullOrWhiteSpace(method) - && !string.Equals(method.Trim(), PickupOrderFeePushService.PushFeeMethod, StringComparison.OrdinalIgnoreCase)) - { - return Fail("400", $"不支持的 method:{method}"); - } - - var data = await FeePush.PushFeeAsync(appKey, body); - return new PickupOpenApiEnvelope - { - Code = "200", - Msg = "成功", - Data = data - }; - } - catch (BusinessException ex) - { - return Fail("400", ex.Message); - } - catch (Exception ex) - { - return Fail("500", string.IsNullOrWhiteSpace(ex.Message) ? "系统异常" : ex.Message); - } - } - - private static PickupOpenApiEnvelope Fail(string code, string msg) => - new() - { - Code = code, - Msg = msg, - Data = null - }; -} diff --git a/src/Oms.Application/Services/Orders/PickupOrderFeePushService.cs b/src/Oms.Application/Services/Orders/PickupOrderFeePushService.cs index b1442c0..37fcafc 100644 --- a/src/Oms.Application/Services/Orders/PickupOrderFeePushService.cs +++ b/src/Oms.Application/Services/Orders/PickupOrderFeePushService.cs @@ -2,7 +2,6 @@ using Fake.Application; using Fake.DependencyInjection; using Oms.Application.Dtos.Channels; using Oms.Application.Dtos.Orders; -using Oms.Application.Integrations.Itfx; using Oms.Application.Services.Orders.Issues; using Oms.Application.Shared; using Oms.Domain.ChannelsAggregate.Entities; @@ -10,7 +9,6 @@ using Oms.Domain.ChannelsAggregate.Repositories; using Oms.Domain.OrdersAggregate.Consts; using Oms.Domain.OrdersAggregate.Entities; using Oms.Domain.OrdersAggregate.Repositories; -using Oms.Domain.SystemAggregate.Consts; using Oms.Domain.SystemAggregate.Repositories; namespace Oms.Application.Services.Orders; @@ -20,51 +18,34 @@ namespace Oms.Application.Services.Orders; /// public class PickupOrderFeePushService : IScopedDependency { - public const string PushFeeMethod = "open.api.openCommon.pushFee"; - private readonly IPickupOrderRepository _pickupOrderRepository; - private readonly ISystemOtherConfigRepository _otherConfigRepository; private readonly ISalesProductRepository _salesProductRepository; private readonly IServiceChannelRepository _serviceChannelRepository; - private readonly IServiceChannelGroupRepository _serviceChannelGroupRepository; private readonly IWeightRuleRepository _weightRuleRepository; private readonly IWeightRoundingRepository _weightRoundingRepository; private readonly IKpiAreaRepository _kpiAreaRepository; private readonly IWarehouseAddressRepository _warehouseAddressRepository; - private readonly IItfxMetadataClient _itfxMetadataClient; public PickupOrderFeePushService( IPickupOrderRepository pickupOrderRepository, - ISystemOtherConfigRepository otherConfigRepository, ISalesProductRepository salesProductRepository, IServiceChannelRepository serviceChannelRepository, - IServiceChannelGroupRepository serviceChannelGroupRepository, IWeightRuleRepository weightRuleRepository, IWeightRoundingRepository weightRoundingRepository, IKpiAreaRepository kpiAreaRepository, - IWarehouseAddressRepository warehouseAddressRepository, - IItfxMetadataClient itfxMetadataClient) + IWarehouseAddressRepository warehouseAddressRepository) { _pickupOrderRepository = pickupOrderRepository; - _otherConfigRepository = otherConfigRepository; _salesProductRepository = salesProductRepository; _serviceChannelRepository = serviceChannelRepository; - _serviceChannelGroupRepository = serviceChannelGroupRepository; _weightRuleRepository = weightRuleRepository; _weightRoundingRepository = weightRoundingRepository; _kpiAreaRepository = kpiAreaRepository; _warehouseAddressRepository = warehouseAddressRepository; - _itfxMetadataClient = itfxMetadataClient; } - public async Task PushFeeAsync(string? appKey, PickupOpenApiPushFeeInput? input) + public async Task PushFeeAsync(PickupOpenApiPushFeeInput? input) { - var key = (appKey ?? string.Empty).Trim(); - if (string.IsNullOrWhiteSpace(key)) - { - throw new BusinessException("appKey 不能为空"); - } - if (input == null) { throw new BusinessException("请求体不能为空"); @@ -90,8 +71,6 @@ public class PickupOrderFeePushService : IScopedDependency var pickup = await _pickupOrderRepository.GetByCarrierWaybillNoAsync(carrierWaybillNo) ?? throw new BusinessException($"揽收单不存在:承运运单号 {carrierWaybillNo}"); - await EnsureAppKeyAsync(key, pickup); - if (pickup.PayStatus == PickupPayStatusCodes.Paid) { throw new BusinessException("揽收单已支付,拒绝修改件重尺与费用"); @@ -240,67 +219,6 @@ public class PickupOrderFeePushService : IScopedDependency }; } - private async Task EnsureAppKeyAsync(string appKey, PickupOrder pickup) - { - var config = await _otherConfigRepository.GetByConfigCodeAsync(SystemOtherConfigCodes.PickupOpenApi); - if (config != null && config.Enabled && !string.IsNullOrWhiteSpace(config.AppKey)) - { - if (!string.Equals(config.AppKey.Trim(), appKey, StringComparison.Ordinal)) - { - throw new BusinessException("appKey 不正确"); - } - - return; - } - - var expected = await ResolvePickupAppKeyAsync(pickup); - if (string.IsNullOrWhiteSpace(expected) - || !string.Equals(expected, appKey, StringComparison.Ordinal)) - { - throw new BusinessException("appKey 不正确"); - } - } - - private async Task ResolvePickupAppKeyAsync(PickupOrder pickup) - { - if (string.IsNullOrWhiteSpace(pickup.CarrierService)) - { - return null; - } - - var product = await _salesProductRepository.GetByProductCodeAsync(pickup.CarrierService.Trim()); - if (product == null) - { - return null; - } - - var providers = await _salesProductRepository.GetAllocationProvidersAsync(product.Id); - var channelId = providers - .Where(x => x.ServiceChannelId is > 0) - .OrderBy(x => x.SerialNumber) - .Select(x => x.ServiceChannelId!.Value) - .FirstOrDefault(); - if (channelId <= 0) - { - return null; - } - - var channel = await _serviceChannelRepository.FirstOrDefaultAsync(x => x.Id == channelId); - if (channel?.GroupId is not > 0) - { - return null; - } - - var group = await _serviceChannelGroupRepository.FirstOrDefaultAsync(x => x.Id == channel.GroupId.Value); - if (group == null || group.AppId <= 0) - { - return null; - } - - var app = await _itfxMetadataClient.GetAppAsync(group.AppId); - return app?.AppKey?.Trim(); - } - private async Task<(decimal VolumetricWeight, decimal ChargeableWeight)> CalculateChargeableAsync( SalesProduct product, OrderWeight weight) diff --git a/src/Oms.Application/Services/System/SystemOtherConfigService.cs b/src/Oms.Application/Services/System/SystemOtherConfigService.cs index c25a817..b213f87 100644 --- a/src/Oms.Application/Services/System/SystemOtherConfigService.cs +++ b/src/Oms.Application/Services/System/SystemOtherConfigService.cs @@ -190,10 +190,7 @@ public class SystemOtherConfigService : ApplicationService } else if (string.Equals(configCode, SystemOtherConfigCodes.PickupOpenApi, StringComparison.OrdinalIgnoreCase)) { - if (string.IsNullOrWhiteSpace(input.AppKey)) - { - throw new BusinessException("揽收开放接口 AppKey 不能为空"); - } + // 鉴权已改由 itfx 上游完成;本配置不再强制 AppKey } else if (string.Equals(configCode, SystemOtherConfigCodes.WeChatMp, StringComparison.OrdinalIgnoreCase)) { diff --git a/src/Oms.Domain/SystemAggregate/Consts/SystemOtherConfigCodes.cs b/src/Oms.Domain/SystemAggregate/Consts/SystemOtherConfigCodes.cs index 81ad7c1..02f0bd1 100644 --- a/src/Oms.Domain/SystemAggregate/Consts/SystemOtherConfigCodes.cs +++ b/src/Oms.Domain/SystemAggregate/Consts/SystemOtherConfigCodes.cs @@ -56,7 +56,7 @@ public static class SystemOtherConfigCodes public const string ApiCenter = "api_center"; /// - /// 国内揽收服务商推送件重尺/费用:仅校验 AppKey(暂不验签)。 + /// 国内揽收服务商推送件重尺/费用(历史配置;鉴权已改由 itfx 上游应用完成,OMS 不再校验 AppKey)。 /// public const string PickupOpenApi = "pickup_open_api"; }