// 麻省理工学院许可证 // // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995 // // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。 // // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。 // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。 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.KeyUserOnline + 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.KeyUserOnline + 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.KeyUserOnline + userid); if (user != null) userlist.Add(user.ConnectionId); } await _chatHubContext.Clients.Clients(userlist).ReceiveMessage(input); } /// /// 发送邮件 /// /// /// /// /// [DisplayName("发送邮件")] public async Task SendEmail([Required] string message, string title = "系统邮件", bool isHtml = false) { await _fluentEmail.To(_emailOptions.DefaultToEmail).Subject(title).Body(message, isHtml).SendAsync(); } }