1
0

OprationLogService.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Admin.Core.Common.Helpers;
  2. using Admin.Core.Common.Input;
  3. using Admin.Core.Common.Output;
  4. using Admin.Core.Model.Admin;
  5. using Admin.Core.Repository.Admin;
  6. using Admin.Core.Service.Admin.OprationLog.Input;
  7. using Admin.Core.Service.Admin.OprationLog.Output;
  8. using Microsoft.AspNetCore.Http;
  9. using System.Threading.Tasks;
  10. namespace Admin.Core.Service.Admin.OprationLog
  11. {
  12. public class OprationLogService : BaseService, IOprationLogService
  13. {
  14. private readonly IHttpContextAccessor _context;
  15. private readonly IOprationLogRepository _oprationLogRepository;
  16. public OprationLogService(
  17. IHttpContextAccessor context,
  18. IOprationLogRepository oprationLogRepository
  19. )
  20. {
  21. _context = context;
  22. _oprationLogRepository = oprationLogRepository;
  23. }
  24. public async Task<IResponseOutput> PageAsync(PageInput<OprationLogEntity> input)
  25. {
  26. var userName = input.Filter?.CreatedUserName;
  27. var list = await _oprationLogRepository.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<OprationLogListOutput>();
  33. var data = new PageOutput<OprationLogListOutput>()
  34. {
  35. List = list,
  36. Total = total
  37. };
  38. return ResponseOutput.Ok(data);
  39. }
  40. public async Task<IResponseOutput> AddAsync(OprationLogAddInput input)
  41. {
  42. string ua = _context.HttpContext.Request.Headers["User-Agent"];
  43. var client = UAParser.Parser.GetDefault().Parse(ua);
  44. var device = client.Device.Family;
  45. device = device.ToLower() == "other" ? "" : device;
  46. input.Browser = client.UA.Family;
  47. input.Os = client.OS.Family;
  48. input.Device = device;
  49. input.BrowserInfo = ua;
  50. input.NickName = User.NickName;
  51. input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);
  52. var entity = Mapper.Map<OprationLogEntity>(input);
  53. var id = (await _oprationLogRepository.InsertAsync(entity)).Id;
  54. return ResponseOutput.Result(id > 0);
  55. }
  56. }
  57. }