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.

590 lines
25 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 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 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 Shentun.WebPeis.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Volo.Abp.Application.Services;
  8. using Volo.Abp.Domain.Entities;
  9. using Volo.Abp.Domain.Repositories;
  10. using Volo.Abp.Users;
  11. using Shentun.WebPeis.Wechats;
  12. using Microsoft.Extensions.Configuration;
  13. using System.Net.Http;
  14. using System.Net;
  15. using System.Text.Json;
  16. using Volo.Abp.Identity;
  17. using Volo.Abp;
  18. using Shentun.WebPeis.Enums;
  19. using System.Net.Http.Headers;
  20. using Microsoft.AspNetCore.Mvc;
  21. using OpenIddict.Abstractions;
  22. using Microsoft.AspNetCore.Identity;
  23. using Volo.Abp.ObjectMapping;
  24. using Microsoft.AspNetCore.Authorization;
  25. using Volo.Abp.Caching;
  26. using NPOI.SS.Formula.Functions;
  27. using System.Linq.Dynamic.Core.Tokenizer;
  28. using Microsoft.Extensions.Caching.Distributed;
  29. using Volo.Abp.Uow;
  30. using Scriban.Parsing;
  31. using System.IdentityModel.Tokens.Jwt;
  32. using Shentun.WebPeis.PatientRegisters;
  33. using Microsoft.AspNetCore.Http;
  34. using System.IO;
  35. using Shentun.WebPeis.CustomerOrgs;
  36. using Shentun.Utilities.Enums;
  37. using Shentun.Sms.Client;
  38. using Shentun.Utilities;
  39. namespace Shentun.WebPeis.Persons
  40. {
  41. /// <summary>
  42. /// 微信人员
  43. /// </summary>
  44. [ApiExplorerSettings(GroupName = "Work")]
  45. [Authorize]
  46. public class PersonAppService : ApplicationService
  47. {
  48. private readonly IConfiguration _configuration;
  49. private readonly IRepository<IdentityUser, Guid> _identityUserRepository;
  50. private readonly IdentityUserManager _userManager;
  51. private readonly IRepository<Person> _repository;
  52. private readonly PersonManager _personManager;
  53. private readonly IDistributedCache<string, string> _cache;
  54. private readonly IUnitOfWorkManager _unitOfWorkManager;
  55. private readonly IRepository<PersonKinship> _personKinshipRepository;
  56. private readonly IRepository<PatientRegister> _patientRegisterRepository;
  57. private readonly IRepository<Patient> _patientRepository;
  58. private readonly CacheService _cacheService;
  59. private readonly IHttpContextAccessor _httpContextAccessor;
  60. private readonly IRepository<CustomerOrg> _customerOrgRepository;
  61. public PersonAppService(IRepository<Person> repository,
  62. IConfiguration configuration,
  63. IRepository<Volo.Abp.Identity.IdentityUser, Guid> identityUserRepository,
  64. IdentityUserManager userManager,
  65. PersonManager personManager,
  66. IUnitOfWorkManager unitOfWorkManager,
  67. IDistributedCache<string, string> cache,
  68. IRepository<PersonKinship> personKinshipRepository,
  69. IRepository<PatientRegister> patientRegisterRepository,
  70. IRepository<Patient> patientRepository,
  71. CacheService cacheService,
  72. //IHttpContextAccessor httpContextAccessor,
  73. IRepository<CustomerOrg> customerOrgRepository)
  74. {
  75. _repository = repository;
  76. _configuration = configuration;
  77. _identityUserRepository = identityUserRepository;
  78. _userManager = userManager;
  79. _personManager = personManager;
  80. _unitOfWorkManager = unitOfWorkManager;
  81. _cache = cache;
  82. _personKinshipRepository = personKinshipRepository;
  83. _patientRegisterRepository = patientRegisterRepository;
  84. _patientRepository = patientRepository;
  85. _cacheService = cacheService;
  86. //_httpContextAccessor = httpContextAccessor;
  87. _customerOrgRepository = customerOrgRepository;
  88. }
  89. public async Task<PersonDto> GetByIdAsync(PersonIdInputDto input)
  90. {
  91. var entity = await _repository.GetAsync(o => o.PersonId == input.PersonId);
  92. var entityDto = ObjectMapper.Map<Person, PersonDto>(entity);
  93. return entityDto;
  94. }
  95. /// <summary>
  96. /// 微信用户登录
  97. /// </summary>
  98. /// <param name="input"></param>
  99. /// <returns></returns>
  100. [AllowAnonymous]
  101. [HttpPost("api/app/Person/WeChatUserLogin")]
  102. public async Task<UserTokenDto> WeChatUserLoginAsync(WechatUserJsCodeInputDto input)
  103. {
  104. var weChatClientId = _configuration.GetSection("AuthServer").GetSection("WeChatClientId").Value;
  105. var secret = _configuration.GetSection("AuthServer").GetSection("WeChatClientSecret").Value;
  106. var commonScopes = new List<string> {
  107. OpenIddictConstants.Permissions.Scopes.Address,
  108. OpenIddictConstants.Permissions.Scopes.Email,
  109. OpenIddictConstants.Permissions.Scopes.Phone,
  110. OpenIddictConstants.Permissions.Scopes.Profile,
  111. OpenIddictConstants.Permissions.Scopes.Roles,
  112. "WebPeis"
  113. };
  114. var dic = new Dictionary<string, object>
  115. {
  116. {"jsCode",input.JsCode},
  117. {"client_id",weChatClientId},
  118. {"client_secret",secret},
  119. {"grant_type",WeChatGrant.GrantType},
  120. {"scope","WeChat offline_access"}
  121. };
  122. var dicStr = dic.Select(m => m.Key + "=" + m.Value).DefaultIfEmpty().Aggregate((m, n) => m + "&" + n);
  123. var token = await GetTokenAsync(dicStr);
  124. var sessionKey = CacheKeys.SessionKey + Guid.NewGuid().ToString();
  125. var sessionKeyValue = Guid.NewGuid().ToString();
  126. _cache.Set(sessionKey, sessionKeyValue);
  127. token.SessionKey = sessionKey;
  128. token.SessionKeyValue = sessionKeyValue;
  129. return token;
  130. }
  131. [AllowAnonymous]
  132. [HttpPost("api/app/Person/Create")]
  133. [UnitOfWork(IsDisabled = false)]
  134. public async Task<UserTokenDto> CreateAsync(CreatePersonDto input)
  135. {
  136. using (var unitOfWork = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
  137. {
  138. var entity = ObjectMapper.Map<CreatePersonDto, Person>(input);
  139. if (string.IsNullOrWhiteSpace(input.JsCode))
  140. {
  141. throw new UserFriendlyException("jsCode不能为空");
  142. }
  143. if (string.IsNullOrWhiteSpace(input.WechatOpenId))
  144. {
  145. throw new UserFriendlyException("WechatOpenId不能为空");
  146. }
  147. if (_cache.Get(CacheKeys.OpenIdKey + input.WechatOpenId) != input.WechatOpenId)
  148. {
  149. throw new UserFriendlyException("无效的WechatOpenId");
  150. }
  151. if (string.IsNullOrWhiteSpace(input.MobileTelephone))
  152. {
  153. throw new UserFriendlyException("手机号码不能为空");
  154. }
  155. if (input.MobileTelephone.Length != 11)
  156. {
  157. throw new UserFriendlyException("手机号码必须为11位");
  158. }
  159. if (string.IsNullOrWhiteSpace(input.SmsVerifyCodeKey))
  160. {
  161. throw new UserFriendlyException("短信校验码键不能为空");
  162. }
  163. if (_cache.Get(input.SmsVerifyCodeKey) != input.SmsVerifyCode)
  164. {
  165. throw new UserFriendlyException("无效的短信校验码或已过期");
  166. }
  167. var person = await _repository.FindAsync(o => o.IdNo == input.IdNo);
  168. if (person != null)
  169. {
  170. var user = (await _identityUserRepository.GetQueryableAsync()).Where(o => o.Id == person.PersonId &&
  171. o.PhoneNumber == input.MobileTelephone).FirstOrDefault();
  172. if (user == null)
  173. {
  174. throw new UserFriendlyException("该身份证号已注册,但手机号码不一致");
  175. }
  176. if(!string.IsNullOrWhiteSpace(person.WechatOpenId))
  177. {
  178. throw new UserFriendlyException("该微信号已注册");
  179. }
  180. person.WechatOpenId = input.WechatOpenId;
  181. await _repository.UpdateAsync(person);
  182. await unitOfWork.SaveChangesAsync();
  183. await unitOfWork.CompleteAsync();
  184. }
  185. else
  186. {
  187. entity.WechatOpenId = input.WechatOpenId;
  188. var userWithPerson = await _personManager.CreateAsync(entity, input.PersonName,
  189. input.Email, input.MobileTelephone);
  190. await _identityUserRepository.InsertAsync(userWithPerson.User);
  191. await _repository.InsertAsync(userWithPerson.Person);
  192. //设置密码
  193. (await _userManager.RemovePasswordAsync(userWithPerson.User)).CheckErrors();
  194. (await _userManager.AddPasswordAsync(userWithPerson.User, Shentun.Utilities.
  195. Encrypt.RandomHelper.CreateRandom(Utilities.Enums.RandomType.NumAndChar, 10) + "0Cz*")).CheckErrors();
  196. await unitOfWork.SaveChangesAsync();
  197. await unitOfWork.CompleteAsync();
  198. }
  199. }
  200. using (var unitOfWork = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
  201. {
  202. var weChatClientId = _configuration.GetSection("AuthServer").GetSection("WeChatClientId").Value;
  203. var secret = _configuration.GetSection("AuthServer").GetSection("WeChatClientSecret").Value;
  204. var dic = new Dictionary<string, object>
  205. {
  206. {"jsCode",input.JsCode},
  207. {"client_id",weChatClientId},
  208. {"client_secret",secret},
  209. {"grant_type",WeChatGrant.GrantType},
  210. {"scope","WeChat offline_access"}
  211. };
  212. var dicStr = dic.Select(m => m.Key + "=" + m.Value).DefaultIfEmpty().Aggregate((m, n) => m + "&" + n);
  213. var token = await GetTokenAsync(dicStr);
  214. //var entityDto = ObjectMapper.Map<Person, PersonDto>(userWithPerson.Person);
  215. await unitOfWork.CompleteAsync();
  216. return token;
  217. }
  218. }
  219. /// <summary>
  220. /// 创建亲属
  221. /// </summary>
  222. /// <param name="input"></param>
  223. /// <returns></returns>
  224. /// <exception cref="UserFriendlyException"></exception>
  225. [HttpPost("api/app/Person/CreatePersonKinship")]
  226. public async Task CreatePersonKinshipAsync(CreatePersonKinshipDto input)
  227. {
  228. var entity = ObjectMapper.Map<CreatePersonKinshipDto, Person>(input);
  229. if (string.IsNullOrWhiteSpace(input.KinshipId))
  230. {
  231. throw new UserFriendlyException("亲属关系不能为空");
  232. }
  233. if (string.IsNullOrWhiteSpace(input.SmsVerifyCodeKey))
  234. {
  235. throw new UserFriendlyException("短信校验码键不能为空");
  236. }
  237. if (_cache.Get(input.SmsVerifyCodeKey) != input.SmsVerifyCode)
  238. {
  239. throw new UserFriendlyException("无效的短信校验码或已过期");
  240. }
  241. var person = await _repository.FindAsync(o=>o.IdNo == input.IdNo);
  242. if (person != null)
  243. {
  244. var user = (await _identityUserRepository.GetQueryableAsync()).Where(o =>o.Id == person.PersonId &&
  245. o.PhoneNumber == input.MobileTelephone).FirstOrDefault();
  246. if(user == null)
  247. {
  248. throw new UserFriendlyException("该身份证号已注册,但手机号码不一致");
  249. }
  250. var personKinshipExist = new PersonKinship()
  251. {
  252. PersonId = person.PersonId,
  253. ParentPersonId = (Guid)CurrentUser.Id,
  254. KinshipId = input.KinshipId,
  255. };
  256. await _personKinshipRepository.InsertAsync(personKinshipExist);
  257. return;
  258. }
  259. var userWithPerson = await _personManager.CreateAsync(entity, input.PersonName,
  260. input.Email, input.MobileTelephone);
  261. var personKinship = new PersonKinship()
  262. {
  263. PersonId = userWithPerson.Person.PersonId,
  264. ParentPersonId = (Guid)CurrentUser.Id,
  265. KinshipId = input.KinshipId,
  266. };
  267. await _identityUserRepository.InsertAsync(userWithPerson.User);
  268. await _repository.InsertAsync(userWithPerson.Person);
  269. await _personKinshipRepository.InsertAsync(personKinship);
  270. //设置密码
  271. (await _userManager.RemovePasswordAsync(userWithPerson.User)).CheckErrors();
  272. (await _userManager.AddPasswordAsync(userWithPerson.User, Shentun.Utilities.
  273. Encrypt.RandomHelper.CreateRandom(Utilities.Enums.RandomType.NumAndChar, 10) + "0Cz*")).CheckErrors();
  274. }
  275. /// <summary>
  276. /// 获取体检次数列表
  277. /// </summary>
  278. /// <param name="input"></param>
  279. /// <returns></returns>
  280. [HttpPost("api/app/Person/GetMedicalTimesListByPersonId")]
  281. public async Task<List<PersonMedicalTimesDto>> GetMedicalTimesListByPersonIdAsync(PersonIdInputDto input)
  282. {
  283. var entityList = (from user in await _identityUserRepository.GetQueryableAsync()
  284. join person in await _repository.GetQueryableAsync()
  285. on user.Id equals person.PersonId
  286. join patient in await _patientRepository.GetQueryableAsync()
  287. on new { idNo = person.IdNo ,phone = user.PhoneNumber} equals new { idNo = patient.IdNo, phone = patient.MobileTelephone }
  288. join patientRegister in await _patientRegisterRepository.GetQueryableAsync()
  289. on patient.PatientId equals patientRegister.PatientId
  290. where user.Id == input.PersonId &&
  291. ( patientRegister.CompleteFlag == PatientRegisterCompleteFlag.Audit)
  292. orderby patientRegister.MedicalStartDate
  293. select new PersonMedicalTimesDto()
  294. {
  295. PatientRegisterId = patientRegister.PatientRegisterId,
  296. PersonName = patient.PatientName,
  297. MedicalStartDate = patientRegister.MedicalStartDate,
  298. }).ToList();
  299. return entityList;
  300. }
  301. /// <summary>
  302. /// 获取本人和亲属列表
  303. /// </summary>
  304. /// <returns></returns>
  305. [HttpPost("api/app/Person/GetPersonKinshipList")]
  306. public async Task<List<PersonDto>> GetPersonKinshipList()
  307. {
  308. var personKinshipIds = (await _personKinshipRepository.GetQueryableAsync())
  309. .Where(o => o.ParentPersonId == CurrentUser.Id)
  310. .Select(o => o.PersonId).ToList();
  311. personKinshipIds.Add((Guid)CurrentUser.Id);
  312. var personList = (from user in await _identityUserRepository.GetQueryableAsync()
  313. join person in await _repository.GetQueryableAsync()
  314. on user.Id equals person.PersonId
  315. where personKinshipIds.Contains(user.Id)
  316. orderby user.CreationTime
  317. select new PersonDto
  318. {
  319. PersonId = user.Id,
  320. PersonName = user.Name,
  321. SexId = person.SexId,
  322. SexName = _cacheService.GetSexNameAsync(person.SexId).Result,
  323. MaritalStatusId = person.MaritalStatusId,
  324. MaritalStatusName = _cacheService.GetMaritalStatusNameAsync(person.MaritalStatusId).Result,
  325. IdNo = person.IdNo,
  326. MobileTelephone = user.PhoneNumber
  327. }).ToList();
  328. for(var i = 0; i<personList.Count;i++)
  329. {
  330. personList[i].DisplayOrder = i + 1;
  331. if (personList[i].PersonId == CurrentUser.Id)
  332. {
  333. personList[i].DisplayOrder = 0; //本人强行排第一个
  334. }
  335. }
  336. personList = personList.OrderBy(o=>o.DisplayOrder).ToList();
  337. return personList;
  338. }
  339. /// <summary>
  340. /// 获取PDF体检报告
  341. /// </summary>
  342. /// <param name="input"></param>
  343. /// <returns></returns>
  344. /// <exception cref="UserFriendlyException"></exception>
  345. [HttpPost("api/app/Person/GetMedicalReportByPatientRegisterId")]
  346. public async Task<MedicalReportDto> GetMedicalReportByPatientRegisterIdAsync(PatientRegisterIdInputDto input)
  347. {
  348. var entity = await _patientRegisterRepository.GetAsync(o=>o.PatientRegisterId == input.PatientRegisterId);
  349. if(string.IsNullOrWhiteSpace(entity.ReportFile))
  350. {
  351. throw new UserFriendlyException("没有报告单");
  352. }
  353. var Host = $"{_httpContextAccessor.HttpContext.Request.Scheme}://{_httpContextAccessor.HttpContext.Request.Host.Host}:{ _httpContextAccessor.HttpContext.Request.Host.Port}";
  354. var returnValue = new MedicalReportDto()
  355. {
  356. FilePath = entity.ReportFile,
  357. FileBase64 = Shentun.Utilities.FileHelper.ToBase64(Host + entity.ReportFile)
  358. };
  359. return returnValue;
  360. }
  361. /// <summary>
  362. /// 获取校验码
  363. /// </summary>
  364. /// <param name="input"></param>
  365. /// <returns></returns>
  366. /// <exception cref="UserFriendlyException"></exception>
  367. [AllowAnonymous]
  368. [HttpPost("api/app/Person/GetSmsVerifyCode")]
  369. public async Task<SmsVerifyCodeDto> GetSmsVerifyCodeAsync(SmsVerifyCodeInputDto input)
  370. {
  371. if(input == null)
  372. {
  373. throw new UserFriendlyException("input不能为空");
  374. }
  375. //if (string.IsNullOrWhiteSpace(input.WechatOpenId))
  376. //{
  377. // throw new UserFriendlyException("WechatOpenId不能为空");
  378. //}
  379. //if (_cache.Get(CacheKeys.OpenIdKey + input.WechatOpenId) != input.WechatOpenId)
  380. //{
  381. // throw new UserFriendlyException("无效的WechatOpenId");
  382. //}
  383. if (string.IsNullOrWhiteSpace(input.SessionKey))
  384. {
  385. throw new UserFriendlyException("SessionKey不能为空");
  386. }
  387. if (_cache.Get(input.SessionKey) != input.SessionKeyValue)
  388. {
  389. throw new UserFriendlyException("无效的SessionKeyValue");
  390. }
  391. if (string.IsNullOrWhiteSpace(input.PersonName))
  392. {
  393. throw new UserFriendlyException("姓名不能为空");
  394. }
  395. if (string.IsNullOrWhiteSpace(input.MobileTelephone))
  396. {
  397. throw new UserFriendlyException("手机号码不能为空");
  398. }
  399. if (input.MobileTelephone.Length != 11)
  400. {
  401. throw new UserFriendlyException("手机号码必须为11位");
  402. }
  403. if (string.IsNullOrWhiteSpace(input.IdNo))
  404. {
  405. throw new UserFriendlyException("身份证号码不能为空");
  406. }
  407. input.IdNo = input.IdNo.Trim();
  408. if (input.IdNo.Length != 18)
  409. {
  410. throw new UserFriendlyException("身份证长度必须为18位");
  411. }
  412. //var sexByIdNo = ConvertExtr.ToSexByIdNo(idNo).ToCharArray();
  413. var person = await _repository.FindAsync(o => o.IdNo == input.IdNo);
  414. if (person != null)
  415. {
  416. var user = (await _identityUserRepository.GetQueryableAsync()).Where(o => o.Id == person.PersonId &&
  417. o.PhoneNumber == input.MobileTelephone).FirstOrDefault();
  418. if (user == null)
  419. {
  420. throw new UserFriendlyException("该身份证号已注册,但手机号码不一致");
  421. }
  422. //if (!string.IsNullOrWhiteSpace(person.WechatOpenId))
  423. //{
  424. // throw new UserFriendlyException("该微信号已注册");
  425. //}
  426. }
  427. var createSmsTaskDto = new CreateSmsTaskDto()
  428. {
  429. PersonId = "0001",
  430. PersonName = input.PersonName,
  431. MobileTelephone = input.MobileTelephone,
  432. CountryCode = "86",
  433. };
  434. var smsVerifyCodeKey = await SendVerifySms(createSmsTaskDto);
  435. var SmsVerifyCodeDto = new SmsVerifyCodeDto()
  436. {
  437. SmsVerifyCodeKey = smsVerifyCodeKey
  438. };
  439. return SmsVerifyCodeDto;
  440. }
  441. private async Task<UserTokenDto> GetTokenAsync(string request)
  442. {
  443. using var client = new HttpClient();
  444. HttpContent httpContent = new StringContent(request);
  445. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
  446. var url = _configuration.GetSection("AuthServer").
  447. GetSection("Authority").Value + "/connect/token";
  448. var tokenResult = await client.PostAsync(url
  449. , httpContent);
  450. var tokenResultStr = await tokenResult.Content.ReadAsStringAsync();
  451. if (tokenResult.IsSuccessStatusCode)
  452. {
  453. if (!string.IsNullOrEmpty(tokenResultStr))
  454. {
  455. if (tokenResultStr.ToLower().Contains("openid"))
  456. {
  457. var wechatUserDto = JsonSerializer.Deserialize<WechatUserDto>(tokenResultStr,
  458. new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
  459. var userTokenDto = new UserTokenDto
  460. {
  461. IsNewUser = "Y",
  462. OpenId = wechatUserDto.OpenId
  463. };
  464. var options = new DistributedCacheEntryOptions()
  465. .SetAbsoluteExpiration(TimeSpan.FromMinutes(720));
  466. _cache.Set(CacheKeys.OpenIdKey + wechatUserDto.OpenId,
  467. wechatUserDto.OpenId, options);
  468. return userTokenDto;
  469. }
  470. else
  471. {
  472. var signResult = JsonSerializer.Deserialize<SignInResultDto>(tokenResultStr,
  473. new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
  474. var userTokenDto = new UserTokenDto
  475. {
  476. IsNewUser = "N",
  477. AccessToken = signResult.access_token,
  478. RefreshToken = signResult.refresh_token
  479. };
  480. return userTokenDto;
  481. }
  482. }
  483. else
  484. {
  485. throw new UserFriendlyException("token值为空");
  486. }
  487. }
  488. else
  489. {
  490. //tokenResultStr = tokenResultStr.Replace("<", "").Replace(">", "");
  491. //var grantErrorDto = JsonSerializer.Deserialize<GrantErrorDto>(tokenResultStr,
  492. // new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
  493. throw new UserFriendlyException("获取token失败:" + tokenResultStr);
  494. }
  495. }
  496. public async Task<string> SendVerifySms(CreateSmsTaskDto createSmsTaskDto)
  497. {
  498. if(createSmsTaskDto == null)
  499. {
  500. throw new UserFriendlyException("createSmsTaskDto参数不能为空");
  501. }
  502. var message = Shentun.Utilities.Encrypt.RandomHelper.CreateRandom(Utilities.Enums.RandomType.Num, 6);
  503. var verifySmsValidTimeStr = _configuration.GetSection("Sms")
  504. .GetSection("VerifySmsValidTime").Value;
  505. if (!int.TryParse(verifySmsValidTimeStr, out var verifySmsValidTime))
  506. {
  507. throw new Exception("解析校验短信有效时间错误");
  508. }
  509. createSmsTaskDto.Content = message+"|" + verifySmsValidTime.ToString();
  510. //发送短信
  511. createSmsTaskDto.TaskCycleType = '0';
  512. await SmsClientHelper.CreateVerifySmsTask(createSmsTaskDto);
  513. //存储短信校验码
  514. var options = new DistributedCacheEntryOptions()
  515. .SetAbsoluteExpiration(TimeSpan.FromMinutes(verifySmsValidTime));
  516. var smsVerifyCodeKey = CacheKeys.SmsKey + createSmsTaskDto.MobileTelephone + Guid.NewGuid().ToString();
  517. _cache.Set(smsVerifyCodeKey, message, options);
  518. return smsVerifyCodeKey;
  519. }
  520. }
  521. }