diff --git a/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/IJinkoSapService.cs b/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/IJinkoSapService.cs index bd654f02..80bf045a 100644 --- a/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/IJinkoSapService.cs +++ b/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/IJinkoSapService.cs @@ -2,7 +2,7 @@ namespace Dlhis.ScheduledTask.SyncSapInfo.Services; -public interface IJinkoSapService : IScopeDependency +public interface IJinkoSapService : ISingletonDependency { Task GetSapOrganizationsAsync(); diff --git a/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs b/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs index 7e2eac25..e1abe780 100644 --- a/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs +++ b/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.Net.Http.Headers; +using System.Text; using Dlhis.ScheduledTask.SyncSapInfo.Dto; using Dlhis.ScheduledTask.SyncSapInfo.Response; using Dlhis.Sys.Api; @@ -8,22 +9,35 @@ namespace Dlhis.ScheduledTask.SyncSapInfo.Services.Implements; public class JinkoSapService : IJinkoSapService { - private readonly HttpClient _httpClient; private readonly ILogger _logger; + private readonly IHttpClientFactory _httpClientFactory; private readonly ITenantApi _tenantApi; public JinkoSapService(IHttpClientFactory httpClientFactory, ILogger logger, ITenantApi tenantApi) { - _httpClient = httpClientFactory.CreateClient("jinko:sap"); _logger = logger; + _httpClientFactory = httpClientFactory; _tenantApi = tenantApi; } + private HttpClient GetHttpClient() + { + var httpClient = _httpClientFactory.CreateClient("jinko:sap"); + //http://sappidap.jinkosolar.cn:51000 + httpClient.BaseAddress = new Uri("http://sappipap01.jinkosolar.cn:50100"); + httpClient.Timeout = TimeSpan.FromMinutes(10); + var credentials = string.Format("{0}:{1}", "EHS_USER", "y3R1hd2h*RICAC"); + var headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", headerValue); + + return httpClient; + } + public async Task GetSapOrganizationsAsync() { - string apiUrl = "http://sappidap.jinkosolar.cn:51000/RESTAdapter/Rest/EHR_ORGANIZATION/"; + string apiUrl = "/RESTAdapter/Rest/EHR_ORGANIZATION/"; - var response = await _httpClient.PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json")); + var response = await GetHttpClient().PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); string responseJson = await response.Content.ReadAsStringAsync(); @@ -34,9 +48,9 @@ public class JinkoSapService : IJinkoSapService public async Task GetPositionsAsync() { - string apiUrl = "http://sappidap.jinkosolar.cn:51000/RESTAdapter/Rest/EHR_POSITION/"; + string apiUrl = "/RESTAdapter/Rest/EHR_POSITION/"; - var response = await _httpClient.PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json")); + var response = await GetHttpClient().PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); string responseJson = await response.Content.ReadAsStringAsync(); @@ -48,7 +62,7 @@ public class JinkoSapService : IJinkoSapService public async Task GetPersonsAsync() { - var apiUrl = "http://sappipap01.jinkosolar.cn:50100/RESTAdapter/Rest/EHR_PERSONNEL/"; + var apiUrl = "/RESTAdapter/Rest/EHR_PERSONNEL/"; var tenantResult = await _tenantApi.GetTenantsInternalAsync(); var tenants = tenantResult.Data; @@ -75,7 +89,7 @@ public class JinkoSapService : IJinkoSapService }; var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); - var response = await _httpClient.PostAsync(apiUrl, content); + var response = await GetHttpClient().PostAsync(apiUrl, content); response.EnsureSuccessStatusCode(); var jsonResponse = await response.Content.ReadAsStringAsync();