Browse Source

feat: 封装Lion.AbpPro.SignalR

feat/SignalR
HanPaoPao 6 months ago
parent
commit
febc3bb922
  1. 11
      aspnet-core/Lion.AbpPro.SignalR/GlobalUsings.cs
  2. 15
      aspnet-core/Lion.AbpPro.SignalR/Lion.AbpPro.SignalR.csproj
  3. 12
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/AbpProNotificationModule.cs
  4. 2
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/Enums/MessageLevel.cs
  5. 2
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/Enums/MessageType.cs
  6. 15
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/Hubs/INotificationHub.cs
  7. 7
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/Hubs/NotificationHub.cs
  8. 17
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/IMessageManager.cs
  9. 79
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/LocalEvent/Notification/CreatedNotificationLocalEvent.cs
  10. 62
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/MessageManager.cs
  11. 40
      aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/SendNotificationDto.cs
  12. 7
      aspnet-core/Lion.AbpPro.sln
  13. 1
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/GlobalUsings.cs
  14. 19
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Hubs/INotificationHub.cs
  15. 6
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Hubs/INotificationHubAppService.cs
  16. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Notifications/Dtos/PagingNotificationInput.cs
  17. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Notifications/Dtos/PagingNotificationOutput.cs
  18. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Notifications/Dtos/PagingNotificationSubscriptionOutput.cs
  19. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Notifications/Dtos/SendNotificationDto.cs
  20. 4
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/GlobalUsings.cs
  21. 65
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/Hubs/NotificationHubAppService.cs
  22. 30
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/Notifications/LocalEventHandlers/NotificationCreatedLocalEventHandler.cs
  23. 18
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/Notifications/NotificationAppService.cs
  24. 1
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/GlobalUsings.cs
  25. 1
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Lion.AbpPro.NotificationManagement.Domain.Shared.csproj
  26. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Notifications/Dtos/NotificationDto.cs
  27. 72
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Notifications/Etos/NotificationEto.cs
  28. 17
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Notifications/LocalEvents/CreatedNotificationLocalEvent.cs
  29. 4
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/GlobalUsings.cs
  30. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/NotificationDomainAutoMapperProfile.cs
  31. 3
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/Aggregates/Notification.cs
  32. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/Aggregates/NotificationSubscription.cs
  33. 16
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/INotificationManager.cs
  34. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/INotificationRepository.cs
  35. 24
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/NotificationManager.cs
  36. 2
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.EntityFrameworkCore/EntityFrameworkCore/Notifications/EfCoreNotificationRepository.cs
  37. 1
      aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.EntityFrameworkCore/GlobalUsings.cs

11
aspnet-core/Lion.AbpPro.SignalR/GlobalUsings.cs

@ -0,0 +1,11 @@
// Global using directives
global using Lion.AbpPro.SignalR.Enums;
global using Microsoft.AspNetCore.SignalR;
global using Microsoft.Extensions.Logging;
global using Volo.Abp.DependencyInjection;
global using Volo.Abp.EventBus.Local;
global using Volo.Abp.Guids;
global using Volo.Abp.Json;
global using Volo.Abp.MultiTenancy;
global using Volo.Abp.Users;

15
aspnet-core/Lion.AbpPro.SignalR/Lion.AbpPro.SignalR.csproj

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace />
<PackageId>Lion.AbpPro.SignalR</PackageId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Autofac" />
<PackageReference Include="Volo.Abp.Ddd.Domain" />
<PackageReference Include="Volo.Abp.AspNetCore.SignalR" />
</ItemGroup>
</Project>

12
aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/AbpProNotificationModule.cs

@ -0,0 +1,12 @@
using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
namespace Lion.AbpPro.SignalR;
[DependsOn(
typeof(AbpAutofacModule),
typeof(AbpAspNetCoreSignalRModule))]
public class AbpProNotificationModule : AbpModule
{
}

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Notifications/Enums/MessageLevel.cs → aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/Enums/MessageLevel.cs

@ -1,6 +1,6 @@
using System.ComponentModel;
namespace Lion.AbpPro.NotificationManagement.Notifications.Enums;
namespace Lion.AbpPro.SignalR.Enums;
/// <summary>
/// 消息等级

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Notifications/Enums/MessageType.cs → aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/Enums/MessageType.cs

