0
0

OprationLogService.cs 2.5 KB

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