ApiLogService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Entity;
  8. using GxPress.Service.Interface;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.Options;
  12. using GxPress.Service.Interface.VisitHistory;
  13. using GxPress.Repository.Interface.VisitHistory;
  14. using GxPress.EnumConst;
  15. namespace GxPress.Service.Implement
  16. {
  17. public class ApiLogService : IApiLogService
  18. {
  19. private readonly IConfiguration _configuration;
  20. private readonly Repository<UserLogin> _repository;
  21. private readonly Repository<User> _userRepository;
  22. private readonly IVisitHistoryService visitHistoryService;
  23. private readonly IVisitHistoryRepository visitHistoryRepository;
  24. public ApiLogService(IConfiguration configuration, IOptionsMonitor<DatabaseOptions> dbOptionsAccessor,
  25. IVisitHistoryService visitHistoryService, IVisitHistoryRepository visitHistoryRepository)
  26. {
  27. _configuration = configuration;
  28. var databaseType =
  29. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  30. //_connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  31. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  32. _repository = new Repository<UserLogin>(database);
  33. _userRepository = new Repository<User>(database);
  34. this.visitHistoryService = visitHistoryService;
  35. this.visitHistoryRepository = visitHistoryRepository;
  36. }
  37. public async Task DataSave(HttpContext context, long responseTime, int userId)
  38. {
  39. var isLog = _configuration.GetValue<bool>("ApiLog:IsEnable");
  40. var requestMethod = context.Request.Method;
  41. var requestURL = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}";
  42. //var accessToken = context.GetTokenAsync("access_token").Result;//添加身份验证的项目可以使用此方法获取到access_toekn
  43. var accessToken = string.Empty;
  44. var requestBody = string.Empty;
  45. //获取用户的历史记录
  46. var sourceDataId = 0;
  47. var sourceTypeId = 0;
  48. GetPathValue(context.Request.Path, out sourceTypeId, out sourceDataId);
  49. if (sourceDataId == 0 && sourceTypeId == 0)
  50. return;
  51. if (!await visitHistoryRepository.ExistsAsync(userId, sourceDataId, sourceTypeId))
  52. await visitHistoryService.InsertAsync(userId, sourceDataId, sourceTypeId);
  53. // if (requestMethod == "POST")
  54. // {
  55. // // //接收从微信后台POST过来的数据
  56. // // using (var reader = new StreamReader(context.Request.Body))
  57. // // {
  58. // // requestBody = await reader.ReadToEndAsync();
  59. // // }
  60. // // context.Request.Body.Seek(0, SeekOrigin.Begin);
  61. // // var _reader = new StreamReader(context.Request.Body);
  62. // // requestBody = _reader.ReadToEnd();
  63. // }
  64. // var model = new UserLogin
  65. // {
  66. // AccessToken = accessToken,
  67. // AccessTime = DateTime.Now,
  68. // AccessAction = requestMethod,
  69. // AccessApiUrl = requestURL,
  70. // QueryString = context.Request.QueryString.ToString(),
  71. // Body = requestBody,
  72. // HttpStatus = context.Response.StatusCode,
  73. // ClientIP = context.Connection.RemoteIpAddress.ToString(),
  74. // ResponseTime = responseTime,
  75. // UserId = userId
  76. // };
  77. // await _repository.InsertAsync(model);
  78. // //
  79. // await _userRepository.UpdateAsync(Q.Where(nameof(User.Id), userId).Set(nameof(User.LoginTime), DateTime.Now));
  80. //}
  81. }
  82. public void GetPathValue(string path, out int sourceTypeId, out int sourceDataId)
  83. {
  84. if (string.IsNullOrEmpty(path))
  85. {
  86. sourceDataId = 0; sourceTypeId = 0;
  87. return;
  88. }
  89. if (path.Contains("article/detail/"))
  90. {
  91. sourceTypeId = AllTypeConst.Article.GetHashCode();
  92. sourceDataId = int.Parse(path.Split('/')[path.Split('/').Length]);
  93. return;
  94. }
  95. sourceDataId = 0; sourceTypeId = 0;
  96. }
  97. }
  98. }