diff --git a/Shentun.WebPeis.Plugins/AppQueueRegisterPlugIns.cs b/Shentun.WebPeis.Plugins/AppQueueRegisterPlugIns.cs new file mode 100644 index 0000000..dceb13e --- /dev/null +++ b/Shentun.WebPeis.Plugins/AppQueueRegisterPlugIns.cs @@ -0,0 +1,222 @@ +using Azure.Core; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Microsoft.Identity.Client; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using Shentun.Utilities; +using Shentun.WebPeis.PatientRegisters; +using Shentun.WebPeis.PlugIns; +using Shentun.WebPeis.QueueRegisters; +using System; +using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace Shentun.WebPeis.Plugins +{ + public class AppQueueRegisterPlugIns + { + protected IConfiguration? AppConfig; + private string? _appBaseAddress; + private static string? _accesToken; + private string? _appUser; + private string? _appPassword; + private string? _appQueueRegisterUrl; + + public AppQueueRegisterPlugIns() + { + Init(); + } + + public void Init() + { + AppConfig = new ConfigurationBuilder() + .SetBasePath(DirectoryHelper.GetAppDirectory()) // 设置基础路径为当前目录 + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .Build(); + _appBaseAddress = AppConfig.GetSection("Peis") + .GetSection("PeisBaseUrl").Value; + _appUser = AppConfig.GetSection("Peis") + .GetSection("AppUser").Value; + _appPassword = AppConfig.GetSection("Peis") + .GetSection("AppPassWord").Value; + _appQueueRegisterUrl = AppConfig.GetSection("Peis") + .GetSection("AppQueueRegisterUrl").Value; + } + + + /// + /// 小程序获取当前排队信息 + /// + /// + /// + public async Task GetAppQueueRegisterByIdNoAsync(IdNoInputDto input) + { + var appQueueRegisterByPersonIdDtos = await CallAppServiceAsync(_appQueueRegisterUrl, + input); + + return appQueueRegisterByPersonIdDtos; + } + + + public async Task CallAppServiceAsync(string url, TInput data, string method = "post") + { + string baseAddress = _appBaseAddress; + await CheckLoginAsync(); + 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); + IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); + timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; + var sendData = JsonConvert.SerializeObject(data, Formatting.Indented, timeFormat); + using (HttpContent httpContent = new StringContent(sendData)) + { + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + HttpResponseMessage response = null; + if (method == "post") + { + response = await httpClient.PostAsync(url, httpContent); + } + else + { + response = await httpClient.GetAsync(url); + } + + 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 != null) + { + if (resultDto.Code == -1) + { + throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.Message); + } + + return resultDto.Data; + } + return resultDto.Data; + } + + } + } + } + + private async Task CheckLoginAsync() + { + if (string.IsNullOrWhiteSpace(_accesToken)) + { + await LoginAsync(); + } + else + { + var handler = new JwtSecurityTokenHandler(); + var payload = handler.ReadJwtToken(_accesToken).Payload; + var claims = payload.Claims; + var exp = claims.First(claim => claim.Type == "exp").Value; + if (exp == null) + { + await LoginAsync(); + } + else + { + if (long.TryParse(exp, out var expires)) + { + var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime; + if (expireTime <= DateTime.Now) + { + await LoginAsync(); + } + } + else + { + await LoginAsync(); + } + + } + + } + } + + public async Task> LoginAsync() + { + if (string.IsNullOrWhiteSpace(_appUser)) + { + throw new Exception("SelfUser不能为空"); + } + if (string.IsNullOrWhiteSpace(_appPassword)) + { + throw new Exception("SelfPassword不能为空"); + } + var relult = await LoginAsync(_appUser, _appPassword); + return relult; + } + + public async Task> LoginAsync(string userId, string password) + { + if (string.IsNullOrWhiteSpace(userId)) + { + throw new Exception("用户ID不能为空"); + } + if (string.IsNullOrWhiteSpace(password)) + { + throw new Exception("密码不能为空"); + } + using (var httpClientHandler = new HttpClientHandler()) + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + httpClient.BaseAddress = new Uri(_appBaseAddress); + + httpClient.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型 + var url = "api/identity/users/login"; + var loginUser = new LoginInputDto() + { + UserName = userId, + Password = password + }; + var sendData = JsonConvert.SerializeObject(loginUser); + 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 restultDto = JsonConvert.DeserializeObject>(result); + if (restultDto == null) + { + throw new Exception("返回结果是空"); + } + if (restultDto.Code != 1) + { + throw new Exception($"登录失败{restultDto.Message}"); + } + _accesToken = restultDto.Data.access_token; + return restultDto; + } + } + } + } + } +} diff --git a/src/Shentun.WebPeis.Application.Contracts/QueueRegisters/GetAppQueueRegisterByPersonIdDto.cs b/src/Shentun.WebPeis.Application.Contracts/QueueRegisters/GetAppQueueRegisterByPersonIdDto.cs new file mode 100644 index 0000000..b495ba5 --- /dev/null +++ b/src/Shentun.WebPeis.Application.Contracts/QueueRegisters/GetAppQueueRegisterByPersonIdDto.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Shentun.WebPeis.QueueRegisters +{ + public class GetAppQueueRegisterByPersonIdDto + { + /// + /// 姓名 + /// + public string PatientName { get; set; } + + /// + /// 身份证号码 + /// + public string IdNo { get; set; } + + /// + /// 人员条码号 + /// + public string PatientRegisterNo { get; set; } + + /// + /// 性别 + /// + public string SexName { get; set; } + + /// + /// 手机号 + /// + public string MobileTelephone { get; set; } + + /// + /// 婚姻状况 + /// + public string MaritalStatusName { get; set; } + + /// + /// 照片 + /// + public string Photo { get; set; } + + + /// + /// 排队房间名称 + /// + public string RoomName { get; set; } + + + /// + /// 候诊人数 + /// + public int QueueCount { get; set; } + + /// + /// 完成标志 (o=候诊 1=已呼 2=过号) + /// + public char CompleteFlag { get; set; } + } +} diff --git a/src/Shentun.WebPeis.Application.Contracts/QueueRegisters/IdNoInputDto.cs b/src/Shentun.WebPeis.Application.Contracts/QueueRegisters/IdNoInputDto.cs new file mode 100644 index 0000000..fc80312 --- /dev/null +++ b/src/Shentun.WebPeis.Application.Contracts/QueueRegisters/IdNoInputDto.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Shentun.WebPeis.QueueRegisters +{ + public class IdNoInputDto + { + /// + /// 身份证号码 + /// + public string IdNo { get; set; } + } +} diff --git a/src/Shentun.WebPeis.Application/QueueRegisters/QueueRegisterAppService.cs b/src/Shentun.WebPeis.Application/QueueRegisters/QueueRegisterAppService.cs new file mode 100644 index 0000000..6e48d9f --- /dev/null +++ b/src/Shentun.WebPeis.Application/QueueRegisters/QueueRegisterAppService.cs @@ -0,0 +1,52 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Shentun.WebPeis.Models; +using Shentun.WebPeis.PatientRegisters; +using Shentun.WebPeis.Persons; +using Shentun.WebPeis.Plugins; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Services; +using Volo.Abp.Domain.Repositories; + +namespace Shentun.WebPeis.QueueRegisters +{ + /// + /// 获取排队信息 + /// + [ApiExplorerSettings(GroupName = "Work")] + [Authorize] + public class QueueRegisterAppService : ApplicationService + { + private readonly IRepository _personRepository; + public QueueRegisterAppService( + IRepository personRepository + ) + { + _personRepository = personRepository; + } + + /// + /// 小程序获取当前排队信息 + /// + /// + /// + [HttpPost("api/app/QueueRegister/GetAppQueueRegisterByPersonId")] + public async Task GetAppQueueRegisterByPersonIdAsync(PersonIdInputDto input) + { + var msg = new GetAppQueueRegisterByPersonIdDto(); + + //获取身份证号码 + var personEnt = await _personRepository.FirstOrDefaultAsync(f => f.PersonId == input.PersonId); + if (personEnt == null) + throw new UserFriendlyException("人员不存在"); + var appQueueRegisterPlugIns = new AppQueueRegisterPlugIns(); + msg = await appQueueRegisterPlugIns.GetAppQueueRegisterByIdNoAsync(new IdNoInputDto { IdNo = personEnt.IdNo }); + return msg; + } + } +} diff --git a/src/Shentun.WebPeis.Application/Shentun.WebPeis.Application.csproj b/src/Shentun.WebPeis.Application/Shentun.WebPeis.Application.csproj index dfc8f21..1880b44 100644 --- a/src/Shentun.WebPeis.Application/Shentun.WebPeis.Application.csproj +++ b/src/Shentun.WebPeis.Application/Shentun.WebPeis.Application.csproj @@ -11,6 +11,7 @@ + diff --git a/src/Shentun.WebPeis.HttpApi.Host/Shentun.WebPeis.HttpApi.Host.csproj b/src/Shentun.WebPeis.HttpApi.Host/Shentun.WebPeis.HttpApi.Host.csproj index b0a0f12..5c35eb5 100644 --- a/src/Shentun.WebPeis.HttpApi.Host/Shentun.WebPeis.HttpApi.Host.csproj +++ b/src/Shentun.WebPeis.HttpApi.Host/Shentun.WebPeis.HttpApi.Host.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/Shentun.WebPeis.HttpApi.Host/appsettings.json b/src/Shentun.WebPeis.HttpApi.Host/appsettings.json index f7df489..63778f3 100644 --- a/src/Shentun.WebPeis.HttpApi.Host/appsettings.json +++ b/src/Shentun.WebPeis.HttpApi.Host/appsettings.json @@ -68,5 +68,11 @@ }, "Swagger": { "IsEnabled": true + }, + "Peis": { + "PeisBaseUrl": "", + "AppQueueRegisterUrl": "", + "AppUser": "", + "AppPassWord": "" } }