feat: 完成后端消息推送用户信息
This commit is contained in:
@@ -13,6 +13,7 @@ using Yi.Framework.ChatHub.Application.Contracts.Dtos;
|
|||||||
using Yi.Framework.ChatHub.Domain.Managers;
|
using Yi.Framework.ChatHub.Domain.Managers;
|
||||||
using Yi.Framework.ChatHub.Domain.Shared.Enums;
|
using Yi.Framework.ChatHub.Domain.Shared.Enums;
|
||||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||||
|
using Yi.Framework.Rbac.Domain.Shared.Etos;
|
||||||
|
|
||||||
namespace Yi.Framework.ChatHub.Application.Services
|
namespace Yi.Framework.ChatHub.Application.Services
|
||||||
{
|
{
|
||||||
@@ -30,8 +31,14 @@ namespace Yi.Framework.ChatHub.Application.Services
|
|||||||
public async Task SendPersonalMessageAsync(PersonalMessageInputDto input)
|
public async Task SendPersonalMessageAsync(PersonalMessageInputDto input)
|
||||||
{
|
{
|
||||||
var mesageContext = MessageContext.CreatePersonal(input.Content, input.UserId, CurrentUser.Id!.Value);
|
var mesageContext = MessageContext.CreatePersonal(input.Content, input.UserId, CurrentUser.Id!.Value);
|
||||||
await _userMessageManager.SendMessageAsync(mesageContext);
|
var userRoleMenuQuery = new UserRoleMenuQueryEventArgs(CurrentUser.Id!.Value, input.UserId);
|
||||||
|
|
||||||
|
//调用用户领域事件,获取用户信息,第一个发送者用户信息,第二个为接收者用户信息
|
||||||
|
await _localEventBus.PublishAsync(userRoleMenuQuery);
|
||||||
|
mesageContext.SetUserInfo(userRoleMenuQuery.Result.First(), userRoleMenuQuery.Result.Last());
|
||||||
|
|
||||||
|
|
||||||
|
await _userMessageManager.SendMessageAsync(mesageContext);
|
||||||
await _userMessageManager.CreateMessageStoreAsync(mesageContext);
|
await _userMessageManager.CreateMessageStoreAsync(mesageContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,11 +54,16 @@ namespace Yi.Framework.ChatHub.Application.Services
|
|||||||
//领域调用,群主消息调用bbs领域
|
//领域调用,群主消息调用bbs领域
|
||||||
|
|
||||||
//如果钱钱不足,将自动断言
|
//如果钱钱不足,将自动断言
|
||||||
await _localEventBus.PublishAsync<MoneyChangeEventArgs>(new MoneyChangeEventArgs { UserId = CurrentUser.Id.Value, Number = -1 });
|
await _localEventBus.PublishAsync<MoneyChangeEventArgs>(new MoneyChangeEventArgs { UserId = CurrentUser.Id.Value, Number = -1 },false);
|
||||||
|
|
||||||
var mesageContext = MessageContext.CreateAll(input.Content, CurrentUser.Id!.Value);
|
var mesageContext = MessageContext.CreateAll(input.Content, CurrentUser.Id!.Value);
|
||||||
await _userMessageManager.SendMessageAsync(mesageContext);
|
UserRoleMenuQueryEventArgs userRoleMenuQuery = new UserRoleMenuQueryEventArgs(CurrentUser.Id!.Value);
|
||||||
|
|
||||||
|
//调用用户领域事件,获取用户信息,第一个发送者用户信息,第二个为接收者用户信息
|
||||||
|
await _localEventBus.PublishAsync(userRoleMenuQuery, false);
|
||||||
|
mesageContext.SetUserInfo(userRoleMenuQuery.Result.First(),null);
|
||||||
|
|
||||||
|
await _userMessageManager.SendMessageAsync(mesageContext);
|
||||||
await _userMessageManager.CreateMessageStoreAsync(mesageContext);
|
await _userMessageManager.CreateMessageStoreAsync(mesageContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +76,8 @@ namespace Yi.Framework.ChatHub.Application.Services
|
|||||||
[RemoteService(IsEnabled = false)]
|
[RemoteService(IsEnabled = false)]
|
||||||
public async Task<List<MessageContext>> GetListAsync(ChatMessageGetListInput input)
|
public async Task<List<MessageContext>> GetListAsync(ChatMessageGetListInput input)
|
||||||
{
|
{
|
||||||
|
//需要关联用户信息
|
||||||
|
|
||||||
var entities = await _userMessageManager._repository._DbQueryable
|
var entities = await _userMessageManager._repository._DbQueryable
|
||||||
.WhereIF(input.MessageType is not null, x => x.MessageType == input.MessageType)
|
.WhereIF(input.MessageType is not null, x => x.MessageType == input.MessageType)
|
||||||
.WhereIF(input.ReceiveId is not null, x => x.ReceiveId == input.ReceiveId)
|
.WhereIF(input.ReceiveId is not null, x => x.ReceiveId == input.ReceiveId)
|
||||||
@@ -83,6 +97,8 @@ namespace Yi.Framework.ChatHub.Application.Services
|
|||||||
[HttpGet("chat-message/account")]
|
[HttpGet("chat-message/account")]
|
||||||
public async Task<List<MessageContext>> GetAccountMessageListAsync()
|
public async Task<List<MessageContext>> GetAccountMessageListAsync()
|
||||||
{
|
{
|
||||||
|
//需要关联用户信息
|
||||||
|
|
||||||
//默认显示1个月的个人数据
|
//默认显示1个月的个人数据
|
||||||
var userId = CurrentUser.Id!.Value;
|
var userId = CurrentUser.Id!.Value;
|
||||||
//3个类型数据
|
//3个类型数据
|
||||||
|
|||||||
@@ -5,14 +5,15 @@ using System.Text;
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Yi.Framework.ChatHub.Domain.Shared.Enums;
|
using Yi.Framework.ChatHub.Domain.Shared.Enums;
|
||||||
|
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
||||||
|
|
||||||
namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||||
{
|
{
|
||||||
public class MessageContext
|
public class MessageContext
|
||||||
{
|
{
|
||||||
public static MessageContext CreatePersonal(string content, Guid userId,Guid sendUserId)
|
public static MessageContext CreatePersonal(string content, Guid userId, Guid sendUserId)
|
||||||
{
|
{
|
||||||
return new MessageContext() { MessageType = MessageTypeEnum.Personal, Content = content, ReceiveId = userId ,SendUserId= sendUserId };
|
return new MessageContext() { MessageType = MessageTypeEnum.Personal, Content = content, ReceiveId = userId, SendUserId = sendUserId };
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MessageContext CreateAll(string content, Guid sendUserId)
|
public static MessageContext CreateAll(string content, Guid sendUserId)
|
||||||
@@ -20,7 +21,12 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
|||||||
return new MessageContext() { MessageType = MessageTypeEnum.All, Content = content, SendUserId = sendUserId };
|
return new MessageContext() { MessageType = MessageTypeEnum.All, Content = content, SendUserId = sendUserId };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetUserInfo(UserRoleMenuDto sendUserInfo, UserRoleMenuDto? receiveInfo)
|
||||||
|
{
|
||||||
|
this.SendUserInfo = sendUserInfo;
|
||||||
|
this.ReceiveInfo = receiveInfo;
|
||||||
|
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息类型
|
/// 消息类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -31,15 +37,25 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Guid? ReceiveId { get; set; }
|
public Guid? ReceiveId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 接收者用户信息
|
||||||
|
/// </summary>
|
||||||
|
public UserRoleMenuDto? ReceiveInfo { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送者的用户id
|
/// 发送者的用户id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Guid SendUserId { get; set; }
|
public Guid SendUserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送者用户信息
|
||||||
|
/// </summary>
|
||||||
|
public UserRoleMenuDto SendUserInfo { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息内容
|
/// 消息内容
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Content { get; set; }
|
public string Content { get; set; }
|
||||||
public DateTime CreationTime { get;protected set; }=DateTime.Now;
|
public DateTime CreationTime { get; protected set; } = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,5 +11,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\bbs\Yi.Framework.Bbs.Domain.Shared\Yi.Framework.Bbs.Domain.Shared.csproj" />
|
<ProjectReference Include="..\..\bbs\Yi.Framework.Bbs.Domain.Shared\Yi.Framework.Bbs.Domain.Shared.csproj" />
|
||||||
|
<ProjectReference Include="..\..\rbac\Yi.Framework.Rbac.Domain.Shared\Yi.Framework.Rbac.Domain.Shared.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
|||||||
private readonly RbacOptions _rbacOptions;
|
private readonly RbacOptions _rbacOptions;
|
||||||
private readonly IAliyunManger _aliyunManger;
|
private readonly IAliyunManger _aliyunManger;
|
||||||
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
|
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
|
||||||
|
private UserManager _userManager;
|
||||||
public AccountService(IUserRepository userRepository,
|
public AccountService(IUserRepository userRepository,
|
||||||
ICurrentUser currentUser,
|
ICurrentUser currentUser,
|
||||||
IAccountManager accountManager,
|
IAccountManager accountManager,
|
||||||
@@ -44,7 +45,8 @@ namespace Yi.Framework.Rbac.Application.Services
|
|||||||
ICaptcha captcha,
|
ICaptcha captcha,
|
||||||
IGuidGenerator guidGenerator,
|
IGuidGenerator guidGenerator,
|
||||||
IOptions<RbacOptions> options,
|
IOptions<RbacOptions> options,
|
||||||
IAliyunManger aliyunManger)
|
IAliyunManger aliyunManger,
|
||||||
|
UserManager userManager)
|
||||||
{
|
{
|
||||||
_userRepository = userRepository;
|
_userRepository = userRepository;
|
||||||
_currentUser = currentUser;
|
_currentUser = currentUser;
|
||||||
@@ -56,6 +58,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
|||||||
_rbacOptions = options.Value;
|
_rbacOptions = options.Value;
|
||||||
_aliyunManger = aliyunManger;
|
_aliyunManger = aliyunManger;
|
||||||
_userCache = userCache;
|
_userCache = userCache;
|
||||||
|
_userManager = userManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -268,32 +271,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
|||||||
throw new UserFriendlyException("用户未登录");
|
throw new UserFriendlyException("用户未登录");
|
||||||
}
|
}
|
||||||
//此处优先从缓存中获取
|
//此处优先从缓存中获取
|
||||||
UserRoleMenuDto output = null;
|
var output = await _userManager.Get(userId.Value);
|
||||||
|
|
||||||
var cacheData = await _userCache.GetAsync(new UserInfoCacheKey(userId.Value));
|
|
||||||
if (cacheData is not null)
|
|
||||||
{
|
|
||||||
output = cacheData.Info;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var data = await _userRepository.GetUserAllInfoAsync(userId.Value);
|
|
||||||
//系统用户数据被重置,老前端访问重新授权
|
|
||||||
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.Value), new UserInfoCacheItem(data), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(tokenExpiresMinuteTime) });
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,7 +314,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
|||||||
if (userId is null)
|
if (userId is null)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
// throw new UserFriendlyException("用户已退出");
|
// throw new UserFriendlyException("用户已退出");
|
||||||
}
|
}
|
||||||
await _userCache.RemoveAsync(new UserInfoCacheKey(userId.Value));
|
await _userCache.RemoveAsync(new UserInfoCacheKey(userId.Value));
|
||||||
//Jwt去中心化登出,只需用记录日志即可
|
//Jwt去中心化登出,只需用记录日志即可
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Rbac.Domain.Shared.Etos
|
||||||
|
{
|
||||||
|
public class UserRoleMenuQueryEventArgs
|
||||||
|
{
|
||||||
|
public UserRoleMenuQueryEventArgs() { }
|
||||||
|
|
||||||
|
public UserRoleMenuQueryEventArgs(params Guid[] userIds)
|
||||||
|
{
|
||||||
|
UserIds.AddRange(userIds.ToList());
|
||||||
|
}
|
||||||
|
public List<Guid> UserIds { get; set; } = new List<Guid>();
|
||||||
|
|
||||||
|
public List<UserRoleMenuDto>? Result { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 Volo.Abp.Guids;
|
||||||
using Yi.Framework.Rbac.Domain.Entities;
|
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;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
namespace Yi.Framework.Rbac.Domain.Managers
|
namespace Yi.Framework.Rbac.Domain.Managers
|
||||||
@@ -10,11 +20,12 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
|||||||
public readonly ISqlSugarRepository<UserEntity> _repository;
|
public readonly ISqlSugarRepository<UserEntity> _repository;
|
||||||
public readonly ISqlSugarRepository<UserRoleEntity> _repositoryUserRole;
|
public readonly ISqlSugarRepository<UserRoleEntity> _repositoryUserRole;
|
||||||
public readonly ISqlSugarRepository<UserPostEntity> _repositoryUserPost;
|
public readonly ISqlSugarRepository<UserPostEntity> _repositoryUserPost;
|
||||||
|
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
|
||||||
private readonly IGuidGenerator _guidGenerator;
|
private readonly IGuidGenerator _guidGenerator;
|
||||||
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator) =>
|
private IUserRepository _userRepository;
|
||||||
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator) =
|
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository) =>
|
||||||
(repository, repositoryUserRole, repositoryUserPost, guidGenerator);
|
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository) =
|
||||||
|
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository);
|
||||||
|
|
||||||
/// <summary>
|
/// <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