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

@@ -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;
}
}
}