OpenApiRateLimit.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Module\OpenAPI\Models;
  3. use UCore\ModelCore;
  4. /**
  5. * OpenAPI频率限制记录模型
  6. *
  7. * @property int $id
  8. * @property string $app_id 应用ID
  9. * @property string $ip_address IP地址
  10. * @property string $endpoint 接口端点
  11. * @property string $limit_type 限制类型
  12. * @property \Carbon\Carbon $window_start 时间窗口开始
  13. * @property int $request_count 请求次数
  14. * @property bool $is_blocked 是否被阻止
  15. * @property string|null $user_agent 用户代理
  16. * @property array|null $headers 请求头信息
  17. * @property \Carbon\Carbon $created_at
  18. * @property \Carbon\Carbon $updated_at
  19. */
  20. class OpenApiRateLimit extends ModelCore
  21. {
  22. // field start
  23. protected $fillable = [
  24. 'app_id',
  25. 'ip_address',
  26. 'endpoint',
  27. 'limit_type',
  28. 'window_start',
  29. 'request_count',
  30. 'is_blocked',
  31. 'user_agent',
  32. 'headers',
  33. ];
  34. // field end
  35. /**
  36. * 数据类型转换
  37. *
  38. * @var array
  39. */
  40. protected $casts = [
  41. 'window_start' => 'datetime',
  42. 'request_count' => 'integer',
  43. 'is_blocked' => 'boolean',
  44. 'headers' => 'array',
  45. ];
  46. /**
  47. * 关联应用模型
  48. *
  49. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  50. */
  51. public function app()
  52. {
  53. return $this->belongsTo(OpenApiApp::class, 'app_id', 'app_id');
  54. }
  55. /**
  56. * 获取限制类型的中文名称
  57. *
  58. * @return string
  59. */
  60. public function getLimitTypeNameAttribute(): string
  61. {
  62. return match ($this->limit_type) {
  63. 'requests_per_minute' => '每分钟请求数',
  64. 'requests_per_hour' => '每小时请求数',
  65. 'requests_per_day' => '每天请求数',
  66. 'requests_per_week' => '每周请求数',
  67. 'requests_per_month' => '每月请求数',
  68. default => $this->limit_type,
  69. };
  70. }
  71. /**
  72. * 获取状态标签
  73. *
  74. * @return string
  75. */
  76. public function getStatusLabelAttribute(): string
  77. {
  78. return $this->is_blocked ? '已阻止' : '正常';
  79. }
  80. /**
  81. * 获取状态颜色
  82. *
  83. * @return string
  84. */
  85. public function getStatusColorAttribute(): string
  86. {
  87. return $this->is_blocked ? 'danger' : 'success';
  88. }
  89. /**
  90. * 按应用ID查询
  91. *
  92. * @param \Illuminate\Database\Eloquent\Builder $query
  93. * @param string $appId
  94. * @return \Illuminate\Database\Eloquent\Builder
  95. */
  96. public function scopeByApp($query, string $appId)
  97. {
  98. return $query->where('app_id', $appId);
  99. }
  100. /**
  101. * 按IP地址查询
  102. *
  103. * @param \Illuminate\Database\Eloquent\Builder $query
  104. * @param string $ip
  105. * @return \Illuminate\Database\Eloquent\Builder
  106. */
  107. public function scopeByIp($query, string $ip)
  108. {
  109. return $query->where('ip_address', $ip);
  110. }
  111. /**
  112. * 按限制类型查询
  113. *
  114. * @param \Illuminate\Database\Eloquent\Builder $query
  115. * @param string $limitType
  116. * @return \Illuminate\Database\Eloquent\Builder
  117. */
  118. public function scopeByLimitType($query, string $limitType)
  119. {
  120. return $query->where('limit_type', $limitType);
  121. }
  122. /**
  123. * 查询被阻止的记录
  124. *
  125. * @param \Illuminate\Database\Eloquent\Builder $query
  126. * @return \Illuminate\Database\Eloquent\Builder
  127. */
  128. public function scopeBlocked($query)
  129. {
  130. return $query->where('is_blocked', true);
  131. }
  132. /**
  133. * 按时间范围查询
  134. *
  135. * @param \Illuminate\Database\Eloquent\Builder $query
  136. * @param \Carbon\Carbon $start
  137. * @param \Carbon\Carbon $end
  138. * @return \Illuminate\Database\Eloquent\Builder
  139. */
  140. public function scopeInTimeRange($query, $start, $end)
  141. {
  142. return $query->whereBetween('window_start', [$start, $end]);
  143. }
  144. /**
  145. * 获取最近的限制记录
  146. *
  147. * @param \Illuminate\Database\Eloquent\Builder $query
  148. * @param int $hours
  149. * @return \Illuminate\Database\Eloquent\Builder
  150. */
  151. public function scopeRecent($query, int $hours = 24)
  152. {
  153. return $query->where('window_start', '>=', now()->subHours($hours));
  154. }
  155. }