feat: 上线聊天室功能模块

This commit is contained in:
橙子
2024-04-07 16:35:21 +08:00
parent 6aedff75f1
commit 1087b0965d
14 changed files with 340 additions and 72 deletions

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
using Yi.Framework.ChatHub.Domain.Shared.Enums;
using Yi.Framework.ChatHub.Domain.Shared.Model;
namespace Yi.Framework.ChatHub.Domain.Entities
{
[SugarTable("YiMessage")]
[SugarIndex($"index_{nameof(MessageEntity)}",
nameof(ReceiveId), OrderByType.Asc,
nameof(SendUserId), OrderByType.Asc,
nameof(CreationTime), OrderByType.Asc)]
public class MessageEntity : Entity<Guid>, IHasCreationTime
{
public static MessageContext CreatePersonal(string content, Guid userId, Guid sendUserId)
{
return new MessageContext() { MessageType = MessageTypeEnum.Personal, Content = content, ReceiveId = userId, SendUserId = sendUserId };
}
public static MessageContext CreateAll(string content, Guid sendUserId)
{
return new MessageContext() { MessageType = MessageTypeEnum.All, Content = content, SendUserId = sendUserId };
}
public string Content { get; set; }
public MessageTypeEnum MessageType { get; set; }
/// <summary>
/// 接收者(用户id、群组id)
/// </summary>
public Guid? ReceiveId { get; set; }
/// <summary>
/// 发送者的用户id
/// </summary>
public Guid SendUserId { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreationTime { get; protected set; }
}
}

View File

@@ -1,22 +1,29 @@
using FreeRedis;
using System.Security.AccessControl;
using FreeRedis;
using Mapster;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Options;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using Yi.Framework.ChatHub.Domain.Entities;
using Yi.Framework.ChatHub.Domain.Shared.Caches;
using Yi.Framework.ChatHub.Domain.Shared.Consts;
using Yi.Framework.ChatHub.Domain.Shared.Enums;
using Yi.Framework.ChatHub.Domain.Shared.Model;
using Yi.Framework.ChatHub.Domain.SignalRHubs;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.ChatHub.Domain.Managers
{
public class UserMessageManager : DomainService
{
private IHubContext<ChatCenterHub> _hubContext;
public UserMessageManager(IHubContext<ChatCenterHub> hubContext)
public ISqlSugarRepository<MessageEntity> _repository;
public UserMessageManager(IHubContext<ChatCenterHub> hubContext, ISqlSugarRepository<MessageEntity> repository)
{
_hubContext = hubContext;
_repository = repository;
}
/// <summary>
/// 使用懒加载防止报错
@@ -28,17 +35,17 @@ namespace Yi.Framework.ChatHub.Domain.Managers
{
switch (context.MessageType)
{
case MessageType.Personal:
var userModel =await GetUserAsync(context.ReceiveId.Value);
case MessageTypeEnum.Personal:
var userModel = await GetUserAsync(context.ReceiveId.Value);
if (userModel is not null)
{
await _hubContext.Clients.Client(userModel.ClientId).SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
}
break;
case MessageType.Group:
case MessageTypeEnum.Group:
throw new NotImplementedException();
break;
case MessageType.All:
case MessageTypeEnum.All:
await _hubContext.Clients.All.SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
break;
default:
@@ -47,6 +54,12 @@ namespace Yi.Framework.ChatHub.Domain.Managers
}
public async Task CreateMessageStoreAsync(MessageContext context)
{
await _repository.InsertAsync(context.Adapt<MessageEntity>());
}
public async Task<List<ChatOnlineUserCacheItem>> GetAllUserAsync()
{
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
@@ -57,7 +70,7 @@ namespace Yi.Framework.ChatHub.Domain.Managers
public async Task<ChatOnlineUserCacheItem?> GetUserAsync(Guid userId)
{
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
var cacheUser = System.Text.Json.JsonSerializer.Deserialize < ChatOnlineUserCacheItem >( await RedisClient.HGetAsync(key.GetKey(), key.GetField(userId)));
var cacheUser = System.Text.Json.JsonSerializer.Deserialize<ChatOnlineUserCacheItem>(await RedisClient.HGetAsync(key.GetKey(), key.GetField(userId)));
return cacheUser;
}
}

View File

@@ -2,6 +2,7 @@
<Import Project="..\..\..\common.props" />
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
<ProjectReference Include="..\Yi.Framework.ChatHub.Domain.Shared\Yi.Framework.ChatHub.Domain.Shared.csproj" />
</ItemGroup>
@@ -16,7 +17,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Entities\" />
<Folder Include="EventHandlers\" />
<Folder Include="Repositories\" />
</ItemGroup>