You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
11 KiB
243 lines
11 KiB
using Azure.Core;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
using Newtonsoft.Json.Linq;
|
|
using Shentun.Peis.AppointPatientRegisters;
|
|
using Shentun.Peis.AppointRegisterAsbitems;
|
|
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.Peis.PlugIns
|
|
{
|
|
public class WebAppointWebPeisPlugIns : WebAppointPlugInsBase
|
|
{
|
|
private string _webPeisUser;
|
|
private string _webPeisPassword;
|
|
private string _webPeisBaseAddress;
|
|
private string _webPeisToken;
|
|
private string _webPeisGetAppointPatientRegisterListByFilterUrl;
|
|
private string _webPeisGetAppointRegisterAsbitemListByIdUrl;
|
|
private Guid _medicalCenterId;
|
|
public WebAppointWebPeisPlugIns(Guid thirdInterfaceId) : base(thirdInterfaceId)
|
|
{
|
|
var configurationBuilder = new ConfigurationBuilder()
|
|
.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(_thirdInterfaceDto.ParmValue)));
|
|
InterfaceConfig = configurationBuilder.Build();
|
|
|
|
var medicalCenterIdStr = InterfaceConfig.GetSection("Interface").GetSection("MedicalCenterId").Value;
|
|
if (!Guid.TryParse(medicalCenterIdStr, out _medicalCenterId))
|
|
{
|
|
throw new Exception("体检中心ID格式不正确");
|
|
}
|
|
_webPeisUser = InterfaceConfig.GetSection("Interface").GetSection("User").Value;
|
|
_webPeisPassword = InterfaceConfig.GetSection("Interface").GetSection("Password").Value;
|
|
_webPeisBaseAddress = InterfaceConfig.GetSection("Interface").GetSection("BaseAddress").Value;
|
|
_webPeisGetAppointPatientRegisterListByFilterUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointPatientRegisterListByFilterUrl").Value;
|
|
_webPeisGetAppointRegisterAsbitemListByIdUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointRegisterAsbitemListByIdUrl").Value;
|
|
}
|
|
/// <summary>
|
|
/// 获取预约人员列表
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public override async Task<List<AppointPatientRegisterDto>> GetAppointPatientRegisterListByFilterAsync(AppointPatientRegisterInputDto input)
|
|
{
|
|
var medicalCenterId = _medicalCenterId;
|
|
var webAppointPatientRegisterInput = new WebAppointPatientRegisterInput()
|
|
{
|
|
MedicalCenterId = medicalCenterId,
|
|
IdNo = input.IdNo,
|
|
MobilePhone = input.MobilePhone,
|
|
AppointStartDate = input.AppointStartDate,
|
|
AppointStopDate = input.AppointStopDate,
|
|
};
|
|
var appointPatientRegisterDtos = await CallWePeisAppServiceAsync<WebAppointPatientRegisterInput, List<AppointPatientRegisterDto>>(_webPeisGetAppointPatientRegisterListByFilterUrl,
|
|
webAppointPatientRegisterInput);
|
|
return appointPatientRegisterDtos;
|
|
|
|
}
|
|
/// <summary>
|
|
/// 获取组合项目列表
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public override async Task<List<AppointRegisterAsbitemDto>> GetAppointRegisterAsbitemListByIdAsync(AppointPatientRegisterIdInputDto input)
|
|
{
|
|
var webAppointPatientRegisterIdInput = new WebAppointPatientRegisterIdInput()
|
|
{
|
|
AppointPatientRegisterId = new Guid(input.AppointPatientRegisterId)
|
|
};
|
|
var appointRegisterAsbitemDtos = await CallWePeisAppServiceAsync<WebAppointPatientRegisterIdInput,
|
|
List<AppointRegisterAsbitemDto>>
|
|
(_webPeisGetAppointRegisterAsbitemListByIdUrl, webAppointPatientRegisterIdInput);
|
|
return appointRegisterAsbitemDtos;
|
|
}
|
|
public async virtual Task<WebApiOutDtoExter<LoginOutDataDto>> LoginWebPeisAsync()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_webPeisUser))
|
|
{
|
|
throw new Exception("WebPeisUser不能为空");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(_webPeisPassword))
|
|
{
|
|
throw new Exception("WebPeisPassword不能为空");
|
|
}
|
|
var relult = await LoginWebPeisAsync(_webPeisUser, _webPeisPassword);
|
|
return relult;
|
|
}
|
|
|
|
public async Task<WebApiOutDtoExter<LoginOutDataDto>> LoginWebPeisAsync(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(_webPeisBaseAddress);
|
|
|
|
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<WebApiOutDtoExter<LoginOutDataDto>>(result);
|
|
if (restultDto == null)
|
|
{
|
|
throw new Exception("返回结果是空");
|
|
}
|
|
if (restultDto.Code != 1)
|
|
{
|
|
throw new Exception($"登录失败{restultDto.Message}");
|
|
}
|
|
_webPeisToken = restultDto.Data.access_token;
|
|
return restultDto;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<TOut> CallWePeisAppServiceAsync<TInput, TOut>(string url, TInput data, string method = "post")
|
|
{
|
|
//if (string.IsNullOrWhiteSpace(_webPeisBaseAddress))
|
|
//{
|
|
// throw new Exception("_webPeisBaseAddress不能为空");
|
|
//}
|
|
string baseAddress = _webPeisBaseAddress;
|
|
await CheckWebPeisLoginAsync();
|
|
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", _webPeisToken);
|
|
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
|
|
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
var sendData = JsonConvert.SerializeObject(data, Newtonsoft.Json.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<WebApiOutDtoExter<TOut>>(result);
|
|
if (resultDto != null)
|
|
{
|
|
if (resultDto.Code == -1)
|
|
{
|
|
throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.Message);
|
|
}
|
|
|
|
return resultDto.Data;
|
|
}
|
|
return resultDto.Data;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task CheckWebPeisLoginAsync()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_webPeisToken))
|
|
{
|
|
await LoginWebPeisAsync();
|
|
}
|
|
else
|
|
{
|
|
var handler = new JwtSecurityTokenHandler();
|
|
var payload = handler.ReadJwtToken(_webPeisToken).Payload;
|
|
var claims = payload.Claims;
|
|
var exp = claims.First(claim => claim.Type == "exp").Value;
|
|
if (exp == null)
|
|
{
|
|
await LoginWebPeisAsync();
|
|
}
|
|
else
|
|
{
|
|
if (long.TryParse(exp, out var expires))
|
|
{
|
|
var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime;
|
|
if (expireTime <= DateTime.Now)
|
|
{
|
|
await LoginAsync();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
await LoginWebPeisAsync();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|