@ -1,6 +1,6 @@
using System.ComponentModel;
namespace Lion.AbpPro.NotificationManagement.Notifications.Enums
namespace Lion.AbpPro.SignalR.Enums
{
/// <summary>
/// 消息类型

15
aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/Hubs/INotificationHub.cs

@ -0,0 +1,15 @@
namespace Lion.AbpPro.SignalR.Hubs
{
public interface INotificationHub
{
/// <summary>
/// 接受普通消息
/// </summary>
Task ReceiveTextMessageAsync(SendNotificationDto message);
/// <summary>
/// 接受广播消息
/// </summary>
Task ReceiveBroadCastMessageAsync(SendNotificationDto message);
}
}

7
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/Hubs/NotificationHub.cs → aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/Hubs/NotificationHub.cs

@ -1,10 +1,13 @@
namespace Lion.AbpPro.NotificationManagement.Hubs
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.Auditing;
namespace Lion.AbpPro.SignalR.Hubs
{
[HubRoute("SignalR/Notification")]
[Authorize]
[DisableAuditing]
public class NotificationHub : AbpHub<INotificationHub>
{
}
}

17
aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/IMessageManager.cs

@ -0,0 +1,17 @@
namespace Lion.AbpPro.SignalR;
public interface IMessageManager
{
/// <summary>
/// 发送消息
/// </summary>
/// <param name="title">消息标题</param>
/// <param name="content">消息内容</param>
/// <param name="messageType">消息类型</param>
/// <param name="messageLevel">消息级别</param>
/// <param name="receiverUserId">消息接受人,如果是广播消息,不需要传递</param>
/// <param name="receiverUserName">消息接受人userName,如果是广播消息,不需要传递</param>
/// <param name="isPersistent">是否持久化,如果ture会在消息管理中出现,并且右上角也会存在</param>
/// <returns></returns>
Task SendMessageAsync(string title, string content, MessageType messageType, MessageLevel messageLevel, Guid? receiverUserId = null, string receiverUserName = "", bool isPersistent = true);
}

79
aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/LocalEvent/Notification/CreatedNotificationLocalEvent.cs

@ -0,0 +1,79 @@
namespace Lion.AbpPro.SignalR.LocalEvent.Notification;
/// <summary>
/// 创建消息本地事件
/// </summary>
public class CreatedNotificationLocalEvent
{
public CreatedNotificationLocalEvent(Guid id, Guid? tenantId, string title, string content, MessageType messageType, MessageLevel messageLevel, Guid senderUserId, string senderUserName, Guid? receiveUserId, string receiveUserName, bool isPersistent)
{
Id = id;
TenantId = tenantId;
Title = title;
Content = content;
MessageType = messageType;
MessageLevel = messageLevel;
SenderUserId = senderUserId;
SenderUserName = senderUserName;
ReceiveUserId = receiveUserId;
ReceiveUserName = receiveUserName;
IsPersistent = isPersistent;
}
/// <summary>
/// 消息id
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// 租户id
/// </summary>
public Guid? TenantId { get; set; }
/// <summary>
/// 消息标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 消息内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public MessageType MessageType { get; set; }
/// <summary>
/// 消息等级
/// </summary>
public MessageLevel MessageLevel { get; set; }
/// <summary>
/// 发送人
/// </summary>
public Guid SenderUserId { get; set; }
/// <summary>
/// 发送人用户名
/// </summary>
public string SenderUserName { get; set; }
/// <summary>
/// 订阅人
/// 消息类型是广播消息时,订阅人为空
/// </summary>
public Guid? ReceiveUserId { get; set; }
/// <summary>
/// 接收人用户名
/// 消息类型是广播消息时,订接收人用户名为空
/// </summary>
public string ReceiveUserName { get; set; }
/// <summary>
/// 消息是否持久化
/// </summary>
public bool IsPersistent { get; set; }
}

62
aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/MessageManager.cs

@ -0,0 +1,62 @@
using Lion.AbpPro.SignalR.Hubs;
using Lion.AbpPro.SignalR.LocalEvent.Notification;
namespace Lion.AbpPro.SignalR;
public class MessageManager : IMessageManager, ITransientDependency
{
private readonly IHubContext<NotificationHub, INotificationHub> _hubContext;
private readonly ILogger<MessageManager> _logger;
private readonly ILocalEventBus _localEventBus;
private readonly ICurrentUser _currentUser;
private readonly ICurrentTenant _currentTenant;
private readonly IGuidGenerator _guidGenerator;
public MessageManager(IHubContext<NotificationHub, INotificationHub> hubContext, ILogger<MessageManager> logger, ILocalEventBus localEventBus, ICurrentUser currentUser, ICurrentTenant currentTenant, IGuidGenerator guidGenerator)
{
_hubContext = hubContext;
_logger = logger;
_localEventBus = localEventBus;
_currentUser = currentUser;
_currentTenant = currentTenant;
_guidGenerator = guidGenerator;
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="title">消息标题</param>
/// <param name="content">消息内容</param>
/// <param name="messageType">消息类型</param>
/// <param name="messageLevel">消息级别</param>
/// <param name="receiverUserId">消息接受人,如果是广播消息,不需要传递</param>
/// <param name="receiverUserName">消息接受人userName,如果是广播消息,不需要传递</param>
/// <param name="isPersistent">是否持久化,如果ture会在消息管理中出现,并且右上角也会存在</param>
/// <returns></returns>
public virtual async Task SendMessageAsync(string title, string content, MessageType messageType, MessageLevel messageLevel, Guid? receiverUserId = null, string receiverUserName = "", bool isPersistent = true)
{
var messageId = _guidGenerator.Create();
if (messageType == MessageType.Common)
{
if (string.IsNullOrWhiteSpace(receiverUserId.ToString()))
{
_logger.LogError($"发送消息失败:接收用户ID为空,消息Id:{messageId}");
return;
}
await _hubContext.Clients
.Users([receiverUserId.ToString()])
.ReceiveTextMessageAsync(new SendNotificationDto(messageId, title, content, messageType, messageLevel));
}
if (messageType == MessageType.BroadCast)
{
await _hubContext.Clients.All.ReceiveBroadCastMessageAsync(new SendNotificationDto(messageId, title, content, messageType, messageLevel));
}
if (isPersistent)
{
await _localEventBus.PublishAsync(new CreatedNotificationLocalEvent(messageId, _currentTenant.Id, title, content, messageType, messageLevel, _currentUser.GetId(), _currentUser.UserName, receiverUserId, receiverUserName, true));
}
}
}

40
aspnet-core/Lion.AbpPro.SignalR/Lion/AbpPro/SignalR/SendNotificationDto.cs

@ -0,0 +1,40 @@
namespace Lion.AbpPro.SignalR
{
public class SendNotificationDto
{
public Guid Id { get; set; }
/// <summary>
/// 消息标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 消息内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public MessageType MessageType { get; set; }
/// <summary>
/// 消息等级
/// </summary>
public MessageLevel MessageLevel { get; set; }
private SendNotificationDto()
{
}
public SendNotificationDto(Guid id, string title, string content, MessageType messageType, MessageLevel messageLevel)
{
Id = id;
Title = title;
Content = content;
MessageType = messageType;
MessageLevel = messageLevel;
}
}
}

7
aspnet-core/Lion.AbpPro.sln

@ -253,6 +253,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lion.AbpPro.AspNetCore", "f
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lion.AbpPro.Hangfire", "frameworks\src\Lion.AbpPro.Hangfire\Lion.AbpPro.Hangfire.csproj", "{6C2FDD3D-F711-46B0-A2F2-B94BC33F136B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lion.AbpPro.SignalR", "Lion.AbpPro.SignalR\Lion.AbpPro.SignalR.csproj", "{66B3D9E0-CB3F-464A-9813-F2DC1426A37A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -623,6 +625,10 @@ Global
{6C2FDD3D-F711-46B0-A2F2-B94BC33F136B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C2FDD3D-F711-46B0-A2F2-B94BC33F136B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C2FDD3D-F711-46B0-A2F2-B94BC33F136B}.Release|Any CPU.Build.0 = Release|Any CPU
{66B3D9E0-CB3F-464A-9813-F2DC1426A37A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66B3D9E0-CB3F-464A-9813-F2DC1426A37A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66B3D9E0-CB3F-464A-9813-F2DC1426A37A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66B3D9E0-CB3F-464A-9813-F2DC1426A37A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -744,6 +750,7 @@ Global
{9C88C5AE-21A1-4A62-9FA3-173806CD9EE3} = {7BE85EBC-99AD-4CDE-957E-4BDD087FC4E3}
{89CCAEA6-8176-4E4B-8D84-A2ACE2715F88} = {7BE85EBC-99AD-4CDE-957E-4BDD087FC4E3}
{6C2FDD3D-F711-46B0-A2F2-B94BC33F136B} = {7BE85EBC-99AD-4CDE-957E-4BDD087FC4E3}
{66B3D9E0-CB3F-464A-9813-F2DC1426A37A} = {7BE85EBC-99AD-4CDE-957E-4BDD087FC4E3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {28315BFD-90E7-4E14-A2EA-F3D23AF4126F}

1
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/GlobalUsings.cs

@ -8,7 +8,6 @@ global using System.Threading.Tasks;
global using Lion.AbpPro.Core;
global using Lion.AbpPro.NotificationManagement.Localization;
global using Lion.AbpPro.NotificationManagement.Notifications.Dtos;
global using Lion.AbpPro.NotificationManagement.Notifications.Enums;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Localization;
global using Volo.Abp.Application;

19
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Hubs/INotificationHub.cs

@ -1,19 +0,0 @@
namespace Lion.AbpPro.NotificationManagement.Hubs
{
public interface INotificationHub
{
/// <summary>
/// 发送普通消息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task ReceiveTextMessageAsync(SendNotificationDto input);
/// <summary>
/// 发送广播消息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task ReceiveBroadCastMessageAsync(SendNotificationDto input);
}
}

6
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Hubs/INotificationHubAppService.cs

@ -1,6 +0,0 @@
namespace Lion.AbpPro.NotificationManagement.Hubs;
public interface INotificationHubAppService : IApplicationService
{
Task SendMessageAsync(Guid id, string title, string content, MessageType messageType,MessageLevel messageLevel, string receiverUserId);
}

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Notifications/Dtos/PagingNotificationInput.cs

@ -1,3 +1,5 @@
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications.Dtos
{
public class PagingNotificationInput : PagingBase

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Notifications/Dtos/PagingNotificationOutput.cs

@ -1,3 +1,5 @@
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications.Dtos
{
public class PagingNotificationOutput

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Notifications/Dtos/PagingNotificationSubscriptionOutput.cs

@ -1,3 +1,5 @@
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications.Dtos
{
public class PagingNotificationSubscriptionOutput

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application.Contracts/Notifications/Dtos/SendNotificationDto.cs

@ -1,3 +1,5 @@
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications.Dtos
{
public class SendNotificationDto

4
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/GlobalUsings.cs

@ -5,13 +5,9 @@ global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
global using AutoMapper;
global using Lion.AbpPro.NotificationManagement.Hubs;
global using Lion.AbpPro.NotificationManagement.Localization;
global using Lion.AbpPro.NotificationManagement.Notifications;
global using Lion.AbpPro.NotificationManagement.Notifications.Aggregates;
global using Lion.AbpPro.NotificationManagement.Notifications.LocalEvents;
global using Lion.AbpPro.NotificationManagement.Notifications.Dtos;
global using Lion.AbpPro.NotificationManagement.Notifications.Enums;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.SignalR;
global using Microsoft.Extensions.DependencyInjection;

65
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/Hubs/NotificationHubAppService.cs

@ -1,65 +0,0 @@
using Volo.Abp.Json;
namespace Lion.AbpPro.NotificationManagement.Hubs;
public class NotificationHubAppService : NotificationManagementAppService, INotificationHubAppService
{
private readonly IHubContext<NotificationHub, INotificationHub> _hubContext;
private readonly ILogger<NotificationAppService> _logger;
private readonly IJsonSerializer _jsonSerializer;
public NotificationHubAppService(
IHubContext<NotificationHub, INotificationHub> hubContext,
ILogger<NotificationAppService> logger,
IJsonSerializer jsonSerializer)
{
_hubContext = hubContext;
_logger = logger;
_jsonSerializer = jsonSerializer;
}
/// <summary>
/// 发送消息
/// </summary>
public virtual async Task SendMessageAsync(Guid id, string title, string content, MessageType messageType, MessageLevel messageLevel, string receiverUserId)
{
switch (messageType)
{
case MessageType.Common:
await SendMessageToClientByUserIdAsync(new SendNotificationDto(id, title, content, messageType, messageLevel), receiverUserId);
break;
case MessageType.BroadCast:
await SendMessageToAllClientAsync(new SendNotificationDto(id, title, content, messageType, messageLevel));
break;
default:
throw new BusinessException(NotificationManagementErrorCodes.MessageTypeUnknown);
}
}
/// <summary>
/// 发送消息指定客户端用户
/// </summary>
private async Task SendMessageToClientByUserIdAsync(SendNotificationDto sendNotificationDto, string receiverUserId)
{
if (receiverUserId.IsNotNullOrWhiteSpace())
{
await _hubContext.Clients
.Users(new string[] { receiverUserId })
.ReceiveTextMessageAsync(sendNotificationDto);
_logger.LogInformation($"通知模块收到消息:{_jsonSerializer.Serialize(sendNotificationDto)},发送给:{receiverUserId}");
}
else
{
_logger.LogWarning($"消息未指定发送人:{_jsonSerializer.Serialize(sendNotificationDto)}");
}
}
/// <summary>
/// 发送消息到所有客户端
/// 广播消息
/// </summary>
private async Task SendMessageToAllClientAsync(SendNotificationDto sendNotificationDto)
{
await _hubContext.Clients.All.ReceiveBroadCastMessageAsync(sendNotificationDto);
_logger.LogInformation($"通知模块收到消息:{_jsonSerializer.Serialize(sendNotificationDto)}");
}
}

30
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/Notifications/LocalEventHandlers/NotificationCreatedLocalEventHandler.cs

@ -1,29 +1,31 @@
using Lion.AbpPro.SignalR.LocalEvent.Notification;
namespace Lion.AbpPro.NotificationManagement.Notifications.LocalEventHandlers
{
/// <summary>
/// 创建消息事件处理
/// </summary>
public class NotificationCreatedLocalEventHandler :
ILocalEventHandler<CreatedNotificationLocalEvent>,
ITransientDependency
ILocalEventHandler<CreatedNotificationLocalEvent>,
ITransientDependency
{
private readonly INotificationHubAppService _hubAppService;
private readonly INotificationManager _notificationManager;
public NotificationCreatedLocalEventHandler(INotificationHubAppService hubAppService)
public NotificationCreatedLocalEventHandler( INotificationManager notificationManager)
{
_hubAppService = hubAppService;
_notificationManager = notificationManager;
}
public virtual Task HandleEventAsync(CreatedNotificationLocalEvent eventData)
public virtual async Task HandleEventAsync(CreatedNotificationLocalEvent eventData)
{
return _hubAppService.SendMessageAsync(
eventData.NotificationEto.Id,
eventData.NotificationEto.Title,
eventData.NotificationEto.Content,
eventData.NotificationEto.MessageType,
eventData.NotificationEto.MessageLevel,
eventData.NotificationEto.ReceiveUserId.ToString());
await _notificationManager.CreateAsync(
eventData.Id,
eventData.Title,
eventData.Content,
eventData.MessageType,
eventData.MessageLevel,
eventData.ReceiveUserId,
eventData.ReceiveUserName);
}
}
}

18
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Application/Notifications/NotificationAppService.cs

@ -1,4 +1,6 @@
using System.Security.Authentication;
using Lion.AbpPro.SignalR;
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications
{
@ -7,12 +9,14 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
{
private readonly INotificationManager _notificationManager;
private readonly INotificationSubscriptionManager _notificationSubscriptionManager;
private readonly IMessageManager _messageManager;
public NotificationAppService(INotificationManager notificationManager, INotificationSubscriptionManager notificationSubscriptionManager)
public NotificationAppService(INotificationManager notificationManager, INotificationSubscriptionManager notificationSubscriptionManager, IMessageManager messageManager)
{
_notificationManager = notificationManager;
_notificationSubscriptionManager = notificationSubscriptionManager;
_messageManager = messageManager;
}
@ -21,7 +25,7 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
/// </summary>
public virtual async Task SendCommonWarningMessageAsync(SendCommonMessageInput input)
{
await _notificationManager.SendCommonWarningMessageAsync(input.Title, input.Content, MessageLevel.Warning, input.ReceiveUserId, input.ReceiveUserName);
await _messageManager.SendMessageAsync(input.Title, input.Content, MessageType.Common, MessageLevel.Warning, input.ReceiveUserId, input.ReceiveUserName);
}
/// <summary>
@ -29,7 +33,7 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
/// </summary>
public virtual async Task SendCommonInformationMessageAsync(SendCommonMessageInput input)
{
await _notificationManager.SendCommonWarningMessageAsync(input.Title, input.Content, MessageLevel.Information, input.ReceiveUserId, input.ReceiveUserName);
await _messageManager.SendMessageAsync(input.Title, input.Content, MessageType.Common, MessageLevel.Information, input.ReceiveUserId, input.ReceiveUserName);
}
/// <summary>
@ -37,7 +41,7 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
/// </summary>
public virtual async Task SendCommonErrorMessageAsync(SendCommonMessageInput input)
{
await _notificationManager.SendCommonWarningMessageAsync(input.Title, input.Content, MessageLevel.Error, input.ReceiveUserId, input.ReceiveUserName);
await _messageManager.SendMessageAsync(input.Title, input.Content, MessageType.Common, MessageLevel.Error, input.ReceiveUserId, input.ReceiveUserName);
}
/// <summary>
@ -45,7 +49,7 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
/// </summary>
public virtual async Task SendBroadCastWarningMessageAsync(SendBroadCastMessageInput input)
{
await _notificationManager.SendBroadCastWarningMessageAsync(input.Title, input.Content, MessageLevel.Warning);
await _messageManager.SendMessageAsync(input.Title, input.Content, MessageType.BroadCast, MessageLevel.Warning);
}
/// <summary>
@ -53,7 +57,7 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
/// </summary>
public virtual async Task SendBroadCastInformationMessageAsync(SendBroadCastMessageInput input)
{
await _notificationManager.SendBroadCastWarningMessageAsync(input.Title, input.Content, MessageLevel.Information);
await _messageManager.SendMessageAsync(input.Title, input.Content, MessageType.BroadCast, MessageLevel.Information);
}
/// <summary>
@ -61,7 +65,7 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
/// </summary>
public virtual async Task SendBroadCastErrorMessageAsync(SendBroadCastMessageInput input)
{
await _notificationManager.SendBroadCastWarningMessageAsync(input.Title, input.Content, MessageLevel.Error);
await _messageManager.SendMessageAsync(input.Title, input.Content, MessageType.BroadCast, MessageLevel.Error);
}
public virtual async Task SetReadAsync(SetReadInput input)

1
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/GlobalUsings.cs

@ -1,7 +1,6 @@
// Global using directives
global using Lion.AbpPro.NotificationManagement.Localization;
global using Lion.AbpPro.NotificationManagement.Notifications.Etos;
global using Volo.Abp.Localization;
global using Volo.Abp.Localization.ExceptionHandling;
global using Volo.Abp.Modularity;

1
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Lion.AbpPro.NotificationManagement.Domain.Shared.csproj

@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\..\frameworks\src\Lion.AbpPro.Core\Lion.AbpPro.Core.csproj" />
<ProjectReference Include="..\..\..\..\Lion.AbpPro.SignalR\Lion.AbpPro.SignalR.csproj" />
</ItemGroup>

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Notifications/Dtos/NotificationDto.cs

@ -1,4 +1,4 @@
using Lion.AbpPro.NotificationManagement.Notifications.Enums;
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications.Dtos
{

72
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Notifications/Etos/NotificationEto.cs

@ -1,72 +0,0 @@
using System;
using System.Collections.Generic;
using Lion.AbpPro.NotificationManagement.Notifications.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications.Etos
{
public class NotificationEto
{
public Guid Id { get; set; }
/// <summary>
/// 租户id
/// </summary>
public Guid? TenantId { get; set; }
/// <summary>
/// 消息标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 消息内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public MessageType MessageType { get; set; }
/// <summary>
/// 消息等级
/// </summary>
public MessageLevel MessageLevel { get; set; }
/// <summary>
/// 发送人
/// </summary>
public Guid SenderUserId { get; set; }
/// <summary>
/// 发送人用户名
/// </summary>
public string SenderUserName { get; set; }
/// <summary>
/// 订阅人
/// 消息类型是广播消息时,订阅人为空
/// </summary>
public Guid? ReceiveUserId { get; set; }
/// <summary>
/// 接收人用户名
/// 消息类型是广播消息时,订接收人用户名为空
/// </summary>
public string ReceiveUserName { get; set; }
/// <summary>
/// 是否已读
/// </summary>
public bool Read { get; set; }
/// <summary>
/// 已读时间
/// </summary>
public DateTime? ReadTime { get; set; }
}
}

17
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain.Shared/Notifications/LocalEvents/CreatedNotificationLocalEvent.cs

@ -1,17 +0,0 @@
namespace Lion.AbpPro.NotificationManagement.Notifications.LocalEvents
{
public class CreatedNotificationLocalEvent
{
public NotificationEto NotificationEto { get; set; }
private CreatedNotificationLocalEvent()
{
}
public CreatedNotificationLocalEvent(NotificationEto notificationEto)
{
NotificationEto = notificationEto;
}
}
}

4
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/GlobalUsings.cs

@ -11,9 +11,6 @@ global using AutoMapper;
global using Lion.AbpPro.Core;
global using Lion.AbpPro.NotificationManagement.Notifications;
global using Lion.AbpPro.NotificationManagement.Notifications.Aggregates;
global using Lion.AbpPro.NotificationManagement.Notifications.LocalEvents;
global using Lion.AbpPro.NotificationManagement.Notifications.Enums;
global using Lion.AbpPro.NotificationManagement.Notifications.Etos;
global using Lion.AbpPro.NotificationManagement.Notifications.MaxLengths;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
@ -26,6 +23,7 @@ global using Volo.Abp.Domain.Repositories;
global using Volo.Abp.Domain.Services;
global using Volo.Abp.EventBus.Distributed;
global using Volo.Abp.Modularity;
global using Volo.Abp.MultiTenancy;
global using Volo.Abp.ObjectMapping;
global using Volo.Abp.Settings;
global using Volo.Abp.Uow;

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/NotificationDomainAutoMapperProfile.cs

@ -6,8 +6,6 @@ namespace Lion.AbpPro.NotificationManagement
{
public NotificationDomainAutoMapperProfile()
{
CreateMap<Notification, NotificationEto>();
CreateMap<Notification, NotificationDto>();
CreateMap<NotificationSubscription, NotificationSubscriptionDto>();
}
}

3
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/Aggregates/Notification.cs

@ -1,4 +1,5 @@
using Volo.Abp.MultiTenancy;
using Lion.AbpPro.SignalR.Enums;
using Lion.AbpPro.SignalR.LocalEvent.Notification;
namespace Lion.AbpPro.NotificationManagement.Notifications.Aggregates
{

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/Aggregates/NotificationSubscription.cs

@ -1,5 +1,3 @@
using Volo.Abp.MultiTenancy;
namespace Lion.AbpPro.NotificationManagement.Notifications.Aggregates
{
/// <summary>

16
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/INotificationManager.cs

@ -1,5 +1,5 @@
using Lion.AbpPro.NotificationManagement.Notifications.Dtos;
using Volo.Abp.DependencyInjection;
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications;
@ -40,22 +40,16 @@ public interface INotificationManager
MessageLevel? messageLevel);
/// <summary>
/// 发送警告文本消息
/// 发送文本消息
/// </summary>
/// <param name="id">id</param>
/// <param name="title">标题</param>
/// <param name="content">消息内容</param>
/// <param name="messageType">消息类型</param>
/// <param name="level">消息等级</param>
/// <param name="receiveUserId">接受人,发送给谁。</param>
/// <param name="receiveUserName">接受人用户名</param>
Task SendCommonWarningMessageAsync(string title, string content, MessageLevel level, Guid receiveUserId,string receiveUserName);
/// <summary>
/// 发送警告广播消息
/// </summary>
/// <param name="title">标题</param>
/// <param name="content">消息内容</param>
/// <param name="level">消息等级</param>
Task SendBroadCastWarningMessageAsync(string title, string content, MessageLevel level);
Task CreateAsync(Guid id, string title, string content,MessageType messageType, MessageLevel level, Guid? receiveUserId,string receiveUserName);
/// <summary>
/// 消息设置为已读

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/INotificationRepository.cs

@ -1,3 +1,5 @@
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications
{
/// <summary>

24
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.Domain/Notifications/NotificationManager.cs

@ -1,5 +1,5 @@
using Lion.AbpPro.NotificationManagement.Notifications.Dtos;
using Lion.AbpPro.NotificationManagement.Notifications.LocalEvents;
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.Notifications
{
@ -33,7 +33,7 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
int maxResultCount = 10,
int skipCount = 0)
{
var list = await _notificationRepository.GetPagingListAsync(title, content, senderUserId, senderUserName, receiverUserId, receiverUserName, read, startReadTime, endReadTime, messageType,messageLevel, maxResultCount, skipCount);
var list = await _notificationRepository.GetPagingListAsync(title, content, senderUserId, senderUserName, receiverUserId, receiverUserName, read, startReadTime, endReadTime, messageType, messageLevel, maxResultCount, skipCount);
return ObjectMapper.Map<List<Notification>, List<NotificationDto>>(list);
}
@ -56,31 +56,15 @@ namespace Lion.AbpPro.NotificationManagement.Notifications
return await _notificationRepository.GetPagingCountAsync(title, content, senderUserId, senderUserName, receiverUserId, receiverUserName, read, startReadTime, endReadTime, messageType, messageLevel);
}
public async Task SendCommonWarningMessageAsync(string title, string content, MessageLevel level, Guid receiveUserId, string receiveUserName)
public async Task CreateAsync(Guid id, string title, string content, MessageType messageType, MessageLevel level, Guid? receiveUserId, string receiveUserName)
{
if (!_currentUser.Id.HasValue)
{
throw new AbpAuthorizationException();
}
var entity = new Notification(GuidGenerator.Create(), title, content, MessageType.Common, level, _currentUser.Id.Value, _currentUser.UserName, receiveUserId, receiveUserName, tenantId: CurrentTenant?.Id);
// 发送集成事件
var notificationEto = ObjectMapper.Map<Notification, NotificationEto>(entity);
entity.AddCreatedNotificationLocalEvent(new CreatedNotificationLocalEvent(notificationEto));
await _notificationRepository.InsertAsync(entity);
}
public async Task SendBroadCastWarningMessageAsync(string title, string content, MessageLevel level)
{
if (!_currentUser.Id.HasValue)
{
throw new AbpAuthorizationException();
}
var entity = new Notification(id, title, content, messageType, level, _currentUser.Id.Value, _currentUser.UserName, receiveUserId, receiveUserName, tenantId: CurrentTenant?.Id);
var entity = new Notification(GuidGenerator.Create(), title, content, MessageType.BroadCast, level, _currentUser.Id.Value, _currentUser.UserName, tenantId: CurrentTenant?.Id);
// 发送集成事件
var notificationEto = ObjectMapper.Map<Notification, NotificationEto>(entity);
entity.AddCreatedNotificationLocalEvent(new CreatedNotificationLocalEvent(notificationEto));
await _notificationRepository.InsertAsync(entity);
}

2
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.EntityFrameworkCore/EntityFrameworkCore/Notifications/EfCoreNotificationRepository.cs

@ -1,3 +1,5 @@
using Lion.AbpPro.SignalR.Enums;
namespace Lion.AbpPro.NotificationManagement.EntityFrameworkCore.Notifications
{
/// <summary>

1
aspnet-core/modules/NotificationManagement/src/Lion.AbpPro.NotificationManagement.EntityFrameworkCore/GlobalUsings.cs

@ -8,7 +8,6 @@ global using System.Threading.Tasks;
global using JetBrains.Annotations;
global using Lion.AbpPro.NotificationManagement.Notifications;
global using Lion.AbpPro.NotificationManagement.Notifications.Aggregates;
global using Lion.AbpPro.NotificationManagement.Notifications.Enums;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.Extensions.DependencyInjection;
global using Volo.Abp;

Loading…
Cancel
Save