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

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using Azure.Core;
  2. using Microsoft.Extensions.Configuration;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Converters;
  5. using Newtonsoft.Json.Linq;
  6. using Shentun.Peis.AppointPatientRegisters;
  7. using Shentun.Peis.AppointRegisterAsbitems;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IdentityModel.Tokens.Jwt;
  11. using System.Linq;
  12. using System.Net.Http.Headers;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace Shentun.Peis.PlugIns
  16. {
  17. public class WebAppointWebPeisPlugIns : WebAppointPlugInsBase
  18. {
  19. private string _webPeisUser;
  20. private string _webPeisPassword;
  21. private string _webPeisBaseAddress;
  22. private string _webPeisToken;
  23. private string _webPeisGetAppointPatientRegisterListByFilterUrl;
  24. private string _webPeisGetAppointRegisterAsbitemListByIdUrl;
  25. private Guid _medicalCenterId;
  26. public WebAppointWebPeisPlugIns(Guid thirdInterfaceId) : base(thirdInterfaceId)
  27. {
  28. var configurationBuilder = new ConfigurationBuilder()
  29. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(_thirdInterfaceDto.ParmValue)));
  30. InterfaceConfig = configurationBuilder.Build();
  31. var medicalCenterIdStr = InterfaceConfig.GetSection("Interface").GetSection("MedicalCenterId").Value;
  32. if (!Guid.TryParse(medicalCenterIdStr, out _medicalCenterId))
  33. {
  34. throw new Exception("体检中心ID格式不正确");
  35. }
  36. _webPeisUser = InterfaceConfig.GetSection("Interface").GetSection("User").Value;
  37. _webPeisPassword = InterfaceConfig.GetSection("Interface").GetSection("Password").Value;
  38. _webPeisBaseAddress = InterfaceConfig.GetSection("Interface").GetSection("BaseAddress").Value;
  39. _webPeisGetAppointPatientRegisterListByFilterUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointPatientRegisterListByFilterUrl").Value;
  40. _webPeisGetAppointRegisterAsbitemListByIdUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointRegisterAsbitemListByIdUrl").Value;
  41. }
  42. /// <summary>
  43. /// 获取预约人员列表
  44. /// </summary>
  45. /// <param name="input"></param>
  46. /// <returns></returns>
  47. public override async Task<List<AppointPatientRegisterDto>> GetAppointPatientRegisterListByFilterAsync(AppointPatientRegisterInputDto input)
  48. {
  49. var medicalCenterId = _medicalCenterId;
  50. var webAppointPatientRegisterInput = new WebAppointPatientRegisterInput()
  51. {
  52. MedicalCenterId = medicalCenterId,
  53. IdNo = input.IdNo,
  54. MobilePhone = input.MobilePhone,
  55. AppointStartDate = input.AppointStartDate,
  56. AppointStopDate = input.AppointStopDate,
  57. };
  58. var appointPatientRegisterDtos = await CallWePeisAppServiceAsync<WebAppointPatientRegisterInput, List<AppointPatientRegisterDto>>(_webPeisGetAppointPatientRegisterListByFilterUrl,
  59. webAppointPatientRegisterInput);
  60. return appointPatientRegisterDtos;
  61. }
  62. /// <summary>
  63. /// 获取组合项目列表
  64. /// </summary>
  65. /// <param name="input"></param>
  66. /// <returns></returns>
  67. public override async Task<List<AppointRegisterAsbitemDto>> GetAppointRegisterAsbitemListByIdAsync(AppointPatientRegisterIdInputDto input)
  68. {
  69. var webAppointPatientRegisterIdInput = new WebAppointPatientRegisterIdInput()
  70. {
  71. AppointPatientRegisterId = new Guid(input.AppointPatientRegisterId)
  72. };
  73. var appointRegisterAsbitemDtos = await CallWePeisAppServiceAsync<WebAppointPatientRegisterIdInput,
  74. List<AppointRegisterAsbitemDto>>
  75. (_webPeisGetAppointRegisterAsbitemListByIdUrl, webAppointPatientRegisterIdInput);
  76. return appointRegisterAsbitemDtos;
  77. }
  78. public async virtual Task<WebApiOutDtoExter<LoginOutDataDto>> LoginWebPeisAsync()
  79. {
  80. if (string.IsNullOrWhiteSpace(_webPeisUser))
  81. {
  82. throw new Exception("WebPeisUser不能为空");
  83. }
  84. if (string.IsNullOrWhiteSpace(_webPeisPassword))
  85. {
  86. throw new Exception("WebPeisPassword不能为空");
  87. }
  88. var relult = await LoginWebPeisAsync(_webPeisUser, _webPeisPassword);
  89. return relult;
  90. }
  91. public async Task<WebApiOutDtoExter<LoginOutDataDto>> LoginWebPeisAsync(string userId, string password)
  92. {
  93. if (string.IsNullOrWhiteSpace(userId))
  94. {
  95. throw new Exception("用户ID不能为空");
  96. }
  97. if (string.IsNullOrWhiteSpace(password))
  98. {
  99. throw new Exception("密码不能为空");
  100. }
  101. using (var httpClientHandler = new HttpClientHandler())
  102. {
  103. using (var httpClient = new HttpClient(httpClientHandler))
  104. {
  105. httpClient.BaseAddress = new Uri(_webPeisBaseAddress);
  106. httpClient.DefaultRequestHeaders.Accept.Add(
  107. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  108. var url = "api/identity/users/login";
  109. var loginUser = new LoginInputDto()
  110. {
  111. UserName = userId,
  112. Password = password
  113. };
  114. var sendData = JsonConvert.SerializeObject(loginUser);
  115. using (HttpContent httpContent = new StringContent(sendData))
  116. {
  117. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  118. HttpResponseMessage response = await httpClient.PostAsync(url, httpContent);
  119. string result;
  120. if (!response.IsSuccessStatusCode)
  121. {
  122. result = response.Content.ReadAsStringAsync().Result;
  123. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  124. }
  125. result = await response.Content.ReadAsStringAsync();
  126. var restultDto = JsonConvert.DeserializeObject<WebApiOutDtoExter<LoginOutDataDto>>(result);
  127. if (restultDto == null)
  128. {
  129. throw new Exception("返回结果是空");
  130. }
  131. if (restultDto.Code != 1)
  132. {
  133. throw new Exception($"登录失败{restultDto.Message}");
  134. }
  135. _webPeisToken = restultDto.Data.access_token;
  136. return restultDto;
  137. }
  138. }
  139. }
  140. }
  141. public async Task<TOut> CallWePeisAppServiceAsync<TInput, TOut>(string url, TInput data, string method = "post")
  142. {
  143. //if (string.IsNullOrWhiteSpace(_webPeisBaseAddress))
  144. //{
  145. // throw new Exception("_webPeisBaseAddress不能为空");
  146. //}
  147. string baseAddress = _webPeisBaseAddress;
  148. await CheckWebPeisLoginAsync();
  149. using (var httpClientHandler = new HttpClientHandler())
  150. {
  151. using (var httpClient = new HttpClient(httpClientHandler))
  152. {
  153. httpClient.BaseAddress = new Uri(baseAddress);
  154. httpClient.DefaultRequestHeaders.Accept.Add(
  155. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  156. httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _webPeisToken);
  157. IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
  158. timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
  159. var sendData = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat);
  160. using (HttpContent httpContent = new StringContent(sendData))
  161. {
  162. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  163. HttpResponseMessage response = null;
  164. if (method == "post")
  165. {
  166. response = await httpClient.PostAsync(url, httpContent);
  167. }
  168. else
  169. {
  170. response = await httpClient.GetAsync(url);
  171. }
  172. string result;
  173. if (!response.IsSuccessStatusCode)
  174. {
  175. result = response.Content.ReadAsStringAsync().Result;
  176. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  177. }
  178. result = await response.Content.ReadAsStringAsync();
  179. var resultDto = JsonConvert.DeserializeObject<WebApiOutDtoExter<TOut>>(result);
  180. if (resultDto != null)
  181. {
  182. if (resultDto.Code == -1)
  183. {
  184. throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.Message);
  185. }
  186. return resultDto.Data;
  187. }
  188. return resultDto.Data;
  189. }
  190. }
  191. }
  192. }
  193. private async Task CheckWebPeisLoginAsync()
  194. {
  195. if (string.IsNullOrWhiteSpace(_webPeisToken))
  196. {
  197. await LoginWebPeisAsync();
  198. }
  199. else
  200. {
  201. var handler = new JwtSecurityTokenHandler();
  202. var payload = handler.ReadJwtToken(_webPeisToken).Payload;
  203. var claims = payload.Claims;
  204. var exp = claims.First(claim => claim.Type == "exp").Value;
  205. if (exp == null)
  206. {
  207. await LoginWebPeisAsync();
  208. }
  209. else
  210. {
  211. if (long.TryParse(exp, out var expires))
  212. {
  213. var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime;
  214. if (expireTime <= DateTime.Now)
  215. {
  216. await LoginAsync();
  217. }
  218. }
  219. else
  220. {
  221. await LoginWebPeisAsync();
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }