1
0

LoginLogService.cs 2.3 KB

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