LoginLogService.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Microsoft.AspNetCore.Http;
  2. using System.Threading.Tasks;
  3. using ZhonTai.Common.Helpers;
  4. using ZhonTai.Admin.Core.Dto;
  5. using ZhonTai.Admin.Domain.LoginLog;
  6. using ZhonTai.Admin.Services.LoginLog.Dto;
  7. using ZhonTai.Admin.Domain;
  8. using ZhonTai.DynamicApi;
  9. using ZhonTai.DynamicApi.Attributes;
  10. using Microsoft.AspNetCore.Mvc;
  11. using ZhonTai.Admin.Core.Consts;
  12. namespace ZhonTai.Admin.Services.LoginLog;
  13. /// <summary>
  14. /// 登录日志服务
  15. /// </summary>
  16. [DynamicApi(Area = AdminConsts.AreaName)]
  17. public class LoginLogService : BaseService, ILoginLogService, IDynamicApi
  18. {
  19. private readonly IHttpContextAccessor _context;
  20. private readonly ILoginLogRepository _loginLogRepository;
  21. public LoginLogService(
  22. IHttpContextAccessor context,
  23. ILoginLogRepository loginLogRepository
  24. )
  25. {
  26. _context = context;
  27. _loginLogRepository = loginLogRepository;
  28. }
  29. /// <summary>
  30. /// 查询登录日志列表
  31. /// </summary>
  32. /// <param name="input"></param>
  33. /// <returns></returns>
  34. [HttpPost]
  35. public async Task<PageOutput<LoginLogListOutput>> GetPageAsync(PageInput<LogGetPageDto> input)
  36. {
  37. var userName = input.Filter?.CreatedUserName;
  38. var list = await _loginLogRepository.Select
  39. .WhereDynamicFilter(input.DynamicFilter)
  40. .WhereIf(userName.NotNull(), a => a.CreatedUserName.Contains(userName))
  41. .Count(out var total)
  42. .OrderByDescending(true, c => c.Id)
  43. .Page(input.CurrentPage, input.PageSize)
  44. .ToListAsync<LoginLogListOutput>();
  45. var data = new PageOutput<LoginLogListOutput>()
  46. {
  47. List = list,
  48. Total = total
  49. };
  50. return data;
  51. }
  52. /// <summary>
  53. /// 新增
  54. /// </summary>
  55. /// <param name="input"></param>
  56. /// <returns></returns>
  57. public async Task<long> AddAsync(LoginLogAddInput input)
  58. {
  59. input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);
  60. string ua = _context.HttpContext.Request.Headers["User-Agent"];
  61. if (ua.NotNull())
  62. {
  63. var client = UAParser.Parser.GetDefault().Parse(ua);
  64. var device = client.Device.Family;
  65. device = device.ToLower() == "other" ? "" : device;
  66. input.Browser = client.UA.Family;
  67. input.Os = client.OS.Family;
  68. input.Device = device;
  69. input.BrowserInfo = ua;
  70. }
  71. var entity = Mapper.Map<LoginLogEntity>(input);
  72. await _loginLogRepository.InsertAsync(entity);
  73. return entity.Id;
  74. }
  75. }