diff --git a/IRaCIS.Core.Application/Service/RepositoryHelper/VerifyCodeHelper.cs b/IRaCIS.Core.Application/Service/RepositoryHelper/VerifyCodeHelper.cs new file mode 100644 index 000000000..51d563238 --- /dev/null +++ b/IRaCIS.Core.Application/Service/RepositoryHelper/VerifyCodeHelper.cs @@ -0,0 +1,108 @@ +using IRaCIS.Core.Domain.Models; +using IRaCIS.Core.Infrastructure; +using Microsoft.AspNetCore.Identity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.Service +{ + public class VerifyCodeHelper + { + + + public static async Task VerifyUserCodeAsync(IRepository _verificationCodeRepository, EmailVerifyCodeType verifyCodeType, string email, string code) + { + return await VerifyUserCodeAsync(_verificationCodeRepository, verifyCodeType, null, email, code); + } + + public static async Task VerifyUserCodeAsync(IRepository _verificationCodeRepository, EmailVerifyCodeType verifyCodeType, Guid identityUserId, string code) + { + return await VerifyUserCodeAsync(_verificationCodeRepository, verifyCodeType, identityUserId, null, code); + } + + public static async Task VerifyUserCodeAsync(IRepository _verificationCodeRepository, EmailVerifyCodeType verifyCodeType, Guid? identityUserId, string? email, string code) + { + + var verifySuccess = false; + + var verificationRecord = new VerificationCode(); + + + if (identityUserId != null) + { + verificationRecord = await _verificationCodeRepository.FirstOrDefaultAsync(t => t.UserId == identityUserId && t.Code == code && t.CodeType == 0); + + if (verificationRecord != null) + { + //删除验证码历史记录 + await _verificationCodeRepository.BatchDeleteNoTrackingAsync(t => t.UserId == identityUserId && t.CodeType == 0); + + + if (email != null) + { + var useEmail = email.Trim(); + + if (verificationRecord.EmailOrPhone.Trim() != useEmail) + { + //发送验证嘛的和提交的邮箱不一致 + + throw new BusinessValidationFailedException(I18n.T("User_VerificationEmailNotSameWithBefore")); + + } + } + } + } + else if (email != null) + { + + var useEmail = email.Trim(); + + verificationRecord = await _verificationCodeRepository.FirstOrDefaultAsync(t => t.EmailOrPhone == useEmail && t.Code == code && t.CodeType == 0); + + if (verificationRecord != null) + { + //删除验证码历史记录 + await _verificationCodeRepository.BatchDeleteNoTrackingAsync(t => t.EmailOrPhone == useEmail && t.CodeType == 0); + } + + } + else + { + throw new InvalidOperationException("验证码参数不正确"); + } + + //检查数据库是否存在该验证码 + if (verificationRecord == null) + { + //---验证码错误。 + + throw new BusinessValidationFailedException(I18n.T("User_VerificationCodeError")); + + } + else + { + //检查验证码是否失效 + if (verificationRecord.ExpirationTime < DateTime.Now) + { + //---验证码已经过期。 + + throw new BusinessValidationFailedException(I18n.T("User_VerificationCodeExpired")); + + } + else //验证码正确 并且 没有超时 + { + verifySuccess = true; + + + } + } + + return verifySuccess; + } + + + } +} diff --git a/IRaCIS.Core.Domain/Common/VerificationCode.cs b/IRaCIS.Core.Domain/Common/VerificationCode.cs index c79357e22..104bb4f33 100644 --- a/IRaCIS.Core.Domain/Common/VerificationCode.cs +++ b/IRaCIS.Core.Domain/Common/VerificationCode.cs @@ -23,4 +23,18 @@ public class VerificationCode : BaseAddAuditEntity [Comment("过期时间")] public DateTime ExpirationTime { get; set; } +} + +public enum EmailVerifyCodeType +{ + + ModifyEmail = 1, + + SiteSurvey = 2, + + ForgetPassword = 3, + + MFA = 4, + + AdminResetPassword = 5 } \ No newline at end of file