添加登录日志功能

This commit is contained in:
橙子
2022-10-02 14:02:21 +08:00
parent 2b02194a18
commit e963a4051f
12 changed files with 165 additions and 37 deletions

View File

@@ -297,6 +297,14 @@
</summary>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.LoginLogController.PageList(Yi.Framework.Model.Models.LoginLogEntity,Yi.Framework.Common.Models.PageParModel)">
<summary>
动态条件分页查询
</summary>
<param name="loginLog"></param>
<param name="page"></param>
<returns></returns>
</member>
<member name="T:Yi.Framework.ApiMicroservice.Controllers.MenuController">
<summary>
菜单管理

View File

@@ -33,12 +33,14 @@ namespace Yi.Framework.ApiMicroservice.Controllers
private JwtInvoker _jwtInvoker;
private ILogger _logger;
private SecurityCodeHelper _securityCode;
private IRepository<UserEntity> _repository;
public AccountController(ILogger<UserEntity> logger, IUserService iUserService, JwtInvoker jwtInvoker, SecurityCodeHelper securityCode)
{
_iUserService = iUserService;
_jwtInvoker = jwtInvoker;
_logger = logger;
_securityCode = securityCode;
_repository = iUserService._repository;
}
/// <summary>
@@ -67,13 +69,22 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{
//跳过需要redis缓存获取uuid与code的关系进行比较即可
//先效验验证码和UUID
//登录还需要进行登录日志的落库
var loginInfo = HttpContext.GetLoginLogInfo();
loginInfo.LoginUser = loginDto.UserName;
loginInfo.LogMsg = "登录成功!";
var loginLogRepository = _repository.ChangeRepository<Repository<LoginLogEntity>>();
UserEntity user = new();
if (await _iUserService.Login(loginDto.UserName, loginDto.Password, o => user = o))
{
var userRoleMenu = await _iUserService.GetUserAllInfo(user.Id);
return Result.Success("登录成功!").SetData(new { token = _jwtInvoker.GetAccessToken(userRoleMenu.User, userRoleMenu.Menus) });
await loginLogRepository.InsertReturnSnowflakeIdAsync(loginInfo);
return Result.Success(loginInfo.LogMsg).SetData(new { token = _jwtInvoker.GetAccessToken(userRoleMenu.User, userRoleMenu.Menus) });
}
return Result.Error("登录失败!用户名或者密码错误!");
loginInfo.LogMsg = "登录失败!用户名或者密码错误!";
await loginLogRepository.InsertReturnSnowflakeIdAsync(loginInfo);
return Result.Error(loginInfo.LogMsg);
}

View File

@@ -17,12 +17,25 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class LoginLogController : BaseCrudController<LoginLogEntity>
public class LoginLogController : BaseSimpleCrudController<LoginLogEntity>
{
private ILoginLogService _iLoginLogService;
public LoginLogController(ILogger<LoginLogEntity> logger, ILoginLogService iLoginLogService) : base(logger, iLoginLogService)
{
_iLoginLogService = iLoginLogService;
}
/// <summary>
/// 动态条件分页查询
/// </summary>
/// <param name="loginLog"></param>
/// <param name="page"></param>
/// <returns></returns>
[HttpGet]
public async Task<Result> PageList([FromQuery] LoginLogEntity loginLog, [FromQuery] PageParModel page)
{
return Result.Success().SetData(await _iLoginLogService.SelctPageList(loginLog, page));
}
}
}

View File

@@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="6.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="UAParser" Version="3.1.47" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
namespace Yi.Framework.Interface
{
public partial interface ILoginLogService:IBaseService<LoginLogEntity>
{
Task<PageModel<List<LoginLogEntity>>> SelctPageList(LoginLogEntity loginLog, PageParModel page);
}
}

View File

@@ -1,9 +1,13 @@
using Yi.Framework.Model.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
namespace Yi.Framework.Interface
{
public partial interface ILoginLogService:IBaseService<LoginLogEntity>
{
public partial interface ILoginLogService : IBaseService<LoginLogEntity>
{
}
}

View File

@@ -0,0 +1,27 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
namespace Yi.Framework.Service
{
public partial class LoginLogService : BaseService<LoginLogEntity>, ILoginLogService
{
public async Task<PageModel<List<LoginLogEntity>>> SelctPageList(LoginLogEntity loginLog, PageParModel page)
{
RefAsync<int> total = 0;
var data = await _repository._DbQueryable
.WhereIF(!string.IsNullOrEmpty(loginLog.LoginIp), u => u.LoginIp.Contains(loginLog.LoginIp))
.WhereIF(!string.IsNullOrEmpty(loginLog.LoginUser), u => u.LoginUser.Contains(loginLog.LoginUser))
.WhereIF(loginLog.IsDeleted.IsNotNull(), u => u.IsDeleted == loginLog.IsDeleted)
.WhereIF(page.StartTime.IsNotNull() && page.EndTime.IsNotNull(), u => u.CreateTime >= page.StartTime && u.CreateTime <= page.EndTime)
.OrderBy(u => u.OrderNum, OrderByType.Desc)
.ToPageListAsync(page.PageNum, page.PageSize, total);
return new PageModel<List<LoginLogEntity>>(data, total);
}
}
}

View File

@@ -19,7 +19,7 @@ namespace Yi.Framework.Service
.WhereIF(!string.IsNullOrEmpty(operationLog.OperUser), u => u.OperUser.Contains(operationLog.OperUser))
.WhereIF(operationLog.OperType is not null, u => u.OperType==operationLog.OperType.GetHashCode())
.WhereIF(operationLog.IsDeleted.IsNotNull(), u => u.IsDeleted == operationLog.IsDeleted)
.WhereIF(page.StartTime.IsNotNull() && page.EndTime.IsNotNull(), u => u.CreateTime >= page.StartTime && u.CreateTime <= page.EndTime)
.OrderBy(u => u.OrderNum, OrderByType.Desc)
.ToPageListAsync(page.PageNum, page.PageSize, total);

View File

@@ -11,6 +11,7 @@ using Yi.Framework.Model.Models;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Text.RegularExpressions;
using UAParser;
namespace Yi.Framework.WebCore
{
@@ -165,6 +166,19 @@ namespace Yi.Framework.WebCore
return param;
}
/// <summary>
/// 获取客户端信息
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static ClientInfo GetClientInfo(this HttpContext context)
{
var str = GetUserAgent(context);
var uaParser = Parser.GetDefault();
ClientInfo c = uaParser.Parse(str);
return c;
}
/// <summary>
/// 获取客户端IP
@@ -191,5 +205,41 @@ namespace Yi.Framework.WebCore
return result;
}
/// <summary>
/// 获取浏览器标识
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetUserAgent(this HttpContext context)
{
return context.Request.Headers["User-Agent"];
}
/// <summary>
/// 记录用户登陆信息
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static LoginLogEntity GetLoginLogInfo(this HttpContext context)
{
var ipAddr = context.GetClientIp();
//var ip_info = IpTool.Search(ipAddr);
//var location = "广州" + "-" + "深圳";
ClientInfo clientInfo = context.GetClientInfo();
LoginLogEntity entity = new()
{
Browser = clientInfo.Device.Family,
Os = clientInfo.OS.ToString(),
LoginIp = ipAddr,
//登录是没有token的所有是获取不到用户名需要在控制器赋值
//LoginUser = context.GetUserNameInfo(),
LoginLocation = "广州" + "-" + "深圳",
IsDeleted = false
};
return entity;
}
}
}