feat: 完成ai message、session搭建
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user