添加项目文件。
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace SimpleAdmin.System;
|
||||
|
||||
/// <summary>
|
||||
/// 通知事件总线
|
||||
/// </summary>
|
||||
public class NoticeEventSubscriber : IEventSubscriber, ISingleton
|
||||
{
|
||||
private readonly ISimpleCacheService _simpleCacheService;
|
||||
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly bool _useMessageCenter;
|
||||
private readonly SqlSugarScope _db;
|
||||
|
||||
public NoticeEventSubscriber(ISimpleCacheService simpleCacheService, IServiceScopeFactory scopeFactory,
|
||||
IOptionsMonitor<SystemSettingsOptions> options)
|
||||
{
|
||||
_db = DbContext.DB;
|
||||
_simpleCacheService = simpleCacheService;
|
||||
_scopeFactory = scopeFactory;
|
||||
_useMessageCenter = options.CurrentValue.UseMessageCenter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通知用户下线事件
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
[EventSubscribe(EventSubscriberConst.USER_LOGIN_OUT)]
|
||||
public async Task UserLoginOut(EventHandlerExecutingContext context)
|
||||
{
|
||||
var loginEvent = (UserLoginOutEvent)context.Source.Payload;//获取参数
|
||||
//客户端ID列表
|
||||
var clientIds = new List<string>();
|
||||
//遍历token列表获取客户端ID列表
|
||||
loginEvent.TokenInfos.ForEach(it =>
|
||||
{
|
||||
clientIds.AddRange(it.ClientIds);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 有新的消息
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
[EventSubscribe(EventSubscriberConst.NEW_MESSAGE)]
|
||||
public async Task NewMessage(EventHandlerExecutingContext context)
|
||||
{
|
||||
var newMessageEvent = (NewMessageEvent)context.Source.Payload;//获取参数
|
||||
var messageId = newMessageEvent.Id;
|
||||
//如果启用了通知中心
|
||||
if (_useMessageCenter)
|
||||
{
|
||||
if (newMessageEvent.SendWay == SysDictConst.SEND_WAY_NOW)
|
||||
{
|
||||
//立即发送
|
||||
await SendMessage(messageId);
|
||||
}
|
||||
else
|
||||
{
|
||||
//延迟发送
|
||||
var sendTime = newMessageEvent.SendTime;
|
||||
//计算延迟时间
|
||||
var delay = (int)(sendTime - DateTime.Now).TotalSeconds;
|
||||
if (delay > 0)
|
||||
{
|
||||
await SendMessage(messageId, delay);
|
||||
}
|
||||
else
|
||||
{
|
||||
await SendMessage(messageId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="messageId">消息id</param>
|
||||
/// <param name="delay">延迟</param>
|
||||
private async Task SendMessage(long messageId, int delay = 0)
|
||||
{
|
||||
_simpleCacheService.AddDelayQueue(CacheConst.CACHE_NOTIFICATION, messageId, delay);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取服务
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
private T GetService<T>() where T : class, new()
|
||||
{
|
||||
// 创建新的作用域
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
return scope.ServiceProvider.GetRequiredService<T>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user