Browse Source

短信校验码

master
DESKTOP-G961P6V\Zhh 2 years ago
parent
commit
6e3a51ea8c
  1. 1
      Shentun.Sms.Client/SmsClientHelper.cs
  2. 24
      src/Shentun.WebPeis.Application.Contracts/Persons/CreatePersonDto.cs
  3. 71
      src/Shentun.WebPeis.Application.Contracts/Persons/CreatePersonKinshipDto.cs
  4. 24
      src/Shentun.WebPeis.Application.Contracts/Persons/SmsVerifyCodeInputDto.cs
  5. 118
      src/Shentun.WebPeis.Application/Persons/PersonAppService.cs
  6. 1
      src/Shentun.WebPeis.Domain.Shared/Enums/CacheKeys.cs
  7. 2
      src/Shentun.WebPeis.Domain/Persons/PersonManager.cs
  8. 1
      src/Shentun.WebPeis.HttpApi.Host/appsettings.json

1
Shentun.Sms.Client/SmsClientHelper.cs

@ -90,6 +90,7 @@ namespace Shentun.Sms.Client
{
throw new Exception("解析校验短信类别ID错误");
}
smsDto.SmsTypeId = verifySmsTypeId;
await CreateSmsTask(smsDto);
}

24
src/Shentun.WebPeis.Application.Contracts/Persons/CreatePersonDto.cs

@ -68,16 +68,30 @@ namespace Shentun.WebPeis.Persons
/// </summary>
public string? Address { get; set; }
/// <summary>
/// 微信OpenId
/// </summary>
public string? WechatOpenId { get; set; }
/// <summary>
/// 身份证件类别
/// </summary>
public string? IdTypeId { get; set; }
/// <summary>
/// 国家码
/// </summary>
public string? CountryCode { get; set; }
/// <summary>
/// 允许绑定
/// </summary>
public char? IsAllowBind { get; set; }
/// <summary>
/// 微信JsCode
/// </summary>
public string? JsCode { get; set; }
/// <summary>
/// 短信校验码
/// </summary>
public string SmsVerifyCode { get; set; }
}
}

71
src/Shentun.WebPeis.Application.Contracts/Persons/CreatePersonKinshipDto.cs

