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.

255 lines
10 KiB

1 year ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
11 months ago
11 months ago
1 year ago
  1. using Azure.Core;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.Logging;
  4. using Microsoft.Identity.Client;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Converters;
  7. using Shentun.Utilities;
  8. using Shentun.WebPeis.AppointPatientRegisters;
  9. using Shentun.WebPeis.PatientRegisters;
  10. using Shentun.WebPeis.PlugIns;
  11. using Shentun.WebPeis.QueueRegisters;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IdentityModel.Tokens.Jwt;
  15. using System.Linq;
  16. using System.Net.Http.Headers;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. namespace Shentun.WebPeis.Plugins
  20. {
  21. public class AppQueueRegisterPlugIns
  22. {
  23. protected IConfiguration? AppConfig;
  24. private string? _appBaseAddress;
  25. private static string? _accesToken;
  26. private string? _appUser;
  27. private string? _appPassword;
  28. private string? _appQueueRegisterUrl;
  29. private string? _appIsPeisRegisterUrl;
  30. private string? _appUpdateAppointPatientAsbitemStatusUrl;
  31. public AppQueueRegisterPlugIns()
  32. {
  33. Init();
  34. }
  35. public void Init()
  36. {
  37. AppConfig = new ConfigurationBuilder()
  38. .SetBasePath(DirectoryHelper.GetAppDirectory()) // 设置基础路径为当前目录
  39. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  40. .Build();
  41. _appBaseAddress = AppConfig.GetSection("Peis")
  42. .GetSection("PeisBaseUrl").Value;
  43. _appUser = AppConfig.GetSection("Peis")
  44. .GetSection("AppUser").Value;
  45. _appPassword = AppConfig.GetSection("Peis")
  46. .GetSection("AppPassWord").Value;
  47. _appQueueRegisterUrl = AppConfig.GetSection("Peis")
  48. .GetSection("AppQueueRegisterUrl").Value;
  49. _appIsPeisRegisterUrl = AppConfig.GetSection("Peis")
  50. .GetSection("AppIsPeisRegisterUrl").Value;
  51. _appUpdateAppointPatientAsbitemStatusUrl= AppConfig.GetSection("Peis")
  52. .GetSection("AppUpdateAppointPatientAsbitemStatusUrl").Value;
  53. }
  54. /// <summary>
  55. /// 小程序获取当前排队信息
  56. /// </summary>
  57. /// <param name="input"></param>
  58. /// <returns></returns>
  59. public async Task<GetAppQueueRegisterByPersonIdDto> GetAppQueueRegisterByIdNoAsync(IdNoInputDto input)
  60. {
  61. var appQueueRegisterByPersonIdDtos = await CallAppServiceAsync<IdNoInputDto, GetAppQueueRegisterByPersonIdDto>(_appQueueRegisterUrl,
  62. input);
  63. return appQueueRegisterByPersonIdDtos;
  64. }
  65. /// <summary>
  66. /// 获取预约记录是否在体检系统登记,用于判断是否能退款
  67. /// </summary>
  68. /// <param name="input"></param>
  69. /// <returns></returns>
  70. public async Task<bool> GetAppointPatientRegisterIsPeisRegisterAsync(AppointPatientRegisterIdInputDto input)
  71. {
  72. bool isPeisRegister = await CallAppServiceAsync<AppointPatientRegisterIdInputDto, bool>(_appIsPeisRegisterUrl,
  73. input);
  74. return isPeisRegister;
  75. }
  76. /// <summary>
  77. /// 小程序退款成功后,更新体检系统项目收费状态
  78. /// </summary>
  79. /// <param name="input"></param>
  80. /// <returns></returns>
  81. public async Task UpdateAppointPatientAsbitemStatusAsync(UpdateAppointPatientAsbitemStatusInputDto input)
  82. {
  83. await CallAppServiceAsync<UpdateAppointPatientAsbitemStatusInputDto, bool>(_appUpdateAppointPatientAsbitemStatusUrl,
  84. input);
  85. }
  86. public async Task<TOut> CallAppServiceAsync<TInput, TOut>(string url, TInput data, string method = "post")
  87. {
  88. string baseAddress = _appBaseAddress;
  89. await CheckLoginAsync();
  90. using (var httpClientHandler = new HttpClientHandler())
  91. {
  92. using (var httpClient = new HttpClient(httpClientHandler))
  93. {
  94. httpClient.BaseAddress = new Uri(baseAddress);
  95. httpClient.DefaultRequestHeaders.Accept.Add(
  96. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  97. httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken);
  98. IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
  99. timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
  100. var sendData = JsonConvert.SerializeObject(data, Formatting.Indented, timeFormat);
  101. using (HttpContent httpContent = new StringContent(sendData))
  102. {
  103. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  104. HttpResponseMessage response = null;
  105. if (method == "post")
  106. {
  107. response = await httpClient.PostAsync(url, httpContent);
  108. }
  109. else
  110. {
  111. response = await httpClient.GetAsync(url);
  112. }
  113. string result;
  114. if (!response.IsSuccessStatusCode)
  115. {
  116. result = response.Content.ReadAsStringAsync().Result;
  117. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  118. }
  119. result = await response.Content.ReadAsStringAsync();
  120. var resultDto = JsonConvert.DeserializeObject<WebApiOutDto<TOut>>(result);
  121. if (resultDto != null)
  122. {
  123. if (resultDto.Code == -1)
  124. {
  125. throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.Message);
  126. }
  127. return resultDto.Data;
  128. }
  129. return resultDto.Data;
  130. }
  131. }
  132. }
  133. }
  134. private async Task CheckLoginAsync()
  135. {
  136. if (string.IsNullOrWhiteSpace(_accesToken))
  137. {
  138. await LoginAsync();
  139. }
  140. else
  141. {
  142. var handler = new JwtSecurityTokenHandler();
  143. var payload = handler.ReadJwtToken(_accesToken).Payload;
  144. var claims = payload.Claims;
  145. var exp = claims.First(claim => claim.Type == "exp").Value;
  146. if (exp == null)
  147. {
  148. await LoginAsync();
  149. }
  150. else
  151. {
  152. if (long.TryParse(exp, out var expires))
  153. {
  154. var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime;
  155. if (expireTime <= DateTime.Now)
  156. {
  157. await LoginAsync();
  158. }
  159. }
  160. else
  161. {
  162. await LoginAsync();
  163. }
  164. }
  165. }
  166. }
  167. public async Task<WebApiOutDto<LoginOutDataDto>> LoginAsync()
  168. {
  169. if (string.IsNullOrWhiteSpace(_appUser))
  170. {
  171. throw new Exception("SelfUser不能为空");
  172. }
  173. if (string.IsNullOrWhiteSpace(_appPassword))
  174. {
  175. throw new Exception("SelfPassword不能为空");
  176. }
  177. var relult = await LoginAsync(_appUser, _appPassword);
  178. return relult;
  179. }
  180. public async Task<WebApiOutDto<LoginOutDataDto>> LoginAsync(string userId, string password)
  181. {
  182. if (string.IsNullOrWhiteSpace(userId))
  183. {
  184. throw new Exception("用户ID不能为空");
  185. }
  186. if (string.IsNullOrWhiteSpace(password))
  187. {
  188. throw new Exception("密码不能为空");
  189. }
  190. using (var httpClientHandler = new HttpClientHandler())
  191. {
  192. using (var httpClient = new HttpClient(httpClientHandler))
  193. {
  194. httpClient.BaseAddress = new Uri(_appBaseAddress);
  195. httpClient.DefaultRequestHeaders.Accept.Add(
  196. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  197. var url = "api/identity/users/login";
  198. var loginUser = new LoginInputDto()
  199. {
  200. UserName = userId,
  201. Password = password
  202. };
  203. var sendData = JsonConvert.SerializeObject(loginUser);
  204. using (HttpContent httpContent = new StringContent(sendData))
  205. {
  206. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  207. HttpResponseMessage response = await httpClient.PostAsync(url, httpContent);
  208. string result;
  209. if (!response.IsSuccessStatusCode)
  210. {
  211. result = response.Content.ReadAsStringAsync().Result;
  212. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  213. }
  214. result = await response.Content.ReadAsStringAsync();
  215. var restultDto = JsonConvert.DeserializeObject<WebApiOutDto<LoginOutDataDto>>(result);
  216. if (restultDto == null)
  217. {
  218. throw new Exception("返回结果是空");
  219. }
  220. if (restultDto.Code != 1)
  221. {
  222. throw new Exception($"登录失败{restultDto.Message}");
  223. }
  224. _accesToken = restultDto.Data.access_token;
  225. return restultDto;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }