匿名邮箱修改密码
This commit is contained in:
@@ -173,6 +173,17 @@ namespace IRaCIS.Application.Contracts
|
||||
public string OldPassWord { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class UserAccountDto
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
|
||||
public string UserRealName { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class UserListQueryDTO : PageInput
|
||||
{
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
|
||||
@@ -12,9 +12,11 @@ namespace IRaCIS.Application.Services
|
||||
Task<IResponseOutput<LoginReturnDTO>> Login(string userName, string password);
|
||||
Task<IResponseOutput> ModifyPassword(EditPasswordCommand editPwModel);
|
||||
Task<IResponseOutput> ResetPassword(Guid userId);
|
||||
Task<IResponseOutput> SendVerificationCode(string emailOrPhone, VerifyType verificationType, bool isReviewer = false);
|
||||
Task<IResponseOutput> SetNewPassword(ResetPasswordCommand resetPwdModel);
|
||||
|
||||
Task<IResponseOutput> UpdateUser(UserCommand model);
|
||||
Task<IResponseOutput> UpdateUserState(Guid userId, UserStateEnum state);
|
||||
|
||||
//Task<IResponseOutput> SendVerificationCode(string emailOrPhone, VerifyType verificationType, bool isReviewer = false);
|
||||
//Task<IResponseOutput> SetNewPassword(ResetPasswordCommand resetPwdModel);
|
||||
}
|
||||
}
|
||||
@@ -161,21 +161,110 @@ namespace IRaCIS.Application.Services
|
||||
/// <returns></returns>
|
||||
[HttpGet("{userId:guid}")]
|
||||
|
||||
public async Task<IResponseOutput> ResetPassword(Guid userId)
|
||||
public async Task<IResponseOutput> ResetPassword(Guid userId
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 重置密码发邮件 (未登陆修改) 发送成功,返回用户账户Id
|
||||
/// </summary>
|
||||
/// <param name="email"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[HttpGet("{email}")]
|
||||
public async Task<IResponseOutput> AnonymousSendVerificationCode(string email)
|
||||
{
|
||||
|
||||
//检查手机或者邮箱是否有效
|
||||
if (!Regex.IsMatch(email, @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"))
|
||||
{
|
||||
|
||||
return ResponseOutput.NotOk("Please input a legal email");
|
||||
|
||||
}
|
||||
|
||||
////查找改邮箱或者手机的用户
|
||||
var exist = await _userRepository.AnyAsync(t => t.EMail == email);
|
||||
|
||||
if (!exist)
|
||||
{
|
||||
return ResponseOutput.NotOk("Email not correct");
|
||||
|
||||
}
|
||||
|
||||
|
||||
//验证码 6位
|
||||
int verificationCode = new Random().Next(100000, 1000000);
|
||||
|
||||
await _mailVerificationService.AnolymousSendEmailForResetAccount(email, verificationCode);
|
||||
|
||||
return ResponseOutput.Ok();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证验证码,没问题就返回用户所有的账户
|
||||
/// </summary>
|
||||
/// <param name="email"></param>
|
||||
/// <param name="verifyCode"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="BusinessValidationFailedException"></exception>
|
||||
[AllowAnonymous]
|
||||
[HttpGet("{email}/{verifyCode}")]
|
||||
public async Task<List<UserAccountDto>> VerifyAnonymousVerifyCode(string email, string verifyCode)
|
||||
{
|
||||
var verificationRecord = await _verificationCodeRepository
|
||||
.Where(t => t.UserId == Guid.Empty && t.Code == verifyCode && t.CodeType == VerifyType.Email && t.EmailOrPhone == email).OrderByDescending(t => t.CreateTime).FirstOrDefaultAsync();
|
||||
|
||||
//检查数据库是否存在该验证码
|
||||
if (verificationRecord == null)
|
||||
{
|
||||
|
||||
throw new BusinessValidationFailedException("Verification code error");
|
||||
}
|
||||
else
|
||||
{
|
||||
//检查验证码是否失效
|
||||
if (verificationRecord.ExpirationTime < DateTime.Now)
|
||||
{
|
||||
|
||||
throw new BusinessValidationFailedException("The verification code has expired");
|
||||
}
|
||||
else //验证码正确 并且 没有超时
|
||||
{
|
||||
|
||||
//删除验证码历史记录
|
||||
await _verificationCodeRepository.DeleteFromQueryAsync(t => t.Id == verificationRecord.Id);
|
||||
}
|
||||
}
|
||||
|
||||
var list = await _userRepository.Where(t => t.EMail == email).Select(t => new UserAccountDto() { UserId = t.Id, UserName = t.UserName, UserRealName = t.LastName + " / " + t.FirstName }).ToListAsync();
|
||||
|
||||
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet("{userId:guid}/{newPwd}")]
|
||||
public async Task<IResponseOutput> AnonymousSetPassword(Guid userId, string newPwd)
|
||||
{
|
||||
var success = await _userRepository.UpdateFromQueryAsync(t => t.Id == userId, u => new User()
|
||||
{
|
||||
Password = MD5Helper.Md5(StaticData.DefaultPassword),
|
||||
PasswordChanged = false
|
||||
Password = newPwd,
|
||||
PasswordChanged = true
|
||||
});
|
||||
|
||||
return ResponseOutput.Result(success);
|
||||
return ResponseOutput.Ok(success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改密码,当前支持旧密码修改密码
|
||||
/// </summary>
|
||||
/// <param name="editPwModel"><
|
||||
/// <param name="editPwModel">
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IResponseOutput> ModifyPassword(EditPasswordCommand editPwModel)
|
||||
@@ -225,109 +314,6 @@ namespace IRaCIS.Application.Services
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 重置密码发邮件 (未登陆修改)
|
||||
/// </summary>
|
||||
/// <param name="email"></param>
|
||||
/// <param name="userName"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[HttpGet("{email}/{userName}")]
|
||||
public async Task<IResponseOutput> SendVerificationCode(string email,string userName)
|
||||
{
|
||||
|
||||
//检查手机或者邮箱是否有效
|
||||
if (!Regex.IsMatch(email, @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"))
|
||||
{
|
||||
|
||||
return ResponseOutput.NotOk("Please input a legal email");
|
||||
|
||||
}
|
||||
|
||||
////查找改邮箱或者手机的用户
|
||||
var exist = await _userRepository.AnyAsync(t => t.EMail == email&& t.UserName == userName);
|
||||
|
||||
if (!exist)
|
||||
{
|
||||
return ResponseOutput.NotOk("User Id or Email not correct");
|
||||
|
||||
}
|
||||
var user = await _userRepository.FirstOrDefaultAsync(t => t.EMail == email);
|
||||
|
||||
|
||||
//验证码 6位
|
||||
int verificationCode = new Random().Next(100000, 1000000);
|
||||
|
||||
await _mailVerificationService.SendMail(user.Id, _userInfo.RealName, email, verificationCode);
|
||||
|
||||
return ResponseOutput.Ok();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
///接受验证码 设置新密码 (未登陆修改)
|
||||
/// </summary>
|
||||
/// <param name="resetPwdModel"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
public async Task<IResponseOutput> SetNewPassword(AllowAnonymousResetPasswordCommand resetPwdModel)
|
||||
{
|
||||
|
||||
var user = await _userRepository.FirstOrDefaultAsync(t => t.EMail == resetPwdModel.Email && t.UserName == resetPwdModel.UserName);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return ResponseOutput.NotOk("User Id or Email not correct");
|
||||
}
|
||||
|
||||
|
||||
var verificationRecord = await _verificationCodeRepository
|
||||
.FirstOrDefaultAsync(t => t.UserId == user.Id && t.Code == resetPwdModel.VerificationCode && t.CodeType == VerifyType.Email);
|
||||
|
||||
//检查数据库是否存在该验证码
|
||||
if (verificationRecord == null)
|
||||
{
|
||||
|
||||
return ResponseOutput.NotOk("Verification code error");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//检查验证码是否失效
|
||||
if (verificationRecord.ExpirationTime < DateTime.Now)
|
||||
{
|
||||
return ResponseOutput.NotOk("The verification code has expired");
|
||||
|
||||
}
|
||||
else //验证码正确 并且 没有超时
|
||||
{
|
||||
//更新密码
|
||||
//var pwd = MD5Helper.Md5(newPwd);
|
||||
//var count = _doctorRepository.Update<Doctor>().Where(t => t.Id == doctor.Id).Set(d => d.Password == pwd).ExecuteAffrows();
|
||||
|
||||
var success = await _userRepository.UpdateFromQueryAsync(t => t.Id == user.Id, u => new User()
|
||||
{
|
||||
Password = resetPwdModel.NewPwd,
|
||||
PasswordChanged = true
|
||||
});
|
||||
|
||||
//删除验证码历史记录
|
||||
await _verificationCodeRepository.DeleteFromQueryAsync(t => t.UserId == user.Id && t.CodeType == VerifyType.Email);
|
||||
|
||||
return ResponseOutput.Result(success);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户列表
|
||||
@@ -511,197 +497,5 @@ namespace IRaCIS.Application.Services
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送验证码 邮箱或者手机号
|
||||
/// </summary>
|
||||
/// <param name="emailOrPhone"></param>
|
||||
/// <param name="verificationType"></param>
|
||||
/// <param name="isReviewer"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{emailOrPhone}/{verificationType:int}")]
|
||||
[Obsolete]
|
||||
public async Task<IResponseOutput> SendVerificationCode(string emailOrPhone, VerifyType verificationType, bool isReviewer = false)
|
||||
{
|
||||
if (string.IsNullOrEmpty(emailOrPhone))
|
||||
{
|
||||
return ResponseOutput.NotOk(verificationType == VerifyType.Email ? "Please input email" : "Please input phone");
|
||||
|
||||
}
|
||||
//防止输入前后有空格
|
||||
var emailOrPhoneStr = emailOrPhone.Trim();
|
||||
|
||||
//检查手机或者邮箱是否有效
|
||||
if (!Regex.IsMatch(emailOrPhoneStr, @"/^1[34578]\d{9}$/") && !Regex.IsMatch(emailOrPhoneStr, @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"))
|
||||
{
|
||||
|
||||
return ResponseOutput.NotOk(verificationType == VerifyType.Email
|
||||
? "Please input a legal email"
|
||||
: "Please input a legal phone");
|
||||
|
||||
}
|
||||
|
||||
//医生登录
|
||||
if (isReviewer)
|
||||
{
|
||||
var exist = await _doctorRepository.AnyAsync(t => t.EMail == emailOrPhoneStr || t.Phone == emailOrPhoneStr);
|
||||
|
||||
if (!exist)
|
||||
{
|
||||
return ResponseOutput.NotOk(verificationType == VerifyType.Email
|
||||
? "No user with this email exists."
|
||||
: "No user with this phone exists.");
|
||||
|
||||
}
|
||||
|
||||
var user = await _doctorRepository.FirstOrDefaultAsync(t => t.EMail == emailOrPhoneStr || t.Phone == emailOrPhoneStr);
|
||||
//邮箱
|
||||
if (verificationType == VerifyType.Email)
|
||||
{
|
||||
//验证码 6位
|
||||
int verificationCode = new Random().Next(100000, 1000000);
|
||||
|
||||
await _mailVerificationService.SendMail(user.Id, user.ChineseName, emailOrPhoneStr,
|
||||
verificationCode);
|
||||
}
|
||||
//手机短信
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else//管理用户登录
|
||||
{
|
||||
//查找改邮箱或者手机的用户
|
||||
var exist = await _userRepository.AnyAsync(t => t.EMail == emailOrPhoneStr || t.Phone == emailOrPhoneStr);
|
||||
|
||||
if (!exist)
|
||||
{
|
||||
return ResponseOutput.NotOk(verificationType == VerifyType.Email
|
||||
? "No user with this email exists."
|
||||
: "No user with this phone exists.");
|
||||
|
||||
}
|
||||
var user = await _userRepository.FirstOrDefaultAsync(t => t.EMail == emailOrPhoneStr || t.Phone == emailOrPhoneStr);
|
||||
//邮箱
|
||||
if (verificationType == VerifyType.Email)
|
||||
{
|
||||
//验证码 6位
|
||||
int verificationCode = new Random().Next(100000, 1000000);
|
||||
|
||||
await _mailVerificationService.SendMail(user.Id, user.LastName + ' ' + user.FirstName, emailOrPhoneStr,
|
||||
verificationCode);
|
||||
}
|
||||
//手机短信
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseOutput.Ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 验证设置新密码
|
||||
/// </summary>
|
||||
/// <param name="resetPwdModel"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Obsolete]
|
||||
public async Task<IResponseOutput> SetNewPassword(ResetPasswordCommand resetPwdModel)
|
||||
{
|
||||
if (resetPwdModel.IsReviewer)
|
||||
{
|
||||
var emailOrPhoneStr = resetPwdModel.EmailOrPhone.Trim();
|
||||
var verificationCodeStr = resetPwdModel.VerificationCode.Trim();
|
||||
var user = await _doctorRepository.FirstOrDefaultAsync(t => t.EMail == emailOrPhoneStr || t.Phone == emailOrPhoneStr);
|
||||
|
||||
var verificationRecord = await _verificationCodeRepository
|
||||
.FirstOrDefaultAsync(t => t.UserId == user.Id && t.Code == verificationCodeStr && t.CodeType == resetPwdModel.VerificationType);
|
||||
|
||||
//检查数据库是否存在该验证码
|
||||
if (verificationRecord == null)
|
||||
{
|
||||
|
||||
return ResponseOutput.NotOk("Verification code error");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//检查验证码是否失效
|
||||
if (verificationRecord.ExpirationTime < DateTime.Now)
|
||||
{
|
||||
return ResponseOutput.NotOk("The verification code has expired");
|
||||
|
||||
}
|
||||
else //验证码正确 并且 没有超时
|
||||
{
|
||||
//更新密码
|
||||
var success = await _doctorRepository.UpdateFromQueryAsync(t => t.Id == user.Id, u => new Doctor()
|
||||
{
|
||||
Password = resetPwdModel.NewPwd
|
||||
});
|
||||
|
||||
//删除验证码历史记录
|
||||
await _verificationCodeRepository.DeleteFromQueryAsync(t => t.UserId == user.Id && t.CodeType == resetPwdModel.VerificationType);
|
||||
|
||||
return ResponseOutput.Result(success);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var emailOrPhoneStr = resetPwdModel.EmailOrPhone.Trim();
|
||||
var verificationCodeStr = resetPwdModel.VerificationCode.Trim();
|
||||
|
||||
var user = await _userRepository.FirstOrDefaultAsync(t => t.EMail == emailOrPhoneStr || t.Phone == emailOrPhoneStr);
|
||||
|
||||
|
||||
var verificationRecord = await _verificationCodeRepository
|
||||
.FirstOrDefaultAsync(t => t.UserId == user.Id && t.Code == verificationCodeStr && t.CodeType == resetPwdModel.VerificationType);
|
||||
|
||||
//检查数据库是否存在该验证码
|
||||
if (verificationRecord == null)
|
||||
{
|
||||
|
||||
return ResponseOutput.NotOk("Verification code error");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//检查验证码是否失效
|
||||
if (verificationRecord.ExpirationTime < DateTime.Now)
|
||||
{
|
||||
return ResponseOutput.NotOk("The verification code has expired");
|
||||
|
||||
}
|
||||
else //验证码正确 并且 没有超时
|
||||
{
|
||||
//更新密码
|
||||
//var pwd = MD5Helper.Md5(newPwd);
|
||||
//var count = _doctorRepository.Update<Doctor>().Where(t => t.Id == doctor.Id).Set(d => d.Password == pwd).ExecuteAffrows();
|
||||
|
||||
var success = await _userRepository.UpdateFromQueryAsync(t => t.Id == user.Id, u => new User()
|
||||
{
|
||||
Password = resetPwdModel.NewPwd,
|
||||
PasswordChanged = true
|
||||
});
|
||||
|
||||
//删除验证码历史记录
|
||||
await _verificationCodeRepository.DeleteFromQueryAsync(t => t.UserId == user.Id && t.CodeType == resetPwdModel.VerificationType);
|
||||
|
||||
return ResponseOutput.Result(success);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user