using FluentEmail.Core; using Microsoft.AspNetCore.SignalR; namespace Admin.NET.Core.Service; /// /// 系统消息发送服务 /// [ApiDescriptionSettings(Order = 370)] public class SysMessageService : IDynamicApiController, ITransient { private readonly SysCacheService _sysCacheService; private readonly EmailOptions _emailOptions; private readonly IFluentEmail _fluentEmail; private readonly IHubContext _chatHubContext; public SysMessageService(SysCacheService sysCacheService, IOptions emailOptions, IFluentEmail fluentEmail, IHubContext chatHubContext) { _sysCacheService = sysCacheService; _emailOptions = emailOptions.Value; _fluentEmail = fluentEmail; _chatHubContext = chatHubContext; } /// /// 发送消息给所有人 /// /// /// [DisplayName("发送消息给所有人")] public async Task SendAllUser(MessageInput input) { await _chatHubContext.Clients.All.ReceiveMessage(input); } /// /// 发送消息给除了发送人的其他人 /// /// /// [DisplayName("发送消息给除了发送人的其他人")] public async Task SendOtherUser(MessageInput input) { var user = _sysCacheService.Get(CacheConst.KeyOnlineUser + input.UserId); if (user != null) { await _chatHubContext.Clients.AllExcept(user.ConnectionId).ReceiveMessage(input); } } /// /// 发送消息给某个人 /// /// /// [DisplayName("发送消息给某个人")] public async Task SendUser(MessageInput input) { var user = _sysCacheService.Get(CacheConst.KeyOnlineUser + input.UserId); if (user == null) return; await _chatHubContext.Clients.Client(user.ConnectionId).ReceiveMessage(input); } /// /// 发送消息给某些人 /// /// /// [DisplayName("发送消息给某些人")] public async Task SendUsers(MessageInput input) { var userlist = new List(); foreach (var userid in input.UserIds) { var user = _sysCacheService.Get(CacheConst.KeyOnlineUser + userid); if (user != null) userlist.Add(user.ConnectionId); } await _chatHubContext.Clients.Clients(userlist).ReceiveMessage(input); } /// /// 发送邮件 /// /// /// [DisplayName("发送邮件")] public async Task SendEmail([Required] string message) { await _fluentEmail.To(_emailOptions.DefaultToEmail).Body(message).SendAsync(); } }