2024-01-07 13:34:50 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Lazy.Captcha.Core;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
2024-02-18 11:41:43 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using SqlSugar;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Volo.Abp;
|
|
|
|
|
|
using Volo.Abp.Application.Services;
|
|
|
|
|
|
using Volo.Abp.Authorization;
|
|
|
|
|
|
using Volo.Abp.Caching;
|
|
|
|
|
|
using Volo.Abp.Guids;
|
|
|
|
|
|
using Volo.Abp.Uow;
|
|
|
|
|
|
using Volo.Abp.Users;
|
|
|
|
|
|
using Yi.Framework.Rbac.Application.Contracts.Dtos.Account;
|
|
|
|
|
|
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Entities;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Managers;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Repositories;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Caches;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Options;
|
|
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.Rbac.Application.Services
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public class AccountService : ApplicationService, IAccountService
|
|
|
|
|
|
{
|
|
|
|
|
|
private IDistributedCache<CaptchaPhoneCacheItem, CaptchaPhoneCacheKey> _phoneCache;
|
|
|
|
|
|
private readonly ICaptcha _captcha;
|
|
|
|
|
|
private readonly IGuidGenerator _guidGenerator;
|
2023-12-20 21:01:35 +08:00
|
|
|
|
private readonly RbacOptions _rbacOptions;
|
2023-12-20 21:43:16 +08:00
|
|
|
|
private readonly IAliyunManger _aliyunManger;
|
2024-02-18 11:41:43 +08:00
|
|
|
|
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public AccountService(IUserRepository userRepository,
|
|
|
|
|
|
ICurrentUser currentUser,
|
2024-01-07 13:34:50 +08:00
|
|
|
|
IAccountManager accountManager,
|
2023-12-11 09:55:12 +08:00
|
|
|
|
ISqlSugarRepository<MenuEntity> menuRepository,
|
|
|
|
|
|
IDistributedCache<CaptchaPhoneCacheItem, CaptchaPhoneCacheKey> phoneCache,
|
2024-02-18 11:41:43 +08:00
|
|
|
|
IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache,
|
2023-12-11 09:55:12 +08:00
|
|
|
|
ICaptcha captcha,
|
2023-12-20 21:01:35 +08:00
|
|
|
|
IGuidGenerator guidGenerator,
|
2023-12-20 21:43:16 +08:00
|
|
|
|
IOptions<RbacOptions> options,
|
2024-02-18 11:41:43 +08:00
|
|
|
|
IAliyunManger aliyunManger)
|
2023-12-11 09:55:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
_userRepository = userRepository;
|
|
|
|
|
|
_currentUser = currentUser;
|
|
|
|
|
|
_accountManager = accountManager;
|
|
|
|
|
|
_menuRepository = menuRepository;
|
|
|
|
|
|
_phoneCache = phoneCache;
|
|
|
|
|
|
_captcha = captcha;
|
|
|
|
|
|
_guidGenerator = guidGenerator;
|
2023-12-20 21:01:35 +08:00
|
|
|
|
_rbacOptions = options.Value;
|
2023-12-20 21:43:16 +08:00
|
|
|
|
_aliyunManger = aliyunManger;
|
2024-02-18 11:41:43 +08:00
|
|
|
|
_userCache = userCache;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
}
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private IUserRepository _userRepository;
|
|
|
|
|
|
private ICurrentUser _currentUser;
|
2024-01-07 13:34:50 +08:00
|
|
|
|
private IAccountManager _accountManager;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private ISqlSugarRepository<MenuEntity> _menuRepository;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 效验图片登录验证码,无需和账号绑定
|
|
|
|
|
|
/// </summary>
|
2024-01-24 11:26:44 +08:00
|
|
|
|
[AllowAnonymous]
|
2023-04-13 21:12:06 +08:00
|
|
|
|
private void ValidationImageCaptcha(LoginInputVo input)
|
|
|
|
|
|
{
|
2023-12-20 21:01:35 +08:00
|
|
|
|
if (_rbacOptions.EnableCaptcha)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2023-12-20 21:01:35 +08:00
|
|
|
|
//登录不想要验证码 ,可不效验
|
|
|
|
|
|
if (!_captcha.Validate(input.Uuid, input.Code))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("验证码错误");
|
|
|
|
|
|
}
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-20 21:43:16 +08:00
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 登录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-01-24 11:26:44 +08:00
|
|
|
|
[AllowAnonymous]
|
2023-04-13 21:12:06 +08:00
|
|
|
|
public async Task<object> PostLoginAsync(LoginInputVo input)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(input.Password) || string.IsNullOrEmpty(input.UserName))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("请输入合理数据!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//效验验证码
|
2023-12-20 21:01:35 +08:00
|
|
|
|
ValidationImageCaptcha(input);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
UserEntity user = new();
|
2024-01-07 13:34:50 +08:00
|
|
|
|
//效验
|
2023-04-13 21:12:06 +08:00
|
|
|
|
await _accountManager.LoginValidationAsync(input.UserName, input.Password, x => user = x);
|
|
|
|
|
|
|
2024-01-07 13:34:50 +08:00
|
|
|
|
//获取token
|
|
|
|
|
|
var accessToken = await _accountManager.GetTokenByUserIdAsync(user.Id);
|
2024-01-24 11:26:44 +08:00
|
|
|
|
var refreshToken = _accountManager.CreateRefreshToken(user.Id);
|
2023-12-11 09:55:12 +08:00
|
|
|
|
|
2024-01-24 11:26:44 +08:00
|
|
|
|
return new { Token = accessToken, RefreshToken = refreshToken };
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-08 19:48:35 +08:00
|
|
|
|
|
2024-01-24 11:26:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 刷新token
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="refresh_token"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Authorize(AuthenticationSchemes = TokenTypeConst.Refresh)]
|
|
|
|
|
|
public async Task<object> PostRefreshAsync([FromQuery] string refresh_token)
|
|
|
|
|
|
{
|
2024-02-18 11:41:43 +08:00
|
|
|
|
var userId = CurrentUser.Id.Value;
|
|
|
|
|
|
var accessToken = await _accountManager.GetTokenByUserIdAsync(userId);
|
|
|
|
|
|
var refreshToken = _accountManager.CreateRefreshToken(userId);
|
|
|
|
|
|
return new { Token = accessToken, RefreshToken = refreshToken };
|
2024-01-24 11:26:44 +08:00
|
|
|
|
}
|
2023-12-11 09:55:12 +08:00
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成验证码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public async Task<CaptchaImageDto> GetCaptchaImageAsync()
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
var uuid = _guidGenerator.Create();
|
|
|
|
|
|
var captcha = _captcha.Generate(uuid.ToString());
|
|
|
|
|
|
return new CaptchaImageDto { Img = captcha.Bytes, Uuid = uuid };
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 验证电话号码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="str_handset"></param>
|
|
|
|
|
|
private async Task ValidationPhone(string str_handset)
|
|
|
|
|
|
{
|
2024-01-11 10:08:44 +08:00
|
|
|
|
var res = Regex.IsMatch(str_handset, @"^\d{11}$");
|
2023-04-13 21:12:06 +08:00
|
|
|
|
if (res == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("手机号码格式错误!请检查");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (await _userRepository.IsAnyAsync(x => x.Phone.ToString() == str_handset))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("该手机号已被注册!");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册 手机验证码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public async Task<object> PostCaptchaPhone(PhoneCaptchaImageDto input)
|
|
|
|
|
|
{
|
|
|
|
|
|
await ValidationPhone(input.Phone);
|
2023-12-11 09:55:12 +08:00
|
|
|
|
var value = await _phoneCache.GetAsync(new CaptchaPhoneCacheKey(input.Phone));
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
//防止暴刷
|
|
|
|
|
|
if (value is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"{input.Phone}已发送过验证码,10分钟后可重试");
|
|
|
|
|
|
}
|
|
|
|
|
|
//生成一个4位数的验证码
|
|
|
|
|
|
//发送短信,同时生成uuid
|
2023-12-11 09:55:12 +08:00
|
|
|
|
////key: 电话号码 value:验证码+uuid
|
2023-12-20 21:43:16 +08:00
|
|
|
|
var code = Guid.NewGuid().ToString().Substring(0, 4);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
var uuid = Guid.NewGuid();
|
2023-12-20 21:43:16 +08:00
|
|
|
|
await _aliyunManger.SendSmsAsync(input.Phone, code);
|
2023-12-11 09:55:12 +08:00
|
|
|
|
|
|
|
|
|
|
await _phoneCache.SetAsync(new CaptchaPhoneCacheKey(input.Phone), new CaptchaPhoneCacheItem(code), new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(10) });
|
|
|
|
|
|
return new
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
Uuid = uuid
|
|
|
|
|
|
};
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-20 21:43:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 效验电话验证码,需要与电话号码绑定
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private async Task ValidationPhoneCaptchaAsync(RegisterDto input)
|
|
|
|
|
|
{
|
2023-12-20 22:06:43 +08:00
|
|
|
|
var item = await _phoneCache.GetAsync(new CaptchaPhoneCacheKey(input.Phone.ToString()));
|
|
|
|
|
|
if (item is not null && item.Code.Equals($"{input.Code}"))
|
2023-12-20 21:43:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
//成功,需要清空
|
|
|
|
|
|
await _phoneCache.RemoveAsync(new CaptchaPhoneCacheKey(input.Phone.ToString()));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new UserFriendlyException("验证码错误");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-01 11:36:19 +08:00
|
|
|
|
private void ValidateUserName(RegisterDto input)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 正则表达式,匹配只包含数字和字母的字符串
|
|
|
|
|
|
string pattern = @"^[a-zA-Z0-9]+$";
|
|
|
|
|
|
|
|
|
|
|
|
bool isMatch = Regex.IsMatch(input.UserName, pattern);
|
|
|
|
|
|
if (!isMatch)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("用户名不能包含除【字母】与【数字】的其他字符");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-20 21:43:16 +08:00
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册,需要验证码通过
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[AllowAnonymous]
|
2023-04-15 12:25:47 +08:00
|
|
|
|
[UnitOfWork]
|
2024-01-07 13:34:50 +08:00
|
|
|
|
public async Task PostRegisterAsync(RegisterDto input)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2023-12-20 21:43:16 +08:00
|
|
|
|
if (_rbacOptions.EnableRegister == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("该系统暂未开放注册功能");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
if (input.UserName == UserConst.Admin)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("用户名无效注册!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (input.UserName.Length < 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("账号名需大于等于2位!");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (input.Password.Length < 6)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("密码需大于等于6位!");
|
|
|
|
|
|
}
|
2024-01-01 11:36:19 +08:00
|
|
|
|
|
|
|
|
|
|
//效验用户名
|
|
|
|
|
|
ValidateUserName(input);
|
|
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
//效验验证码,根据电话号码获取 value,比对验证码已经uuid
|
2023-12-20 21:43:16 +08:00
|
|
|
|
await ValidationPhoneCaptchaAsync(input);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-01-07 13:34:50 +08:00
|
|
|
|
//注册领域逻辑
|
|
|
|
|
|
await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-02-18 11:41:43 +08:00
|
|
|
|
/// 查询已登录的账户信息,已缓存
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2023-12-11 09:55:12 +08:00
|
|
|
|
[Route("account")]
|
2023-04-13 21:12:06 +08:00
|
|
|
|
[Authorize]
|
2023-12-11 09:55:12 +08:00
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
public async Task<UserRoleMenuDto> Get()
|
|
|
|
|
|
{
|
|
|
|
|
|
//通过鉴权jwt获取到用户的id
|
|
|
|
|
|
var userId = _currentUser.Id;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
if (userId is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("用户未登录");
|
|
|
|
|
|
}
|
2024-02-18 11:41:43 +08:00
|
|
|
|
//此处优先从缓存中获取
|
|
|
|
|
|
UserRoleMenuDto output = null;
|
|
|
|
|
|
var cacheData = await _userCache.GetAsync(new UserInfoCacheKey(userId.Value));
|
|
|
|
|
|
if (cacheData is not null)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2024-02-18 11:41:43 +08:00
|
|
|
|
output = cacheData.Info;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
2024-02-18 11:41:43 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = await _userRepository.GetUserAllInfoAsync(userId.Value);
|
|
|
|
|
|
//系统用户数据被重置,老前端访问重新授权
|
|
|
|
|
|
if (data is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new AbpAuthorizationException();
|
|
|
|
|
|
}
|
|
|
|
|
|
data.Menus.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
output = data;
|
|
|
|
|
|
|
|
|
|
|
|
var tokenExpiresMinuteTime = LazyServiceProvider.GetRequiredService<IOptions<JwtOptions>>().Value.ExpiresMinuteTime;
|
|
|
|
|
|
//将用户信息放入缓存,下次获取直接从缓存中获取即可,过期时间为token过期时间
|
|
|
|
|
|
await _userCache.SetAsync(new UserInfoCacheKey(userId.Value), new UserInfoCacheItem(data), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(tokenExpiresMinuteTime) });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return output;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前登录用户的前端路由
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Authorize]
|
2023-12-11 09:55:12 +08:00
|
|
|
|
[Route("account/Vue3Router")]
|
2023-04-13 21:12:06 +08:00
|
|
|
|
public async Task<List<Vue3RouterDto>> GetVue3Router()
|
|
|
|
|
|
{
|
|
|
|
|
|
var userId = _currentUser.Id;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
if (_currentUser.Id is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new AbpAuthorizationException("用户未登录");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
var data = await _userRepository.GetUserAllInfoAsync(userId ?? Guid.Empty);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
var menus = data.Menus.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
//为超级管理员直接给全部路由
|
|
|
|
|
|
if (UserConst.Admin.Equals(data.User.UserName))
|
|
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
menus = ObjectMapper.Map<List<MenuEntity>, List<MenuDto>>(await _menuRepository.GetListAsync());
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
//将后端菜单转换成前端路由,组件级别需要过滤
|
2023-12-11 09:55:12 +08:00
|
|
|
|
List<Vue3RouterDto> routers = ObjectMapper.Map<List<MenuDto>, List<MenuEntity>>(menus).Vue3RouterBuild();
|
2023-04-13 21:12:06 +08:00
|
|
|
|
return routers;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 退出登录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-02-18 11:41:43 +08:00
|
|
|
|
public async Task<bool> PostLogout()
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2024-02-18 11:41:43 +08:00
|
|
|
|
//通过鉴权jwt获取到用户的id
|
|
|
|
|
|
var userId = _currentUser.Id;
|
|
|
|
|
|
if (userId is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
// throw new UserFriendlyException("用户已退出");
|
|
|
|
|
|
}
|
|
|
|
|
|
await _userCache.RemoveAsync(new UserInfoCacheKey(userId.Value));
|
2024-01-07 13:34:50 +08:00
|
|
|
|
//Jwt去中心化登出,只需用记录日志即可
|
2024-02-18 11:41:43 +08:00
|
|
|
|
return true;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新密码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<bool> UpdatePasswordAsync(UpdatePasswordDto input)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (input.OldPassword.Equals(input.NewPassword))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("无效更新!输入的数据,新密码不能与老密码相同");
|
|
|
|
|
|
}
|
2023-12-11 09:55:12 +08:00
|
|
|
|
if (_currentUser.Id is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("用户未登录");
|
|
|
|
|
|
}
|
|
|
|
|
|
await _accountManager.UpdatePasswordAsync(_currentUser.Id ?? Guid.Empty, input.NewPassword, input.OldPassword);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重置密码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPut]
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public async Task<bool> RestPasswordAsync(Guid userId, RestPasswordDto input)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2023-07-19 01:24:27 +08:00
|
|
|
|
if (string.IsNullOrEmpty(input.Password))
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("重置密码不能为空!");
|
|
|
|
|
|
}
|
|
|
|
|
|
await _accountManager.RestPasswordAsync(userId, input.Password);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新头像
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<bool> UpdateIconAsync(UpdateIconDto input)
|
|
|
|
|
|
{
|
|
|
|
|
|
var entity = await _userRepository.GetByIdAsync(_currentUser.Id);
|
|
|
|
|
|
entity.Icon = input.Icon;
|
|
|
|
|
|
await _userRepository.UpdateAsync(entity);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|