feat: 添加事件总线

This commit is contained in:
橙子
2023-02-19 20:57:31 +08:00
parent 1f33204697
commit dffdaa8d68
12 changed files with 126 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
using Hei.Captcha;
using Cike.EventBus.DistributedEvent;
using Hei.Captcha;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
@@ -11,6 +12,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Auth.JwtBearer.Authentication;
using Yi.Framework.Auth.JwtBearer.Authorization;
using Yi.Framework.Core.CurrentUsers;
using Yi.Framework.Core.Enums;
using Yi.Framework.Core.Exceptions;
@@ -25,6 +27,7 @@ using Yi.RBAC.Domain.Identity.Entities;
using Yi.RBAC.Domain.Identity.Repositories;
using Yi.RBAC.Domain.Shared.Identity.ConstClasses;
using Yi.RBAC.Domain.Shared.Identity.Dtos;
using Yi.RBAC.Domain.Shared.Identity.Etos;
namespace Yi.RBAC.Application.Identity
{
@@ -45,6 +48,9 @@ namespace Yi.RBAC.Application.Identity
[Autowired]
private SecurityCodeHelper _securityCode { get; set; }
[Autowired]
private IDistributedEventBus _distributedEventBus { get; set; }
/// <summary>
/// 登录
/// </summary>
@@ -63,6 +69,16 @@ namespace Yi.RBAC.Application.Identity
{
throw new UserFriendlyException(UserConst.);
}
//这里抛出一个登录的事件
//不阻碍执行,无需等待
#pragma warning disable CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
_distributedEventBus.PublishAsync(new LoginEventArgs
{
UserId = userInfo.User.Id,
UserName = user.UserName
});
#pragma warning restore CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
//创建token
var token = _jwtTokenManager.CreateToken(_accountManager.UserInfoToClaim(userInfo));

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.RBAC.Domain.Shared.Identity.Etos
{
public class LoginEventArgs
{
public long UserId { get; set; }
public string UserName { get; set; }
}
}

View File

@@ -0,0 +1,19 @@
using Cike.EventBus.EventHandlerAbstracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.RBAC.Domain.Shared.Identity.Etos;
namespace Yi.RBAC.Domain.Identity.Event
{
public class LoginEventHandler : IDistributedEventHandler<LoginEventArgs>
{
public Task HandlerAsync(LoginEventArgs eventData)
{
Console.WriteLine(eventData.UserName+"登录系统");
return Task.CompletedTask;
}
}
}

View File

@@ -15,6 +15,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Data\Yi.Framework.Data.csproj" />
<ProjectReference Include="..\..\..\module\Yi.Framework.EventBus\Yi.Framework.EventBus.csproj" />
<ProjectReference Include="..\..\..\module\Yi.Framework.ThumbnailSharp\Yi.Framework.ThumbnailSharp.csproj" />
<ProjectReference Include="..\Yi.RBAC.Domain.Shared\Yi.RBAC.Domain.Shared.csproj" />
</ItemGroup>

View File

@@ -9,6 +9,7 @@ using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Attributes;
using Yi.Framework.Data;
using Yi.Framework.EventBus;
using Yi.Framework.ThumbnailSharp;
using Yi.RBAC.Domain.Shared;
@@ -17,7 +18,8 @@ namespace Yi.RBAC.Domain
[DependsOn(
typeof(YiRBACDomainSharedModule),
typeof(YiFrameworkDataModule),
typeof(YiFrameworkThumbnailSharpModule)
typeof(YiFrameworkThumbnailSharpModule),
typeof(YiFrameworkEventBusModule)
)]
public class YiRBACDomainModule : IStartupModule
{