LoginLogService.cs 2.3 KB

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