mirror of
https://github.com/nsnail/NetAdmin.git
synced 2025-12-29 00:55:48 +08:00
wip: 🧠 初步的框架
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using NetAdmin.Domain.Events;
|
||||
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Subscribers;
|
||||
|
||||
/// <summary>
|
||||
/// Api接口同步器
|
||||
/// </summary>
|
||||
public sealed class ApiSynchronizer : IEventSubscriber
|
||||
{
|
||||
private readonly ILogger<ApiSynchronizer> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ApiSynchronizer" /> class.
|
||||
/// </summary>
|
||||
public ApiSynchronizer(ILogger<ApiSynchronizer> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步Api接口
|
||||
/// </summary>
|
||||
[EventSubscribe(nameof(SyncStructureAfterEvent))]
|
||||
public async Task SyncApiAsync(EventHandlerExecutingContext _)
|
||||
{
|
||||
var logService = App.GetService<IApiService>();
|
||||
await logService.SyncAsync();
|
||||
_logger.Info($"{nameof(IApiService)}.{nameof(IApiService.SyncAsync)} {Ln.已完成}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using NetAdmin.Domain.Contexts;
|
||||
using NetAdmin.Domain.Events.Sys;
|
||||
using NetAdmin.SysComponent.Cache.Sys.Dependency;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Subscribers;
|
||||
|
||||
/// <summary>
|
||||
/// 缓存清理器
|
||||
/// </summary>
|
||||
public sealed class CacheCleaner : IEventSubscriber
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CacheCleaner" /> class.
|
||||
/// </summary>
|
||||
public CacheCleaner() { }
|
||||
|
||||
/// <summary>
|
||||
/// 用户缓存清理
|
||||
/// </summary>
|
||||
[EventSubscribe(nameof(UserUpdatedEvent))]
|
||||
public async Task RemoveUserInfoAsync(EventHandlerExecutingContext context)
|
||||
{
|
||||
if (context.Source is not UserUpdatedEvent userUpdatedEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
var cache = App.GetService<IUserCache>();
|
||||
cache.Service.UserToken = ContextUserToken.Create(userUpdatedEvent.Data);
|
||||
await cache.RemoveUserInfoAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using NetAdmin.Domain.Dto.Sys.VerifyCode;
|
||||
using NetAdmin.Domain.Enums.Sys;
|
||||
using NetAdmin.Domain.Events.Sys;
|
||||
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Subscribers;
|
||||
|
||||
/// <summary>
|
||||
/// 邮件验证码发送器
|
||||
/// </summary>
|
||||
public sealed class EmailCodeSender : IEventSubscriber
|
||||
{
|
||||
private readonly ILogger<EmailCodeSender> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EmailCodeSender" /> class.
|
||||
/// </summary>
|
||||
public EmailCodeSender(ILogger<EmailCodeSender> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送邮件
|
||||
/// </summary>
|
||||
[EventSubscribe(nameof(VerifyCodeCreatedEvent))]
|
||||
public async Task SendEmailAsync(EventHandlerExecutingContext context)
|
||||
{
|
||||
if (context.Source is not VerifyCodeCreatedEvent verifyCodeCreatedEvent ||
|
||||
verifyCodeCreatedEvent.Data.DeviceType != VerifyCodeDeviceTypes.Email) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送...
|
||||
var verifyCodeService = App.GetService<IVerifyCodeService>();
|
||||
_ = await verifyCodeService.UpdateAsync(
|
||||
verifyCodeCreatedEvent.Data.Adapt<UpdateVerifyCodeReq>() with { Status = VerifyCodeStatues.Sent });
|
||||
_logger.Info($"{nameof(IVerifyCodeService)}.{nameof(IVerifyCodeService.UpdateAsync)} {Ln.已完成}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using NetAdmin.Domain.Dto.Sys.RequestLog;
|
||||
using NetAdmin.Domain.Dto.Sys.User;
|
||||
using NetAdmin.Domain.Events.Sys;
|
||||
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Subscribers;
|
||||
|
||||
/// <summary>
|
||||
/// 操作日志记录
|
||||
/// </summary>
|
||||
public sealed class OperationLogger : IEventSubscriber
|
||||
{
|
||||
/// <summary>
|
||||
/// 保存请求日志到数据库
|
||||
/// </summary>
|
||||
[EventSubscribe(nameof(RequestLogEvent))]
|
||||
public async Task OperationEventDbRecordAsync(EventHandlerExecutingContext context)
|
||||
{
|
||||
if (context.Source is not RequestLogEvent operationEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 跳过心跳请求
|
||||
if (operationEvent.Data.ApiId.Equals("api/health/check", StringComparison.OrdinalIgnoreCase)) {
|
||||
return;
|
||||
}
|
||||
|
||||
CreateRequestLogReq logReq = null;
|
||||
|
||||
// 登录日志特殊处理
|
||||
if (operationEvent.Data.ApiId.Equals("api/user/login", StringComparison.OrdinalIgnoreCase)) {
|
||||
try {
|
||||
var loginReq = operationEvent.Data.RequestBody.ToObject<LoginByPwdReq>();
|
||||
logReq = operationEvent.Data with { ExtraData = loginReq.Account };
|
||||
}
|
||||
catch {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
logReq ??= operationEvent.Data;
|
||||
var logService = App.GetService<IRequestLogService>();
|
||||
logReq.TruncateStrings();
|
||||
_ = await logService.CreateAsync(logReq);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using NetAdmin.Domain.Dto.Sys.VerifyCode;
|
||||
using NetAdmin.Domain.Enums.Sys;
|
||||
using NetAdmin.Domain.Events.Sys;
|
||||
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Subscribers;
|
||||
|
||||
/// <summary>
|
||||
/// 短信验证码发送器
|
||||
/// </summary>
|
||||
public sealed class SmsCodeSender : IEventSubscriber
|
||||
{
|
||||
private readonly ILogger<SmsCodeSender> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SmsCodeSender" /> class.
|
||||
/// </summary>
|
||||
public SmsCodeSender(ILogger<SmsCodeSender> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送短信
|
||||
/// </summary>
|
||||
[EventSubscribe(nameof(VerifyCodeCreatedEvent))]
|
||||
public async Task SendSmsAsync(EventHandlerExecutingContext context)
|
||||
{
|
||||
if (context.Source is not VerifyCodeCreatedEvent verifyCodeCreatedEvent ||
|
||||
verifyCodeCreatedEvent.Data.DeviceType != VerifyCodeDeviceTypes.Mobile) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送...
|
||||
var verifyCodeService = App.GetService<IVerifyCodeService>();
|
||||
_ = await verifyCodeService.UpdateAsync(
|
||||
verifyCodeCreatedEvent.Data.Adapt<UpdateVerifyCodeReq>() with { Status = VerifyCodeStatues.Sent });
|
||||
_logger.Info($"{nameof(IVerifyCodeService)}.{nameof(IVerifyCodeService.UpdateAsync)} {Ln.已完成}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user