diff --git a/Apimanager_backend/Controllers/UserController.cs b/Apimanager_backend/Controllers/UserController.cs index 748f01a..cabdd18 100644 --- a/Apimanager_backend/Controllers/UserController.cs +++ b/Apimanager_backend/Controllers/UserController.cs @@ -19,6 +19,10 @@ namespace Apimanager_backend.Controllers { this.userService = userService; } + /// + /// 获取用户个人信息 + /// + /// [HttpGet] [Authorize(Roles = "User")] public async Task>> UserInfo() @@ -32,5 +36,43 @@ namespace Apimanager_backend.Controllers ); return Ok(res); } + /// + /// 重置用户密码 + /// + /// + /// + [HttpPost] + public async Task>> Resetpassword([FromBody]ResetPasswordDto dto) + { + try + { + await userService.ResetPasswordAsync(dto.Email, dto.Code, dto.NewPassword); + var res = new ResponseBase( + code:1000, + message:"Success", + data: null + ); + return Ok(res); + }catch(BaseException e) + { + var res = new ResponseBase( + code:e.code, + message:e.message, + data:null + ); + return StatusCode(400,res); + } + } + [HttpPost] + public async Task>> SendResetEmail([FromQuery]string email) + { + await userService.SendResetPasswordEmailAsync(email); + var res = new ResponseBase( + code: 1000, + message: "Success", + data: null + ); + return Ok(res); + } } } diff --git a/Apimanager_backend/Dtos/ResetPasswordDto.cs b/Apimanager_backend/Dtos/ResetPasswordDto.cs new file mode 100644 index 0000000..7b4d394 --- /dev/null +++ b/Apimanager_backend/Dtos/ResetPasswordDto.cs @@ -0,0 +1,9 @@ +namespace Apimanager_backend.Dtos +{ + public class ResetPasswordDto + { + public string Email { get; set; } + public string NewPassword { get; set; } + public string Code { get; set; } + } +} diff --git a/Apimanager_backend/Services/IUserService.cs b/Apimanager_backend/Services/IUserService.cs index 02fea0e..644a04b 100644 --- a/Apimanager_backend/Services/IUserService.cs +++ b/Apimanager_backend/Services/IUserService.cs @@ -20,7 +20,7 @@ namespace Apimanager_backend.Services /// 重置密码的令牌 /// 新的密码 /// 异步操作 - Task ResetPasswordAsync(string email, string token, string newPassword); + Task ResetPasswordAsync(string email, string code, string newPassword); /// /// 获取用户信息。 diff --git a/Apimanager_backend/Services/UserService.cs b/Apimanager_backend/Services/UserService.cs index 973444b..155252c 100644 --- a/Apimanager_backend/Services/UserService.cs +++ b/Apimanager_backend/Services/UserService.cs @@ -3,9 +3,12 @@ using Apimanager_backend.Data; using Apimanager_backend.Dtos; using Apimanager_backend.Exceptions; using Apimanager_backend.Models; +using Apimanager_backend.Tools; using AutoMapper; using Microsoft.AspNetCore.Connections.Features; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; +using StackExchange.Redis; using System.ComponentModel; namespace Apimanager_backend.Services @@ -14,10 +17,17 @@ namespace Apimanager_backend.Services { private readonly ApiContext apiContext; private readonly IMapper mapper; - public UserService(ApiContext apiContext,IMapper automapper) + private readonly ILogger logger; + private readonly IConnectionMultiplexer redis; + private readonly IEmailService emailService; + private readonly int DbSet = 2; + public UserService(ApiContext apiContext,IMapper automapper,ILogger logger,IConnectionMultiplexer redis,IEmailService emailService) { this.apiContext = apiContext; this.mapper = automapper; + this.logger = logger; + this.redis = redis; + this.emailService = emailService; } public async Task GetUserAsync(int userId) { @@ -40,14 +50,41 @@ namespace Apimanager_backend.Services return await apiContext.Users.AnyAsync(x => x.Username == username); } - public Task ResetPasswordAsync(string email, string token, string newPassword) + public async Task ResetPasswordAsync(string email, string code, string newPassword) { - throw new NotImplementedException(); + //校验验证码 + var db = redis.GetDatabase(DbSet); + var value = await db.StringGetAsync(email); + if (!value.HasValue || value.ToString() != code) + { + throw new BaseException(5005, "验证码错误"); + } + //验证成功,开始重置流程 + var user = await apiContext.Users.FirstOrDefaultAsync(x => x.Email == email); + if(user == null) + { + throw new BaseException(2004, "用户不存在"); + } + //修改密码 + user.PassHash = newPassword; + apiContext.Users.Update(user); + await apiContext.SaveChangesAsync(); } - public Task SendResetPasswordEmailAsync(string email) + public async Task SendResetPasswordEmailAsync(string email) { - throw new NotImplementedException(); + var randomCode = RandomCodeHelper.GetRandomCodeStr(); + //记录到redis + var db = redis.GetDatabase(DbSet); + bool redisSuccess = await db.StringSetAsync(email,randomCode,TimeSpan.FromHours(1)); + if (!redisSuccess) + { + throw new BaseException(1005, "Redis Str Set Error"); + } + string subject = "重置验证码"; + string body = $"您的重置验证码为:{randomCode}
有效期60分钟!"; + //发送邮件 + await emailService.SendEmailAsync(email,subject,body); }