fix 自定义header

This commit is contained in:
2025-01-13 18:02:00 +08:00
parent 889730b7a3
commit fd052020f5

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Text;
using Dlhis.Entity.Canteen.Enums;
using Dlhis.Middle.Apis;
using Dlhis.Middle.Entities;
@@ -19,7 +20,11 @@ public class OrderService : IOrderService
private readonly ICanteenSettingService _canteenSettingService;
private readonly IRemoteOrderService _remoteOrderService;
private readonly ICanteenApi _canteenApi;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<OrderService> _logger;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ITokenService _tokenService;
private readonly IConfiguration _configuration;
private readonly string _factory;
private readonly IDbContext _dbContext;
private Dictionary<CanteenSettingItemEnumType, string> _settings;
@@ -28,13 +33,21 @@ public class OrderService : IOrderService
IRemoteOrderService remoteOrderService,
ISettingService settingService,
ICanteenApi canteenApi,
IHttpClientFactory httpClientFactory,
ILogger<OrderService> logger,
IHttpContextAccessor httpContextAccessor,
ITokenService tokenService,
IConfiguration configuration,
IDbContext dbContext)
{
_canteenSettingService = canteenSettingService;
_remoteOrderService = remoteOrderService;
_canteenApi = canteenApi;
_httpClientFactory = httpClientFactory;
_logger = logger;
_httpContextAccessor = httpContextAccessor;
_tokenService = tokenService;
_configuration = configuration;
_factory = settingService.GetFactories().Join(",");
_dbContext = dbContext;
}
@@ -47,7 +60,20 @@ public class OrderService : IOrderService
{
if (await _remoteOrderService.AbleRemoteCallAsync())
{
var remoteResult = await _canteenApi.AddAsync(request);
var httpClient = _httpClientFactory.CreateClient();
httpClient.BaseAddress = new Uri(_configuration.GetSection("BaseUrl").Value);
if (_httpContextAccessor.HttpContext!.Request.Headers.TryGetValue("X-Requested-DeviceIp", out var ip))
{
httpClient.DefaultRequestHeaders.Add("X-Requested-DeviceIp", ip.ToString());
}
var token = await _tokenService.AuthAsync();
httpClient.DefaultRequestHeaders.Add("Authorization", token);
var responseMessage = await httpClient.PostAsync("/api/canteen/orders", new StringContent(request.ToJson(), Encoding.UTF8, "application/json"));
var responseJson = await responseMessage.Content.ReadAsStringAsync();
var remoteResult = responseJson.ToObject<DefaultApiResult<OrderResultResponse>>();
//var remoteResult = await _canteenApi.AddAsync(request);
if (remoteResult.Code == ApiStateCode.Success)
{
var d = remoteResult.Data;