@ -4,76 +4,9 @@ using System.Text;
namespace Shentun.WebPeis.Persons
{
public class CreatePersonKinshipDto
public class CreatePersonKinshipDto: CreatePersonDto
{
/// <summary>
/// 组织单位ID
/// </summary>
public Guid MedicalCenterId { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string PersonName { get; set; } = null!;
/// <summary>
/// 性别
/// </summary>
public char SexId { get; set; }
/// <summary>
/// 婚姻状况
/// </summary>
public char MaritalStatusId { get; set; }
/// <summary>
/// 出生日期
/// </summary>
public DateTime? BirthDate { get; set; }
/// <summary>
/// 民族编号
/// </summary>
public string? NationId { get; set; }
/// <summary>
/// 出生地
/// </summary>
public Guid? BirthPlaceId { get; set; }
/// <summary>
/// 身份证号
/// </summary>
public string? IdNo { get; set; }
/// <summary>
/// 邮政编码
/// </summary>
public string? PostalCode { get; set; }
/// <summary>
/// email
/// </summary>
public string? Email { get; set; }
/// <summary>
/// 手机号
/// </summary>
public string? MobileTelephone { get; set; }
/// <summary>
/// 地址
/// </summary>
public string? Address { get; set; }
public string? IdTypeId { get; set; }
public string? CountryCode { get; set; }
public char? IsAllowBind { get; set; }
public string KinshipId { get; set; }
}
}

24
src/Shentun.WebPeis.Application.Contracts/Persons/SmsVerifyCodeInputDto.cs

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Shentun.WebPeis.Persons
{
public class SmsVerifyCodeInputDto
{
/// <summary>
/// 微信OpenId
/// </summary>
public string WechatOpenId { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string PersonName { get; set; } ;
/// <summary>
/// 手机号码
/// </summary>
public string MobileTelephone { get; set; }
public string IdNo { get; set; }
}
}

118
src/Shentun.WebPeis.Application/Persons/PersonAppService.cs

@ -36,6 +36,7 @@ using System.IO;
using Shentun.WebPeis.CustomerOrgs;
using Shentun.Utilities.Enums;
using Shentun.Sms.Client;
using Shentun.Utilities;
namespace Shentun.WebPeis.Persons
{
/// <summary>
@ -70,7 +71,7 @@ namespace Shentun.WebPeis.Persons
IRepository<PatientRegister> patientRegisterRepository,
IRepository<Patient> patientRepository,
CacheService cacheService,
//IHttpContextAccessor httpContextAccessor,
IHttpContextAccessor httpContextAccessor,
IRepository<CustomerOrg> customerOrgRepository)
{
_repository = repository;
@ -84,7 +85,7 @@ namespace Shentun.WebPeis.Persons
_patientRegisterRepository = patientRegisterRepository;
_patientRepository = patientRepository;
_cacheService = cacheService;
//_httpContextAccessor = httpContextAccessor;
_httpContextAccessor = httpContextAccessor;
_customerOrgRepository = customerOrgRepository;
}
@ -153,6 +154,19 @@ namespace Shentun.WebPeis.Persons
{
throw new UserFriendlyException("无效的WechatOpenId");
}
if (string.IsNullOrWhiteSpace(input.MobileTelephone))
{
throw new UserFriendlyException("手机号码不能为空");
}
if (input.MobileTelephone.Length != 11)
{
throw new UserFriendlyException("手机号码必须为11位");
}
if (_cache.Get(CacheKeys.SmsKey + input.MobileTelephone) != input.SmsVerifyCode)
{
throw new UserFriendlyException("无效的短信校验码");
}
var person = await _repository.FindAsync(o => o.IdNo == input.IdNo);
if (person != null)
@ -233,6 +247,11 @@ namespace Shentun.WebPeis.Persons
throw new UserFriendlyException("亲属关系不能为空");
}
if (_cache.Get(CacheKeys.SmsKey + input.MobileTelephone) != input.SmsVerifyCode)
{
throw new UserFriendlyException("无效的短信校验码");
}
var person = await _repository.FindAsync(o=>o.IdNo == input.IdNo);
if (person != null)
{
@ -368,8 +387,78 @@ namespace Shentun.WebPeis.Persons
return returnValue;
}
/// <summary>
/// 获取校验码
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
[HttpPost("api/app/Person/GetSmsVerifyCode")]
public async Task GetSmsVerifyCodeAsync(SmsVerifyCodeInputDto input)
{
if(input == null)
{
throw new UserFriendlyException("input不能为空");
}
if (string.IsNullOrWhiteSpace(input.WechatOpenId))
{
throw new UserFriendlyException("WechatOpenId不能为空");
}
if (_cache.Get(CacheKeys.OpenIdKey + input.WechatOpenId) != input.WechatOpenId)
{
throw new UserFriendlyException("无效的WechatOpenId");
}
if (string.IsNullOrWhiteSpace(input.PersonName))
{
throw new UserFriendlyException("姓名不能为空");
}
if (string.IsNullOrWhiteSpace(input.MobileTelephone))
{
throw new UserFriendlyException("手机号码不能为空");
}
if (input.MobileTelephone.Length != 11)
{
throw new UserFriendlyException("手机号码必须为11位");
}
if (string.IsNullOrWhiteSpace(input.IdNo))
{
throw new UserFriendlyException("身份证号码不能为空");
}
input.IdNo = input.IdNo.Trim();
if (input.IdNo.Length != 18)
{
throw new UserFriendlyException("身份证长度必须为18位");
}
//var sexByIdNo = ConvertExtr.ToSexByIdNo(idNo).ToCharArray();
var person = await _repository.FindAsync(o => o.IdNo == input.IdNo);
if (person != null)
{
var user = (await _identityUserRepository.GetQueryableAsync()).Where(o => o.Id == person.PersonId &&
o.PhoneNumber == input.MobileTelephone).FirstOrDefault();
if (user == null)
{
throw new UserFriendlyException("该身份证号已注册,但手机号码不一致");
}
//if (!string.IsNullOrWhiteSpace(person.WechatOpenId))
//{
// throw new UserFriendlyException("该微信号已注册");
//}
}
var createSmsTaskDto = new CreateSmsTaskDto()
{
PersonId = "0001",
PersonName = input.PersonName,
MobileTelephone = input.MobileTelephone,
CountryCode = "86",
};
var message = await SendVerifySms(createSmsTaskDto);
}
private async Task<UserTokenDto> GetTokenAsync(string request)
{
@ -434,22 +523,35 @@ namespace Shentun.WebPeis.Persons
}
public async Task SendVerifySms(CreateSmsTaskDto createSmsTaskDto)
public async Task<string> SendVerifySms(CreateSmsTaskDto createSmsTaskDto)
{
if(createSmsTaskDto == null)
{
throw new UserFriendlyException("createSmsTaskDto参数不能为空");
}
var message = Shentun.Utilities.Encrypt.RandomHelper.CreateRandom(Utilities.Enums.RandomType.Num, 6);
createSmsTaskDto.Content = message+"|1";
var verifySmsValidTimeStr = _configuration.GetSection("Sms")
.GetSection("VerifySmsValidTime").Value;
if (!int.TryParse(verifySmsValidTimeStr, out var verifySmsValidTime))
{
throw new Exception("解析校验短信有效时间错误");
}
createSmsTaskDto.Content = message+"|" + verifySmsValidTime.ToString();
//发送短信
createSmsTaskDto.TaskCycleType = '0';
await SmsClientHelper.CreateVerifySmsTask(createSmsTaskDto);
//存储短信校验码
_cache.Set(createSmsTaskDto.MobileTelephone, message);
var options = new DistributedCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(verifySmsValidTime));
_cache.Set(CacheKeys.SmsKey + createSmsTaskDto.MobileTelephone, message, options);
return message;
}
}
}

1
src/Shentun.WebPeis.Domain.Shared/Enums/CacheKeys.cs

@ -7,5 +7,6 @@ namespace Shentun.WebPeis.Enums
public class CacheKeys
{
public const string OpenIdKey = "OpenIdKey";
public const string SmsKey = "SmsKey";
}
}

2
src/Shentun.WebPeis.Domain/Persons/PersonManager.cs

@ -143,6 +143,8 @@ namespace Shentun.WebPeis.Persons
{
throw new UserFriendlyException("体检中心不能为空");
}
if (!string.IsNullOrEmpty(entity.IdNo))
{
entity.IdNo = entity.IdNo.Trim();

1
src/Shentun.WebPeis.HttpApi.Host/appsettings.json

@ -53,6 +53,7 @@
"User": "admin",
"Password": "888888",
"VerifySmsTypeId": "3a12e09d-438a-1d08-d4f0-712aef13708d",
"VerifySmsValidTime": "1",
"SmsAppId": "3a12e09f-8534-ec43-8c46-e61af0964ab6"
}
}
Loading…
Cancel
Save