RecordAPIOperationMiddleware.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Aspose.Words;
  2. using NPOI.HSSF.Record;
  3. using OASystem.Domain.Attributes;
  4. using OASystem.Domain.Entities.Customer;
  5. using OASystem.Infrastructure.Repositories.CRM;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.Diagnostics;
  8. using XAct;
  9. using static Google.Protobuf.Reflection.SourceCodeInfo.Types;
  10. namespace OASystem.API.Middlewares
  11. {
  12. /// <summary>
  13. /// 指定API操作记录信息
  14. /// </summary>
  15. public class RecordAPIOperationMiddleware
  16. {
  17. private readonly RequestDelegate _next;
  18. private readonly HttpClient _httpClient;
  19. private readonly IConfiguration _config;
  20. private readonly SqlSugarClient _sqlSugar;
  21. public RecordAPIOperationMiddleware(RequestDelegate next, IConfiguration config, HttpClient httpClient, SqlSugarClient sqlSugar)
  22. {
  23. _next = next;
  24. _httpClient = httpClient;
  25. _config = config;
  26. _sqlSugar = sqlSugar;
  27. }
  28. public async Task InvokeAsync(HttpContext context)
  29. {
  30. // 启用请求体流的缓冲,允许多次读取
  31. context.Request.EnableBuffering();
  32. // 读取请求体内容
  33. var requestBodyText = await ReadRequestBody(context.Request);
  34. // 检查控制器方法是否使用了自定义属性
  35. var endpoint = context.GetEndpoint();
  36. if (endpoint?.Metadata?.GetMetadata<ApiLogAttribute>() != null)
  37. {
  38. var logInfo = new Crm_TableOperationRecord();
  39. logInfo.ActionName = context.Request.Path;
  40. logInfo.UpdatePreData = requestBodyText;
  41. // 保存原始响应体流
  42. var originalResponseBody = context.Response.Body;
  43. // 创建一个新的内存流来捕获响应体
  44. using var responseMemoryStream = new MemoryStream();
  45. context.Response.Body = responseMemoryStream;
  46. // 调用下一个中间件
  47. await _next(context);
  48. // 重置响应体流的位置
  49. responseMemoryStream.Position = 0;
  50. // 读取响应体内容
  51. var responseBodyText = await new StreamReader(responseMemoryStream).ReadToEndAsync();
  52. // 将响应体内容写回原始响应体流
  53. await responseMemoryStream.CopyToAsync(originalResponseBody);
  54. logInfo.UpdateBefData = responseBodyText;
  55. // 存储到数据库
  56. //_sqlSugar.ChangeDatabase(DBEnum.OA2023DB);
  57. await _sqlSugar.Insertable(logInfo).ExecuteCommandAsync();
  58. }
  59. else
  60. {
  61. // 调用下一个中间件
  62. await _next(context);
  63. }
  64. }
  65. private async Task<string> ReadRequestBody(HttpRequest request)
  66. {
  67. request.EnableBuffering();
  68. // 读取请求体内容
  69. using var reader = new StreamReader(request.Body, Encoding.UTF8, true, 1024, true);
  70. var requestBodyText = await reader.ReadToEndAsync();
  71. // 重置请求体流的位置
  72. request.Body.Position = 0;
  73. return requestBodyText;
  74. }
  75. private async Task<string> GetExternalIp()
  76. {
  77. var response = await _httpClient.GetAsync("https://ifconfig.me");
  78. response.EnsureSuccessStatusCode();
  79. return await response.Content.ReadAsStringAsync();
  80. }
  81. private async Task<string> GetIpLocation(string ip)
  82. {
  83. var response = await _httpClient.GetAsync($"https://ipinfo.io/{ip}/json");
  84. response.EnsureSuccessStatusCode();
  85. var json = await response.Content.ReadAsStringAsync();
  86. var ipInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
  87. return $"{ipInfo.country}, {ipInfo.city}";
  88. }
  89. }
  90. }