2024-09-24 07:13:14 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Lazy.Captcha.Core;
|
2024-09-05 23:10:40 +08:00
|
|
|
|
using Mapster;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2024-09-05 23:10:40 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
2024-02-18 11:41:43 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-08-16 17:57:58 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
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;
|
2024-08-16 17:57:58 +08:00
|
|
|
|
using Volo.Abp.EventBus.Local;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Volo.Abp.Guids;
|
|
|
|
|
|
using Volo.Abp.Uow;
|
|
|
|
|
|
using Volo.Abp.Users;
|
2024-08-16 17:57:58 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Enums;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Etos;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
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;
|
2024-10-03 01:10:32 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Enums;
|
2024-09-05 23:10:40 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Etos;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2024-08-16 17:57:58 +08:00
|
|
|
|
protected ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus>();
|
2023-12-11 09:55:12 +08:00
|
|
|
|
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;
|
2024-04-08 18:57:59 +08:00
|
|
|
|
private UserManager _userManager;
|
2024-09-05 23:10:40 +08:00
|
|
|
|
private IHttpContextAccessor _httpContextAccessor;
|
2024-10-03 01:10:32 +08:00
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public AccountService(IUserRepository userRepository,
|
|
|
|
|
|
ICurrentUser currentUser,
|
2024-01-07 13:34:50 +08:00
|
|
|
|
IAccountManager accountManager,
|
2024-05-22 14:35:08 +08:00
|
|
|
|
ISqlSugarRepository<MenuAggregateRoot> menuRepository,
|
2023-12-11 09:55:12 +08:00
|
|
|
|
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-04-08 18:57:59 +08:00
|
|
|
|
IAliyunManger aliyunManger,
|
2024-09-05 23:10:40 +08:00
|
|
|
|
UserManager userManager, IHttpContextAccessor httpContextAccessor)
|
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;
|
2024-04-08 18:57:59 +08:00
|
|
|
|
_userManager = userManager;
|
2024-09-05 23:10:40 +08:00
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
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;
|
2024-05-22 14:35:08 +08:00
|
|
|
|
private ISqlSugarRepository<MenuAggregateRoot> _menuRepository;
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// <summary>
|
2024-03-07 11:32:49 +08:00
|
|
|
|
/// 校验图片登录验证码,无需和账号绑定
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// </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
|
|
|
|
{
|
2024-03-07 11:32:49 +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("请输入合理数据!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-07 11:32:49 +08:00
|
|
|
|
//校验验证码
|
2023-12-20 21:01:35 +08:00
|
|
|
|
ValidationImageCaptcha(input);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
2024-05-22 14:35:08 +08:00
|
|
|
|
UserAggregateRoot user = new();
|
2024-03-07 11:32:49 +08:00
|
|
|
|
//校验
|
2023-04-13 21:12:06 +08:00
|
|
|
|
await _accountManager.LoginValidationAsync(input.UserName, input.Password, x => user = x);
|
|
|
|
|
|
|
2024-09-05 23:10:40 +08:00
|
|
|
|
var userInfo = new UserRoleMenuDto();
|
2024-01-07 13:34:50 +08:00
|
|
|
|
//获取token
|
2024-09-05 23:10:40 +08:00
|
|
|
|
var accessToken = await _accountManager.GetTokenByUserIdAsync(user.Id, (info) => userInfo = info);
|
2024-01-24 11:26:44 +08:00
|
|
|
|
var refreshToken = _accountManager.CreateRefreshToken(user.Id);
|
2023-12-11 09:55:12 +08:00
|
|
|
|
|
2024-09-05 23:10:40 +08:00
|
|
|
|
//这里抛出一个登录的事件,也可以在全部流程走完,在应用层组装
|
|
|
|
|
|
if (_httpContextAccessor.HttpContext is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var loginEntity = new LoginLogAggregateRoot().GetInfoByHttpContext(_httpContextAccessor.HttpContext);
|
|
|
|
|
|
var loginEto = loginEntity.Adapt<LoginEventArgs>();
|
|
|
|
|
|
loginEto.UserName = userInfo.User.UserName;
|
|
|
|
|
|
loginEto.UserId = userInfo.User.Id;
|
|
|
|
|
|
await LocalEventBus.PublishAsync(loginEto);
|
|
|
|
|
|
}
|
2024-10-03 01:10:32 +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());
|
2024-10-14 16:49:33 +08:00
|
|
|
|
var enableCaptcha = _rbacOptions.EnableCaptcha;
|
|
|
|
|
|
return new CaptchaImageDto { Img = captcha.Bytes, Uuid = uuid,IsEnableCaptcha= enableCaptcha };
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 验证电话号码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="str_handset"></param>
|
2024-10-11 16:48:11 +08:00
|
|
|
|
private async Task ValidationPhone(string phone)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2024-10-11 16:48:11 +08:00
|
|
|
|
var res = Regex.IsMatch(phone, @"^\d{11}$");
|
2023-04-13 21:12:06 +08:00
|
|
|
|
if (res == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("手机号码格式错误!请检查");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-03 01:10:32 +08:00
|
|
|
|
/// 手机验证码-注册
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// </summary>
|
2024-10-03 01:10:32 +08:00
|
|
|
|
/// <param name="input"></param>
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// <returns></returns>
|
2024-10-04 00:00:44 +08:00
|
|
|
|
[HttpPost("account/captcha-phone")]
|
2023-04-13 21:12:06 +08:00
|
|
|
|
[AllowAnonymous]
|
2024-10-03 01:10:32 +08:00
|
|
|
|
public async Task<object> PostCaptchaPhoneForRegisterAsync(PhoneCaptchaImageDto input)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await PostCaptchaPhoneAsync(ValidationPhoneTypeEnum.Register, input);
|
|
|
|
|
|
}
|
2024-10-04 00:00:44 +08:00
|
|
|
|
|
2024-10-03 01:10:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 手机验证码-找回密码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-10-04 00:00:44 +08:00
|
|
|
|
[HttpPost("account/captcha-phone/repassword")]
|
2024-10-03 01:10:32 +08:00
|
|
|
|
public async Task<object> PostCaptchaPhoneForRetrievePasswordAsync(PhoneCaptchaImageDto input)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await PostCaptchaPhoneAsync(ValidationPhoneTypeEnum.RetrievePassword, input);
|
|
|
|
|
|
}
|
2024-10-04 00:00:44 +08:00
|
|
|
|
|
2024-10-03 01:10:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 手机验证码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private async Task<object> PostCaptchaPhoneAsync(ValidationPhoneTypeEnum validationPhoneType,
|
|
|
|
|
|
PhoneCaptchaImageDto input)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
await ValidationPhone(input.Phone);
|
2024-10-13 01:04:58 +08:00
|
|
|
|
|
|
|
|
|
|
//注册的手机号验证,是不能已经注册过的
|
|
|
|
|
|
if (validationPhoneType == ValidationPhoneTypeEnum.Register&& await _userRepository.IsAnyAsync(x => x.Phone.ToString() == input.Phone))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("该手机号已被注册!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-03 01:10:32 +08:00
|
|
|
|
var value = await _phoneCache.GetAsync(new CaptchaPhoneCacheKey(validationPhoneType, input.Phone));
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
//防止暴刷
|
|
|
|
|
|
if (value is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"{input.Phone}已发送过验证码,10分钟后可重试");
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
//生成一个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
|
|
|
|
|
2024-10-11 16:48:11 +08:00
|
|
|
|
await _phoneCache.SetAsync(new CaptchaPhoneCacheKey(validationPhoneType, input.Phone),
|
2024-10-03 01:10:32 +08:00
|
|
|
|
new CaptchaPhoneCacheItem(code),
|
2024-08-16 17:57:58 +08:00
|
|
|
|
new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(10) });
|
2023-12-11 09:55:12 +08:00
|
|
|
|
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>
|
2024-03-07 11:32:49 +08:00
|
|
|
|
/// 校验电话验证码,需要与电话号码绑定
|
2023-12-20 21:43:16 +08:00
|
|
|
|
/// </summary>
|
2024-10-03 01:10:32 +08:00
|
|
|
|
private async Task ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum validationPhoneType, long phone,
|
|
|
|
|
|
string code)
|
2023-12-20 21:43:16 +08:00
|
|
|
|
{
|
2024-10-03 01:10:32 +08:00
|
|
|
|
var item = await _phoneCache.GetAsync(new CaptchaPhoneCacheKey(validationPhoneType, phone.ToString()));
|
|
|
|
|
|
if (item is not null && item.Code.Equals($"{code}"))
|
2023-12-20 21:43:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
//成功,需要清空
|
2024-10-03 01:10:32 +08:00
|
|
|
|
await _phoneCache.RemoveAsync(new CaptchaPhoneCacheKey(validationPhoneType, code.ToString()));
|
2023-12-20 21:43:16 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2023-12-20 21:43:16 +08:00
|
|
|
|
throw new UserFriendlyException("验证码错误");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-03 01:10:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 找回密码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[UnitOfWork]
|
2024-10-04 00:00:44 +08:00
|
|
|
|
public async Task<string> PostRetrievePasswordAsync(RetrievePasswordDto input)
|
2024-10-03 01:10:32 +08:00
|
|
|
|
{
|
2024-10-04 00:00:44 +08:00
|
|
|
|
//校验验证码,根据电话号码获取 value,比对验证码已经uuid
|
|
|
|
|
|
await ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum.RetrievePassword, input.Phone, input.Code);
|
|
|
|
|
|
|
2024-10-03 01:10:32 +08:00
|
|
|
|
var entity = await _userRepository.GetFirstAsync(x => x.Phone == input.Phone);
|
|
|
|
|
|
if (entity is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("该手机号码未注册");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _accountManager.RestPasswordAsync(entity.Id, input.Password);
|
2024-10-04 00:00:44 +08:00
|
|
|
|
|
|
|
|
|
|
return entity.UserName;
|
2024-10-03 01:10:32 +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("该系统暂未开放注册功能");
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2024-04-29 17:50:51 +08:00
|
|
|
|
if (_rbacOptions.EnableCaptcha)
|
|
|
|
|
|
{
|
|
|
|
|
|
//校验验证码,根据电话号码获取 value,比对验证码已经uuid
|
2024-10-03 01:10:32 +08:00
|
|
|
|
await ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum.Register, input.Phone, input.Code);
|
2024-04-29 17:50:51 +08:00
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2024-01-07 13:34:50 +08:00
|
|
|
|
//注册领域逻辑
|
2024-10-03 01:10:32 +08:00
|
|
|
|
await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone, input.Nick);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-03 01:10:32 +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]
|
2024-04-29 17:50:51 +08:00
|
|
|
|
public async Task<UserRoleMenuDto> GetAsync()
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
//通过鉴权jwt获取到用户的id
|
|
|
|
|
|
var userId = _currentUser.Id;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
if (userId is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("用户未登录");
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2024-02-18 11:41:43 +08:00
|
|
|
|
//此处优先从缓存中获取
|
2024-04-09 17:45:12 +08:00
|
|
|
|
var output = await _userManager.GetInfoAsync(userId.Value);
|
2024-02-18 11:41:43 +08:00
|
|
|
|
return output;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前登录用户的前端路由
|
2024-09-07 02:17:07 +08:00
|
|
|
|
/// 支持ruoyi/pure
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Authorize]
|
2024-09-07 02:17:07 +08:00
|
|
|
|
[Route("account/Vue3Router/{routerType?}")]
|
2024-10-03 01:10:32 +08:00
|
|
|
|
public async Task<object> GetVue3Router([FromRoute] string? routerType)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
var userId = _currentUser.Id;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
if (_currentUser.Id is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new AbpAuthorizationException("用户未登录");
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2024-04-09 17:45:12 +08:00
|
|
|
|
var data = await _userManager.GetInfoAsync(userId!.Value);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
var menus = data.Menus.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
//为超级管理员直接给全部路由
|
|
|
|
|
|
if (UserConst.Admin.Equals(data.User.UserName))
|
|
|
|
|
|
{
|
2024-05-22 14:35:08 +08:00
|
|
|
|
menus = ObjectMapper.Map<List<MenuAggregateRoot>, List<MenuDto>>(await _menuRepository.GetListAsync());
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2024-09-07 02:17:07 +08:00
|
|
|
|
object output = null;
|
2024-10-03 01:10:32 +08:00
|
|
|
|
if (routerType is null || routerType == "ruoyi")
|
2024-09-07 02:17:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
//将后端菜单转换成前端路由,组件级别需要过滤
|
|
|
|
|
|
output =
|
|
|
|
|
|
ObjectMapper.Map<List<MenuDto>, List<MenuAggregateRoot>>(menus).Vue3RuoYiRouterBuild();
|
|
|
|
|
|
}
|
2024-10-03 01:10:32 +08:00
|
|
|
|
else if (routerType == "pure")
|
2024-09-07 02:17:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
//将后端菜单转换成前端路由,组件级别需要过滤
|
|
|
|
|
|
output =
|
|
|
|
|
|
ObjectMapper.Map<List<MenuDto>, List<MenuAggregateRoot>>(menus).Vue3PureRouterBuild();
|
|
|
|
|
|
}
|
2024-10-03 01:10:32 +08:00
|
|
|
|
|
2024-09-07 02:17:07 +08:00
|
|
|
|
return output;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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;
|
2024-04-08 18:57:59 +08:00
|
|
|
|
// throw new UserFriendlyException("用户已退出");
|
2024-02-18 11:41:43 +08:00
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2024-02-18 11:41:43 +08:00
|
|
|
|
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("无效更新!输入的数据,新密码不能与老密码相同");
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
if (_currentUser.Id is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("用户未登录");
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
|
|
|
|
|
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("重置密码不能为空!");
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
await _accountManager.RestPasswordAsync(userId, input.Password);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新头像
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<bool> UpdateIconAsync(UpdateIconDto input)
|
|
|
|
|
|
{
|
2024-09-24 07:13:14 +00:00
|
|
|
|
Guid userId=input.UserId == null?_currentUser.GetId():input.UserId.Value;
|
2024-09-24 14:44:06 +08:00
|
|
|
|
|
|
|
|
|
|
var entity = await _userRepository.GetByIdAsync(userId);
|
|
|
|
|
|
|
2024-08-16 17:57:58 +08:00
|
|
|
|
if (entity.Icon == input.Icon)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
entity.Icon = input.Icon;
|
|
|
|
|
|
await _userRepository.UpdateAsync(entity);
|
|
|
|
|
|
|
2024-08-16 17:57:58 +08:00
|
|
|
|
//发布更新头像任务事件
|
|
|
|
|
|
await this.LocalEventBus.PublishAsync(
|
2024-09-24 14:44:06 +08:00
|
|
|
|
new AssignmentEventArgs(AssignmentRequirementTypeEnum.UpdateIcon, userId), false);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-16 17:57:58 +08:00
|
|
|
|
}
|