From 7dcbb96d4f7168e9d5f449a186423b9cc827982a Mon Sep 17 00:00:00 2001 From: "DESKTOP-G961P6V\\Zhh" <839860190@qq.com> Date: Fri, 24 May 2024 03:39:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=93=E6=A3=80=E6=8A=A5=E5=91=8A=E4=BC=A0?= =?UTF-8?q?=E8=BE=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebApiOutDto.cs | 11 +++ .../TransToWebPeisAppService.cs | 83 ++++++++++++++++++- .../TransToWebPeisAppServiceTest.cs | 43 ++++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 src/Shentun.Peis.Application.Contracts/WebApiOutDto.cs create mode 100644 test/Shentun.Peis.Application.Tests/TransToWebPeisAppServiceTest.cs diff --git a/src/Shentun.Peis.Application.Contracts/WebApiOutDto.cs b/src/Shentun.Peis.Application.Contracts/WebApiOutDto.cs new file mode 100644 index 0000000..08db698 --- /dev/null +++ b/src/Shentun.Peis.Application.Contracts/WebApiOutDto.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Shentun.Peis +{ + public class WebApiOutDto: WebApiOutDtoBase + { + public object Data { get; set; } + } +} diff --git a/src/Shentun.Peis.Application/TransToWebPeis/TransToWebPeisAppService.cs b/src/Shentun.Peis.Application/TransToWebPeis/TransToWebPeisAppService.cs index a021b5e..e28a3a0 100644 --- a/src/Shentun.Peis.Application/TransToWebPeis/TransToWebPeisAppService.cs +++ b/src/Shentun.Peis.Application/TransToWebPeis/TransToWebPeisAppService.cs @@ -1,8 +1,11 @@ -using Microsoft.AspNetCore.Authorization; +using Azure.Core; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json; using Org.BouncyCastle.Utilities; using Shentun.Peis.CustomerOrgs; using Shentun.Peis.DataMigrations; @@ -19,6 +22,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net.Http.Headers; +using System.Net.Http; using System.Text; using System.Threading.Tasks; using Volo.Abp; @@ -164,8 +169,23 @@ namespace Shentun.Peis.TransToWebPeis public async Task UploadPeisReportAsync(UploadPeisReportIuputDto input) { //同步数据 - await TransPatientRegisterByPatientRegisterIdAsync(new PatientRegisterIdInputDto { PatientRegisterId = input.PatientRegisterId }); + //await TransPatientRegisterByPatientRegisterIdAsync(new PatientRegisterIdInputDto { PatientRegisterId = input.PatientRegisterId }); //上传报告 + var thirdInterfaces = await _thirdInterfaceRepository.GetListAsync(o => o.ThirdInterfaceType == + ThirdInterfaceTypeFlag.TranToWebPeis); + foreach (var thirdInterface in thirdInterfaces) + { + var parmValue = thirdInterface.ParmValue; + var configurationBuilder = new ConfigurationBuilder() + .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); + var interfaceConfig = configurationBuilder.Build(); + var url = interfaceConfig.GetSection("Interface").GetSection("Url").Value; + var user = interfaceConfig.GetSection("Interface").GetSection("User").Value; + var password = interfaceConfig.GetSection("Interface").GetSection("Password").Value; + + var result = await CallAppServiceAsync(url, "", input); + + } } @@ -246,6 +266,65 @@ namespace Shentun.Peis.TransToWebPeis return interfaceConnctionStr; } + private string GetUrl(string parmValue) + { + var configurationBuilder = new ConfigurationBuilder() + .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); + var interfaceConfig = configurationBuilder.Build(); + var url = interfaceConfig.GetSection("Interface").GetSection("Url").Value; + + return url; + } + public async Task CallAppServiceAsync(string appBaseAddress,string url, TInput data) + { + string baseAddress = appBaseAddress; + + using (var httpClientHandler = new HttpClientHandler()) + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + httpClient.BaseAddress = new Uri(baseAddress); + + httpClient.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型 + //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken); + string sendData = ""; + if (data != null) + { + IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); + timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; + sendData = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat); + } + + using (HttpContent httpContent = new StringContent(sendData)) + { + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + HttpResponseMessage response = await httpClient.PostAsync(url, httpContent); + string result; + if (!response.IsSuccessStatusCode) + { + result = response.Content.ReadAsStringAsync().Result; + throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); + } + result = await response.Content.ReadAsStringAsync(); + + var resultDto = JsonConvert.DeserializeObject(result); + if (resultDto is WebApiOutDtoBase) + { + var webApiOutDtoBase = resultDto as WebApiOutDtoBase; + if (webApiOutDtoBase.Code == "-1") + { + throw new Exception($"调用服务失败{webApiOutDtoBase.Message}"); + } + } + return resultDto; + } + + } + } + } + + #region 基础数据 diff --git a/test/Shentun.Peis.Application.Tests/TransToWebPeisAppServiceTest.cs b/test/Shentun.Peis.Application.Tests/TransToWebPeisAppServiceTest.cs new file mode 100644 index 0000000..e9018a7 --- /dev/null +++ b/test/Shentun.Peis.Application.Tests/TransToWebPeisAppServiceTest.cs @@ -0,0 +1,43 @@ +using Shentun.Peis.ChargeRequests; +using Shentun.Peis.Models; +using Shentun.Peis.TransToWebPeis; +using Shentun.Peis.TransToWebPeiss; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.Uow; +using Xunit; +using Xunit.Abstractions; + +namespace Shentun.Peis +{ + public class TransToWebPeisAppServiceTest : PeisApplicationTestBase + { + private readonly IRepository _repository; + private readonly TransToWebPeisAppService _appService; + private readonly ITestOutputHelper _output; + private readonly IUnitOfWorkManager _unitOfWorkManager; + public TransToWebPeisAppServiceTest(ITestOutputHelper testOutputHelper) + { + _output = testOutputHelper; + _unitOfWorkManager = GetRequiredService(); + _repository = GetRequiredService>(); + _appService = GetRequiredService (); + } + [Fact] + public async Task UploadPeisReportAsync() + { + var uploadPeisReportIuputDto = new UploadPeisReportIuputDto() + { + PatientRegisterId = new Guid("3a128197-3e61-23d3-1115-aed602ab82a8"), + //ReportBase64 = Shentun.Utilities.FileHelper.ToBase64("E:\\Whitedolphins\\prog20230709\\体检报告.pdf"), + ReportBase64 = Shentun.Utilities.FileHelper.ToBase64("E:\\Whitedolphins\\upload.pdf"), + + }; + await _appService.UploadPeisReportAsync(uploadPeisReportIuputDto); + } + } +}