heshaofeng
2025-12-29 266e4bf654c55ce2f7e9271048e4625f1b8b49f6
项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_BasicService/ESSApiService.cs
@@ -8,6 +8,8 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Core.Helper;
using WIDESEA_Core.LogHelper;
using WIDESEA_DTO.Basic;
using WIDESEA_IBasicService;
@@ -31,17 +33,22 @@
            try
            {
                var url = "conveyor/moveContainer";
                _logger.LogInformation("ESSApiService MoveContainerAsync Request:  " + JsonConvert.SerializeObject(request));
                var result = await PostAsync<MoveContainerRequest, ApiResponse<string>>(url, request);
                if (result != null && result.Code == 0)
                {
                    //{"code":0,"msg":"success","data":{"107":"TASK_ALREADY_EXIST"}}
                    if (result.Data.Contains("TASK_ALREADY_EXIST"))
                    {
                        return false;
                    }
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                _logger.LogInformation("瀹瑰櫒娴佸姩澶辫触:  " + ex.Message);
                _logger.LogInformation("ESSApiService 瀹瑰櫒娴佸姩澶辫触:  " + ex.Message);
                return false;
            }
        }
@@ -55,47 +62,63 @@
        {
            try
            {
                _logger.LogInformation("鍒涘缓浠诲姟Request:  " + JsonConvert.SerializeObject(request));
                _logger.LogInformation("ESSApiService 鍒涘缓浠诲姟Request:  " + JsonConvert.SerializeObject(request));
                var url = "task/create";
                var result = await PostAsync<TaskModel, ApiResponse<TasksData>>(url, request);
                if (result != null && result.Code == 0)
                {
                    _logger.LogInformation(result.Serialize());
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                _logger.LogInformation("鍒涘缓浠诲姟澶辫触:  " + ex.Message);
                _logger.LogInformation("ESSApiService 鍒涘缓浠诲姟澶辫触:  " + ex.Message);
                return false;
            }
        }
        private async Task<TResponse> PostAsync<TRequest, TResponse>(string url, TRequest request)
        {
            string json = JsonConvert.SerializeObject(request, new JsonSerializerSettings
            TResponse response1 = (TResponse)Activator.CreateInstance(typeof(TResponse));
            try
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            });
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var _client = _httpClientFactory.CreateClient("ESSUrl");
            _client.DefaultRequestHeaders.Clear();
                string json = JsonConvert.SerializeObject(request, new JsonSerializerSettings
                {
                    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                });
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                var _client = _httpClientFactory.CreateClient("ESSUrl");
                _client.DefaultRequestHeaders.Clear();
            _client.DefaultRequestHeaders.Add("Accept", "application/json");
                _client.DefaultRequestHeaders.Add("Accept", "application/json");
            using var response = await _client.PostAsync(url, content);
            string body = await response.Content.ReadAsStringAsync();
            _logger.LogInformation($"ESSAPI post :  {_client.BaseAddress}  {url}   {body}");
            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException(body);
                using var response = await _client.PostAsync(url, content);
                string body = await response.Content.ReadAsStringAsync();
                _logger.LogInformation($"ESSAPI post :  {_client.BaseAddress}  {url}   {body}");
                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpRequestException(body);
                }
                response1 = JsonConvert.DeserializeObject<TResponse>(body);
                return response1;
            }
            return JsonConvert.DeserializeObject<TResponse>(body);
            catch (Exception ex)
            {
                Logger.Add(request == null ? "" : JsonConvert.SerializeObject(request), response1 == null ? ex.ToString() : JsonConvert.SerializeObject(response1));
                throw new Exception(ex.Message);
            }
            finally
            {
                Logger.Add(request == null ? "" : JsonConvert.SerializeObject(request), response1 == null ? "" : JsonConvert.SerializeObject(response1));
            }
        }
    }