feat: 完成ai message、session搭建

This commit is contained in:
ccnetcore
2025-06-21 13:02:38 +08:00
parent 29985e2118
commit ac04e846fa
18 changed files with 353 additions and 58 deletions

View File

@@ -10,7 +10,7 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices
Task<UserRoleMenuDto> GetAsync();
Task<CaptchaImageDto> GetCaptchaImageAsync();
Task<LoginOutputDto> PostLoginAsync(LoginInputVo input);
Task PostRegisterAsync(RegisterDto input);
Task<LoginOutputDto> PostRegisterAsync(RegisterDto input);
Task<bool> RestPasswordAsync(Guid userId, RestPasswordDto input);
/// <summary>
@@ -25,7 +25,7 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices
/// <param name="userName"></param>
/// <param name="phone"></param>
/// <returns></returns>
Task<UserRoleMenuDto?> GetAsync(string? userName,long? phone);
Task<UserRoleMenuDto?> GetAsync(string? userName, long? phone);
/// <summary>
/// 校验电话验证码,需要与电话号码绑定
@@ -40,4 +40,4 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices
/// <param name="input"></param>
Task PostTempRegisterAsync(RegisterDto input);
}
}
}

View File

@@ -5,11 +5,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Authorization;
using Volo.Abp.Caching;
@@ -81,8 +77,8 @@ namespace Yi.Framework.Rbac.Application.Services
/// <summary>
/// 校验图片登录验证码,无需和账号绑定
/// </summary>
[RemoteService(isEnabled:false)]
public void ValidationImageCaptcha(string? uuid,string? code )
[RemoteService(isEnabled: false)]
private void ValidationImageCaptcha(string? uuid, string? code)
{
if (_rbacOptions.EnableCaptcha)
{
@@ -109,7 +105,7 @@ namespace Yi.Framework.Rbac.Application.Services
}
//校验验证码
ValidationImageCaptcha(input.Uuid,input.Code);
ValidationImageCaptcha(input.Uuid, input.Code);
UserAggregateRoot user = new();
//校验
@@ -174,7 +170,7 @@ namespace Yi.Framework.Rbac.Application.Services
/// <summary>
/// 验证电话号码
/// </summary>
/// <param name="str_handset"></param>
/// <param name="phone"></param>
private async Task ValidationPhone(string phone)
{
var res = Regex.IsMatch(phone, @"^\d{11}$");
@@ -207,7 +203,7 @@ namespace Yi.Framework.Rbac.Application.Services
{
return await PostCaptchaPhoneAsync(ValidationPhoneTypeEnum.RetrievePassword, input);
}
/// <summary>
/// 手机验证码-绑定
/// </summary>
@@ -219,20 +215,20 @@ namespace Yi.Framework.Rbac.Application.Services
{
return await PostCaptchaPhoneAsync(ValidationPhoneTypeEnum.Bind, input);
}
/// <summary>
/// 手机验证码-需通过图形验证码
/// </summary>
/// <returns></returns>
[RemoteService(isEnabled:false)]
[RemoteService(isEnabled: false)]
private async Task<object> PostCaptchaPhoneAsync(ValidationPhoneTypeEnum validationPhoneType,
PhoneCaptchaImageDto input)
{
//验证uuid 和 验证码
ValidationImageCaptcha(input.Uuid,input.Code);
ValidationImageCaptcha(input.Uuid, input.Code);
await ValidationPhone(input.Phone);
if (validationPhoneType == ValidationPhoneTypeEnum.Register &&
await _userRepository.IsAnyAsync(x => x.Phone.ToString() == input.Phone))
{
@@ -310,7 +306,7 @@ namespace Yi.Framework.Rbac.Application.Services
/// <returns></returns>
[AllowAnonymous]
[UnitOfWork]
public async Task PostRegisterAsync(RegisterDto input)
public async Task<LoginOutputDto> PostRegisterAsync(RegisterDto input)
{
if (_rbacOptions.EnableRegister == false)
{
@@ -321,20 +317,22 @@ namespace Yi.Framework.Rbac.Application.Services
{
throw new UserFriendlyException("手机号不能为空");
}
//临时账号
if (input.UserName.StartsWith("ls_"))
{
throw new UserFriendlyException("注册账号不能以ls_字符开头");
}
if (_rbacOptions.EnableCaptcha)
{
//校验验证码,根据电话号码获取 value比对验证码已经uuid
await ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum.Register, input.Phone.Value, input.Code);
}
//注册领域逻辑
await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone, input.Nick);
//注册之后免再次登录直接给前端token
var userId = await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone, input.Nick);
return await this.PostLoginAsync(userId);
}
/// <summary>
@@ -342,7 +340,7 @@ namespace Yi.Framework.Rbac.Application.Services
/// 不需要验证,为了给第三方使用,例如微信小程序,后续可通过绑定操作,进行账号合并
/// </summary>
/// <param name="input"></param>
[RemoteService(isEnabled:false)]
[RemoteService(isEnabled: false)]
public async Task PostTempRegisterAsync(RegisterDto input)
{
//注册领域逻辑
@@ -423,13 +421,17 @@ namespace Yi.Framework.Rbac.Application.Services
{
//将后端菜单转换成前端路由,组件级别需要过滤
output =
ObjectMapper.Map<List<MenuDto>, List<MenuAggregateRoot>>(menus.Where(x=>x.MenuSource==MenuSourceEnum.Ruoyi).ToList()).Vue3RuoYiRouterBuild();
ObjectMapper
.Map<List<MenuDto>, List<MenuAggregateRoot>>(menus
.Where(x => x.MenuSource == MenuSourceEnum.Ruoyi).ToList()).Vue3RuoYiRouterBuild();
}
else if (routerType == "pure")
{
//将后端菜单转换成前端路由,组件级别需要过滤
output =
ObjectMapper.Map<List<MenuDto>, List<MenuAggregateRoot>>(menus.Where(x=>x.MenuSource==MenuSourceEnum.Pure).ToList()).Vue3PureRouterBuild();
ObjectMapper
.Map<List<MenuDto>, List<MenuAggregateRoot>>(menus
.Where(x => x.MenuSource == MenuSourceEnum.Pure).ToList()).Vue3PureRouterBuild();
}
return output;

View File

@@ -272,11 +272,12 @@ namespace Yi.Framework.Rbac.Domain.Managers
/// <param name="password"></param>
/// <param name="phone"></param>
/// <returns></returns>
public async Task RegisterAsync(string userName, string password, long? phone,string? nick)
public async Task<Guid> RegisterAsync(string userName, string password, long? phone,string? nick)
{
var user = new UserAggregateRoot(userName, password, phone,nick);
await _userManager.CreateAsync(user);
var userId=await _userManager.CreateAsync(user);
await _userManager.SetDefautRoleAsync(user.Id);
return userId;
}
}

View File

@@ -14,7 +14,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
string CreateRefreshToken(Guid userId);
Task<string> GetTokenByUserIdAsync(Guid userId,Action<UserRoleMenuDto>? getUserInfo=null);
Task LoginValidationAsync(string userName, string password, Action<UserAggregateRoot>? userAction = null);
Task RegisterAsync(string userName, string password, long? phone,string? nick);
Task<Guid> RegisterAsync(string userName, string password, long? phone,string? nick);
Task<bool> RestPasswordAsync(Guid userId, string password);
Task UpdatePasswordAsync(Guid userId, string newPassword, string oldPassword);
}

View File

@@ -29,9 +29,16 @@ namespace Yi.Framework.Rbac.Domain.Managers
private readonly IGuidGenerator _guidGenerator;
private IUserRepository _userRepository;
private ILocalEventBus _localEventBus;
public UserManager(ISqlSugarRepository<UserAggregateRoot> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository, ILocalEventBus localEventBus, ISqlSugarRepository<RoleAggregateRoot> roleRepository) =>
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository, _localEventBus, _roleRepository) =
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository, localEventBus, roleRepository);
public UserManager(ISqlSugarRepository<UserAggregateRoot> repository,
ISqlSugarRepository<UserRoleEntity> repositoryUserRole,
ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator,
IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository,
ILocalEventBus localEventBus, ISqlSugarRepository<RoleAggregateRoot> roleRepository) =>
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository,
_localEventBus, _roleRepository) =
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository,
localEventBus, roleRepository);
/// <summary>
/// 给用户设置角色
@@ -56,6 +63,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
userRoleEntities.Add(new UserRoleEntity() { UserId = userId, RoleId = roleId });
}
//一次性批量添加
await _repositoryUserRole.InsertRangeAsync(userRoleEntities);
}
@@ -88,7 +96,6 @@ namespace Yi.Framework.Rbac.Domain.Managers
//一次性批量添加
await _repositoryUserPost.InsertRangeAsync(userPostEntities);
}
}
}
@@ -96,7 +103,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
/// 创建用户
/// </summary>
/// <returns></returns>
public async Task CreateAsync(UserAggregateRoot userEntity)
public async Task<Guid> CreateAsync(UserAggregateRoot userEntity)
{
//校验用户名
ValidateUserName(userEntity);
@@ -111,7 +118,6 @@ namespace Yi.Framework.Rbac.Domain.Managers
if (await _repository.IsAnyAsync(x => x.Phone == userEntity.Phone))
{
throw new UserFriendlyException(UserConst.Phone_Repeat);
}
}
@@ -123,10 +129,8 @@ namespace Yi.Framework.Rbac.Domain.Managers
var entity = await _repository.InsertReturnEntityAsync(userEntity);
userEntity = entity;
await _localEventBus.PublishAsync(new UserCreateEventArgs(entity.Id));
return entity.Id;
}
@@ -174,37 +178,43 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
throw new AbpAuthorizationException();
}
//data.Menus.Clear();
// output = data;
return data;
// var output = await GetInfoByCacheAsync(userId);
// return output;
}
private async Task<UserRoleMenuDto> GetInfoByCacheAsync(Guid userId)
{
//此处优先从缓存中获取
UserRoleMenuDto output = null;
var tokenExpiresMinuteTime = LazyServiceProvider.GetRequiredService<IOptions<JwtOptions>>().Value.ExpiresMinuteTime;
var tokenExpiresMinuteTime =
LazyServiceProvider.GetRequiredService<IOptions<JwtOptions>>().Value.ExpiresMinuteTime;
var cacheData = await _userCache.GetOrAddAsync(new UserInfoCacheKey(userId),
async () =>
{
var user = await _userRepository.GetUserAllInfoAsync(userId);
var data = EntityMapToDto(user);
//系统用户数据被重置,老前端访问重新授权
if (data is null)
{
throw new AbpAuthorizationException();
}
//data.Menus.Clear();
output = data;
return new UserInfoCacheItem(data);
},
() => new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(tokenExpiresMinuteTime) });
async () =>
{
var user = await _userRepository.GetUserAllInfoAsync(userId);
var data = EntityMapToDto(user);
//系统用户数据被重置,老前端访问重新授权
if (data is null)
{
throw new AbpAuthorizationException();
}
//data.Menus.Clear();
output = data;
return new UserInfoCacheItem(data);
},
() => new DistributedCacheEntryOptions
{ AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(tokenExpiresMinuteTime) });
if (cacheData is not null)
{
output = cacheData.Info;
}
return output!;
}
@@ -221,14 +231,13 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
output.Add(await GetInfoByCacheAsync(userId));
}
return output;
}
private UserRoleMenuDto EntityMapToDto(UserAggregateRoot user)
{
var userRoleMenu = new UserRoleMenuDto();
//首先获取到该用户全部信息,导航到角色、菜单,(菜单需要去重,完全交给Set来处理即可)
if (user is null)
@@ -236,6 +245,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
//为了解决token前端缓存后端数据库重新dbseed
throw new UserFriendlyException($"数据错误,查询用户不存在,请重新登录");
}
user.EncryPassword.Password = string.Empty;
user.EncryPassword.Salt = string.Empty;
@@ -264,6 +274,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
userRoleMenu.PermissionCodes.Add(menu.PermissionCode);
}
userRoleMenu.Menus.Add(menu.Adapt<MenuDto>());
}
}
@@ -279,5 +290,4 @@ namespace Yi.Framework.Rbac.Domain.Managers
return userRoleMenu;
}
}
}