IdempotentAttribute.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // 大名科技(天津)有限公司 版权所有
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
  6. //
  7. // 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
  8. using System.Security.Claims;
  9. namespace Admin.NET.Core;
  10. /// <summary>
  11. /// 防止重复请求过滤器特性
  12. /// </summary>
  13. [SuppressSniffer]
  14. [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
  15. public class IdempotentAttribute : Attribute, IAsyncActionFilter
  16. {
  17. /// <summary>
  18. /// 请求间隔时间/秒
  19. /// </summary>
  20. public int IntervalTime { get; set; } = 5;
  21. /// <summary>
  22. /// 错误提示内容
  23. /// </summary>
  24. public string Message { get; set; } = "你操作频率过快,请稍后重试!";
  25. /// <summary>
  26. /// 缓存前缀: Key+请求路由+用户Id+请求参数
  27. /// </summary>
  28. public string CacheKey { get; set; }
  29. /// <summary>
  30. /// 是否直接抛出异常:Ture是,False返回上次请求结果
  31. /// </summary>
  32. public bool ThrowBah { get; set; }
  33. public IdempotentAttribute()
  34. {
  35. }
  36. public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  37. {
  38. var httpContext = context.HttpContext;
  39. var path = httpContext.Request.Path.Value.ToString();
  40. var userId = httpContext.User?.FindFirstValue(ClaimConst.UserId);
  41. var cacheExpireTime = TimeSpan.FromSeconds(IntervalTime);
  42. var parameters = "";
  43. foreach (var parameter in context.ActionDescriptor.Parameters)
  44. {
  45. parameters += parameter.Name;
  46. parameters += context.ActionArguments[parameter.Name].ToJson();
  47. }
  48. var cacheKey = MD5Encryption.Encrypt($"{CacheKey}{path}{userId}{parameters}");
  49. var sysCacheService = App.GetRequiredService<SysCacheService>();
  50. if (sysCacheService.ExistKey(cacheKey))
  51. {
  52. if (ThrowBah) throw Oops.Oh(Message);
  53. try
  54. {
  55. var cachedResult = sysCacheService.Get<ResponseData>(cacheKey);
  56. context.Result = new ObjectResult(cachedResult.Value);
  57. }
  58. catch (Exception ex)
  59. {
  60. throw Oops.Oh($"{Message}-{ex}");
  61. }
  62. }
  63. else
  64. {
  65. // 先加入一个空缓存,防止第一次请求结果没回来导致连续请求
  66. sysCacheService.Set(cacheKey, "", cacheExpireTime);
  67. var resultContext = await next();
  68. if (resultContext.Result is ObjectResult objectResult)
  69. {
  70. var valueType = objectResult.Value.GetType();
  71. var responseData = new ResponseData
  72. {
  73. Type = valueType.Name,
  74. Value = objectResult.Value
  75. };
  76. sysCacheService.Set(cacheKey, responseData, cacheExpireTime);
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// 请求结果数据
  82. /// </summary>
  83. private class ResponseData
  84. {
  85. /// <summary>
  86. /// 结果类型
  87. /// </summary>
  88. public string Type { get; set; }
  89. /// <summary>
  90. /// 请求结果
  91. /// </summary>
  92. public dynamic Value { get; set; }
  93. }
  94. }