LoginLogService.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Threading.Tasks;
  2. using Microsoft.AspNetCore.Http;
  3. using AutoMapper;
  4. using Admin.Core.Common.Input;
  5. using Admin.Core.Common.Output;
  6. using Admin.Core.Model.Admin;
  7. using Admin.Core.Repository.Admin;
  8. using Admin.Core.Service.Admin.LoginLog.Input;
  9. using Admin.Core.Service.Admin.LoginLog.Output;
  10. using Admin.Core.Common.Helpers;
  11. namespace Admin.Core.Service.Admin.LoginLog
  12. {
  13. public class LoginLogService : ILoginLogService
  14. {
  15. private readonly IMapper _mapper;
  16. private readonly IHttpContextAccessor _context;
  17. private readonly ILoginLogRepository _loginLogRepository;
  18. public LoginLogService(
  19. IMapper mapper,
  20. IHttpContextAccessor context,
  21. ILoginLogRepository loginLogRepository
  22. )
  23. {
  24. _mapper = mapper;
  25. _context = context;
  26. _loginLogRepository = loginLogRepository;
  27. }
  28. public async Task<IResponseOutput> PageAsync(PageInput<LoginLogEntity> input)
  29. {
  30. var userName = input.Filter?.CreatedUserName;
  31. var list = await _loginLogRepository.Select
  32. .WhereIf(userName.NotNull(), a => a.CreatedUserName.Contains(userName))
  33. .Count(out var total)
  34. .OrderByDescending(true, c => c.Id)
  35. .Page(input.CurrentPage, input.PageSize)
  36. .ToListAsync<LoginLogListOutput>();
  37. var data = new PageOutput<LoginLogListOutput>()
  38. {
  39. List = list,
  40. Total = total
  41. };
  42. return ResponseOutput.Ok(data);
  43. }
  44. public async Task<IResponseOutput<long>> AddAsync(LoginLogAddInput input)
  45. {
  46. var res = new ResponseOutput<long>();
  47. input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);
  48. string ua = _context.HttpContext.Request.Headers["User-Agent"];
  49. var client = UAParser.Parser.GetDefault().Parse(ua);
  50. var device = client.Device.Family;
  51. device = device.ToLower() == "other" ? "" : device;
  52. input.Browser = client.UA.Family;
  53. input.Os = client.OS.Family;
  54. input.Device = device;
  55. input.BrowserInfo = ua;
  56. var entity = _mapper.Map<LoginLogEntity>(input);
  57. var id = (await _loginLogRepository.InsertAsync(entity)).Id;
  58. return id > 0 ? res.Ok(id) : res;
  59. }
  60. }
  61. }