diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Event/LoginEventHandler.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Event/LoginEventHandler.cs index 4eba4810..bab88a96 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Event/LoginEventHandler.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Event/LoginEventHandler.cs @@ -16,60 +16,21 @@ namespace Yi.Furion.Application.Rbac.Event { _loginLogRepository = loginLogRepository; } - //[EventSubscribe(nameof(LoginEventSource))] + [EventSubscribe(nameof(LoginEventSource))] public Task HandlerAsync(EventHandlerExecutingContext context) { var eventData = (LoginEventArgs)context.Source.Payload; - var loginLogEntity = GetLoginLogInfo(eventData.httpContext); + var loginLogEntity = eventData.LoginLogEntity; loginLogEntity.Id = SnowflakeHelper.NextId; loginLogEntity.LogMsg = eventData.UserName + "登录系统"; loginLogEntity.LoginUser = eventData.UserName; - loginLogEntity.LoginIp = eventData.httpContext.GetClientIp(); _loginLogRepository.InsertAsync(loginLogEntity); return Task.CompletedTask; } - /// - /// 获取客户端信息 - /// - /// - /// - private static ClientInfo GetClientInfo(HttpContext context) - { - var str = context.GetUserAgent(); - var uaParser = Parser.GetDefault(); - ClientInfo c = uaParser.Parse(str); - return c; - } - /// - /// 记录用户登陆信息 - /// - /// - /// - private static LoginLogEntity GetLoginLogInfo(HttpContext context) - { - var ipAddr = context.GetClientIp(); - IpInfo location; - if (ipAddr == "127.0.0.1") - { - location = new IpInfo() { Province = "本地", City = "本机" }; - } - else - { - location = IpTool.Search(ipAddr); - } - ClientInfo clientInfo = GetClientInfo(context); - LoginLogEntity entity = new() - { - Browser = clientInfo.Device.Family, - Os = clientInfo.OS.ToString(), - LoginIp = ipAddr, - LoginLocation = location.Province + "-" + location.City - }; - return entity; - } + } } diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/AccountService.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/AccountService.cs index 689f6cb0..0620b727 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/AccountService.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/AccountService.cs @@ -1,8 +1,12 @@ using System.Text.RegularExpressions; using Furion.EventBus; +using IPTools.Core; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; using SqlSugar; +using UAParser; +using Yi.Framework.Infrastructure.AspNetCore; using Yi.Framework.Infrastructure.CurrentUsers; using Yi.Framework.Infrastructure.Ddd.Repositories; using Yi.Framework.Infrastructure.Ddd.Services; @@ -22,7 +26,7 @@ namespace Yi.Furion.Application.Rbac.Services.Impl public class AccountService : ApplicationService, ITransient, IDynamicApiController { - public AccountService(IUserRepository userRepository, ICurrentUser currentUser, AccountManager accountManager, IRepository menuRepository, SmsAliyunManager smsAliyunManager, IOptions smsAliyunManagerOptions, SecurityCodeHelper securityCode, IMemoryCache memoryCache, IEventPublisher eventPublisher,IHttpContextAccessor httpContextAccessor) => + public AccountService(IUserRepository userRepository, ICurrentUser currentUser, AccountManager accountManager, IRepository menuRepository, SmsAliyunManager smsAliyunManager, IOptions smsAliyunManagerOptions, SecurityCodeHelper securityCode, IMemoryCache memoryCache, IEventPublisher eventPublisher, IHttpContextAccessor httpContextAccessor) => (_userRepository, _currentUser, _accountManager, _menuRepository, _smsAliyunManager, _smsAliyunManagerOptions, _securityCode, _memoryCache, _eventPublisher, _httpContextAccessor) = (userRepository, currentUser, accountManager, menuRepository, smsAliyunManager, smsAliyunManagerOptions, securityCode, memoryCache, eventPublisher, httpContextAccessor); @@ -123,19 +127,14 @@ namespace Yi.Furion.Application.Rbac.Services.Impl throw new UserFriendlyException(UserConst.用户无角色分配); } //这里抛出一个登录的事件 - - //不阻碍执行,无需等待 - - - await _eventPublisher.PublishAsync(new LoginEventSource(new LoginEventArgs + var loginLogEntity = _httpContextAccessor.HttpContext.GetLoginLogInfo(); + await _eventPublisher.PublishAsync(new LoginEventSource(new LoginEventArgs { - UserId = userInfo.User.Id, UserName = user.UserName, - httpContext= _httpContextAccessor.HttpContext - - }) - );; + LoginLogEntity = loginLogEntity + }) + ); //创建token diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Yi.Furion.Application.xml b/Yi.Furion.Net6/Yi.Furion.Application/Yi.Furion.Application.xml index d5368ce0..9b15fe03 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Yi.Furion.Application.xml +++ b/Yi.Furion.Net6/Yi.Furion.Application/Yi.Furion.Application.xml @@ -264,20 +264,6 @@ - - - 获取客户端信息 - - - - - - - 记录用户登陆信息 - - - - Config服务抽象 diff --git a/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Entities/LoginLogEntity.cs b/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Entities/LoginLogEntity.cs index df7ebecb..10e12be6 100644 --- a/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Entities/LoginLogEntity.cs +++ b/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Entities/LoginLogEntity.cs @@ -1,5 +1,8 @@ using System; +using IPTools.Core; using SqlSugar; +using UAParser; +using Yi.Framework.Infrastructure.AspNetCore; using Yi.Framework.Infrastructure.Data.Auditing; using Yi.Framework.Infrastructure.Ddd.Entities; @@ -45,4 +48,45 @@ namespace Yi.Furion.Core.Rbac.Entities public long? CreatorId { get; set; } } + + public static class LoginLogEntityExtensions { + + + + /// + /// 记录用户登陆信息 + /// + /// + /// + public static LoginLogEntity GetLoginLogInfo(this HttpContext context) + { + ClientInfo GetClientInfo(HttpContext context) + { + var str = context.GetUserAgent(); + var uaParser = Parser.GetDefault(); + ClientInfo c = uaParser.Parse(str); + return c; + } + var ipAddr = context.GetClientIp(); + IpInfo location; + if (ipAddr == "127.0.0.1") + { + location = new IpInfo() { Province = "本地", City = "本机" }; + } + else + { + location = IpTool.Search(ipAddr); + } + ClientInfo clientInfo = GetClientInfo(context); + LoginLogEntity entity = new() + { + Browser = clientInfo.Device.Family, + Os = clientInfo.OS.ToString(), + LoginIp = ipAddr, + LoginLocation = location.Province + "-" + location.City + }; + + return entity; + } + } } diff --git a/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Etos/LoginEventSource.cs b/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Etos/LoginEventSource.cs index 817d676b..d0e262f6 100644 --- a/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Etos/LoginEventSource.cs +++ b/Yi.Furion.Net6/Yi.Furion.Core/Rbac/Etos/LoginEventSource.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Furion.EventBus; +using Yi.Furion.Core.Rbac.Entities; namespace Yi.Furion.Core.Rbac.Etos { @@ -28,6 +29,6 @@ namespace Yi.Furion.Core.Rbac.Etos public long UserId { get; set; } public string UserName { get; set; } - public HttpContext httpContext { get; set; } + public LoginLogEntity LoginLogEntity { get; set; } } } diff --git a/Yi.Furion.Net6/Yi.Furion.Core/Yi.Furion.Core.xml b/Yi.Furion.Net6/Yi.Furion.Core/Yi.Furion.Core.xml index 6916a8c4..fe0b6888 100644 --- a/Yi.Furion.Net6/Yi.Furion.Core/Yi.Furion.Core.xml +++ b/Yi.Furion.Net6/Yi.Furion.Core/Yi.Furion.Core.xml @@ -506,6 +506,13 @@ 登录信息 + + + 记录用户登陆信息 + + + + 菜单表