109 lines
3.7 KiB
C#
109 lines
3.7 KiB
C#
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<bool> VerifyUserCodeAsync(IRepository<VerificationCode> _verificationCodeRepository, EmailVerifyCodeType verifyCodeType, string email, string code)
|
|
{
|
|
return await VerifyUserCodeAsync(_verificationCodeRepository, verifyCodeType, null, email, code);
|
|
}
|
|
|
|
public static async Task<bool> VerifyUserCodeAsync(IRepository<VerificationCode> _verificationCodeRepository, EmailVerifyCodeType verifyCodeType, Guid identityUserId, string code)
|
|
{
|
|
return await VerifyUserCodeAsync(_verificationCodeRepository, verifyCodeType, identityUserId, null, code);
|
|
}
|
|
|
|
public static async Task<bool> VerifyUserCodeAsync(IRepository<VerificationCode> _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;
|
|
}
|
|
|
|
|
|
}
|
|
}
|