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.

288 lines
12 KiB

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