feat: 上线聊天室功能模块
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Enums;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Contracts.Dtos
|
||||
{
|
||||
public class ChatMessageGetListInput
|
||||
{
|
||||
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? StartTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间
|
||||
/// </summary>
|
||||
public DateTime? EndTime { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,15 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Volo.Abp.EventBus.Local;
|
||||
using Yi.Framework.Bbs.Domain.Shared.Etos;
|
||||
using Yi.Framework.ChatHub.Application.Contracts.Dtos;
|
||||
using Yi.Framework.ChatHub.Domain.Managers;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Enums;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Application.Services
|
||||
@@ -15,7 +19,8 @@ namespace Yi.Framework.ChatHub.Application.Services
|
||||
public class ChatMessageService : ApplicationService
|
||||
{
|
||||
private UserMessageManager _userMessageManager;
|
||||
public ChatMessageService(UserMessageManager userMessageManager) { _userMessageManager = userMessageManager; }
|
||||
private ILocalEventBus _localEventBus;
|
||||
public ChatMessageService(UserMessageManager userMessageManager, ILocalEventBus localEventBus) { _userMessageManager = userMessageManager; _localEventBus = localEventBus; }
|
||||
/// <summary>
|
||||
/// 发送个人消息
|
||||
/// </summary>
|
||||
@@ -24,7 +29,10 @@ namespace Yi.Framework.ChatHub.Application.Services
|
||||
[Authorize]
|
||||
public async Task SendPersonalMessageAsync(PersonalMessageInputDto input)
|
||||
{
|
||||
await _userMessageManager.SendMessageAsync(MessageContext.CreatePersonal(input.Content, input.UserId,CurrentUser.Id!.Value)); ;
|
||||
var mesageContext = MessageContext.CreatePersonal(input.Content, input.UserId, CurrentUser.Id!.Value);
|
||||
await _userMessageManager.SendMessageAsync(mesageContext);
|
||||
|
||||
await _userMessageManager.CreateMessageStoreAsync(mesageContext);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,8 +44,58 @@ namespace Yi.Framework.ChatHub.Application.Services
|
||||
[Authorize]
|
||||
public async Task SendGroupMessageAsync(GroupMessageInputDto input)
|
||||
{
|
||||
await _userMessageManager.SendMessageAsync(MessageContext.CreateAll(input.Content, CurrentUser.Id!.Value)); ;
|
||||
//领域调用,群主消息调用bbs领域
|
||||
|
||||
//如果钱钱不足,将自动断言
|
||||
await _localEventBus.PublishAsync<MoneyChangeEventArgs>(new MoneyChangeEventArgs { UserId = CurrentUser.Id.Value, Number = -1 });
|
||||
|
||||
var mesageContext = MessageContext.CreateAll(input.Content, CurrentUser.Id!.Value);
|
||||
await _userMessageManager.SendMessageAsync(mesageContext);
|
||||
|
||||
await _userMessageManager.CreateMessageStoreAsync(mesageContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询消息
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize]
|
||||
[RemoteService(IsEnabled = false)]
|
||||
public async Task<List<MessageContext>> GetListAsync(ChatMessageGetListInput input)
|
||||
{
|
||||
var entities = await _userMessageManager._repository._DbQueryable
|
||||
.WhereIF(input.MessageType is not null, x => x.MessageType == input.MessageType)
|
||||
.WhereIF(input.ReceiveId is not null, x => x.ReceiveId == input.ReceiveId)
|
||||
.WhereIF(input.SendUserId is not null, x => x.SendUserId == input.SendUserId)
|
||||
.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
|
||||
.OrderBy(x => x.CreationTime)
|
||||
.ToListAsync();
|
||||
var output = entities.Adapt<List<MessageContext>>();
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自己的消息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Authorize]
|
||||
[HttpGet("chat-message/account")]
|
||||
public async Task<List<MessageContext>> GetAccountMessageListAsync()
|
||||
{
|
||||
//默认显示1个月的个人数据
|
||||
var userId = CurrentUser.Id!.Value;
|
||||
//3个类型数据
|
||||
var entities = await _userMessageManager._repository._DbQueryable
|
||||
.Where(x => x.MessageType == MessageTypeEnum.All
|
||||
|| x.ReceiveId == userId
|
||||
|| x.SendUserId == userId
|
||||
)
|
||||
.Where(x => x.CreationTime >= DateTime.Now.AddDays(-30))
|
||||
.OrderBy(x => x.CreationTime)
|
||||
.ToListAsync();
|
||||
var output = entities.Adapt<List<MessageContext>>();
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Shared.Enums
|
||||
{
|
||||
public enum MessageTypeEnum
|
||||
{
|
||||
|
||||
|
||||
Personal,
|
||||
Group,
|
||||
All
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.ChatHub.Domain.Shared.Enums;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
{
|
||||
@@ -11,12 +12,12 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
{
|
||||
public static MessageContext CreatePersonal(string content, Guid userId,Guid sendUserId)
|
||||
{
|
||||
return new MessageContext() { MessageType = MessageType.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)
|
||||
{
|
||||
return new MessageContext() { MessageType = MessageType.All, Content = content, SendUserId = sendUserId };
|
||||
return new MessageContext() { MessageType = MessageTypeEnum.All, Content = content, SendUserId = sendUserId };
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +25,7 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
/// 消息类型
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public MessageType MessageType { get; set; }
|
||||
public MessageTypeEnum MessageType { get; set; }
|
||||
/// <summary>
|
||||
/// 接收者(用户id、群组id)
|
||||
/// </summary>
|
||||
@@ -38,14 +39,8 @@ namespace Yi.Framework.ChatHub.Domain.Shared.Model
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
|
||||
public DateTime CreationTime { get;protected set; }=DateTime.Now;
|
||||
}
|
||||
|
||||
public enum MessageType
|
||||
{
|
||||
|
||||
Personal,
|
||||
Group,
|
||||
All
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="..\..\..\common.props" />
|
||||
<ItemGroup>
|
||||
<Folder Include="Enums\" />
|
||||
<Folder Include="Etos\" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -9,4 +8,8 @@
|
||||
<PackageReference Include="Volo.Abp.Ddd.Domain.Shared" Version="$(AbpVersion)" />
|
||||
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\bbs\Yi.Framework.Bbs.Domain.Shared\Yi.Framework.Bbs.Domain.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
using Volo.Abp.Domain;
|
||||
using Yi.Framework.Bbs.Domain.Shared;
|
||||
|
||||
namespace Yi.Framework.ChatHub.Domain.Shared
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(AbpDddDomainSharedModule))]
|
||||
typeof(AbpDddDomainSharedModule),
|
||||
|
||||
typeof(YiFrameworkBbsDomainSharedModule))]
|
||||
public class YiFrameworkChatHubDomainSharedModule : AbpModule
|
||||
{
|
||||
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user