feat: 完成后端消息推送用户信息
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.EventBus;
|
||||
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.Dtos;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Etos;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.EventHandlers
|
||||
{
|
||||
public class UserInfoHandler : ILocalEventHandler<UserRoleMenuQueryEventArgs>, ITransientDependency
|
||||
{
|
||||
private UserManager _userManager;
|
||||
public UserInfoHandler(UserManager userManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
}
|
||||
public async Task HandleEventAsync(UserRoleMenuQueryEventArgs eventData)
|
||||
{
|
||||
//数据库查询方式
|
||||
eventData.Result = new List<UserRoleMenuDto>();
|
||||
|
||||
//缓存查询方式
|
||||
foreach (var userId in eventData.UserIds)
|
||||
{
|
||||
eventData.Result.Add(await _userManager.Get(userId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,16 @@
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp.Authorization;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Volo.Abp.Guids;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.Rbac.Domain.Repositories;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Caches;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Options;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Managers
|
||||
@@ -10,11 +20,12 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
public readonly ISqlSugarRepository<UserEntity> _repository;
|
||||
public readonly ISqlSugarRepository<UserRoleEntity> _repositoryUserRole;
|
||||
public readonly ISqlSugarRepository<UserPostEntity> _repositoryUserPost;
|
||||
|
||||
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
|
||||
private readonly IGuidGenerator _guidGenerator;
|
||||
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator) =>
|
||||
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator) =
|
||||
(repository, repositoryUserRole, repositoryUserPost, guidGenerator);
|
||||
private IUserRepository _userRepository;
|
||||
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository) =>
|
||||
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository) =
|
||||
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository);
|
||||
|
||||
/// <summary>
|
||||
/// 给用户设置角色
|
||||
@@ -75,6 +86,46 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 查询用户信息,已缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<UserRoleMenuDto> Get(Guid userId)
|
||||
{
|
||||
//if (userId is null)
|
||||
//{
|
||||
// throw new UserFriendlyException("用户未登录");
|
||||
//}
|
||||
//此处优先从缓存中获取
|
||||
UserRoleMenuDto output = null;
|
||||
|
||||
var cacheData = await _userCache.GetAsync(new UserInfoCacheKey(userId));
|
||||
if (cacheData is not null)
|
||||
{
|
||||
output = cacheData.Info;
|
||||
}
|
||||
else
|
||||
{
|
||||
var data = await _userRepository.GetUserAllInfoAsync(userId);
|
||||
//系统用户数据被重置,老前端访问重新授权
|
||||
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), new UserInfoCacheItem(data), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(tokenExpiresMinuteTime) });
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user