ApiCallEvent.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\Module\ThirdParty\Events;
  3. use App\Module\ThirdParty\Models\ThirdPartyService;
  4. use App\Module\ThirdParty\Models\ThirdPartyCredential;
  5. use Illuminate\Foundation\Events\Dispatchable;
  6. use Illuminate\Queue\SerializesModels;
  7. /**
  8. * API调用事件
  9. *
  10. * 当第三方API被调用时触发此事件
  11. */
  12. class ApiCallEvent
  13. {
  14. use Dispatchable, SerializesModels;
  15. /**
  16. * 第三方服务实例
  17. *
  18. * @var ThirdPartyService
  19. */
  20. public ThirdPartyService $service;
  21. /**
  22. * 认证凭证实例
  23. *
  24. * @var ThirdPartyCredential
  25. */
  26. public ThirdPartyCredential $credential;
  27. /**
  28. * 请求ID
  29. *
  30. * @var string
  31. */
  32. public string $requestId;
  33. /**
  34. * HTTP方法
  35. *
  36. * @var string
  37. */
  38. public string $method;
  39. /**
  40. * 请求URL
  41. *
  42. * @var string
  43. */
  44. public string $url;
  45. /**
  46. * 请求头
  47. *
  48. * @var array
  49. */
  50. public array $headers;
  51. /**
  52. * 请求参数
  53. *
  54. * @var array
  55. */
  56. public array $params;
  57. /**
  58. * 响应数据
  59. *
  60. * @var array|null
  61. */
  62. public ?array $response;
  63. /**
  64. * 响应状态码
  65. *
  66. * @var int|null
  67. */
  68. public ?int $statusCode;
  69. /**
  70. * 响应时间(毫秒)
  71. *
  72. * @var int
  73. */
  74. public int $responseTime;
  75. /**
  76. * 是否成功
  77. *
  78. * @var bool
  79. */
  80. public bool $success;
  81. /**
  82. * 错误信息
  83. *
  84. * @var string|null
  85. */
  86. public ?string $errorMessage;
  87. /**
  88. * 创建事件实例
  89. *
  90. * @param ThirdPartyService $service
  91. * @param ThirdPartyCredential $credential
  92. * @param string $requestId
  93. * @param string $method
  94. * @param string $url
  95. * @param array $headers
  96. * @param array $params
  97. * @param array|null $response
  98. * @param int|null $statusCode
  99. * @param int $responseTime
  100. * @param bool $success
  101. * @param string|null $errorMessage
  102. */
  103. public function __construct(
  104. ThirdPartyService $service,
  105. ThirdPartyCredential $credential,
  106. string $requestId,
  107. string $method,
  108. string $url,
  109. array $headers,
  110. array $params,
  111. ?array $response = null,
  112. ?int $statusCode = null,
  113. int $responseTime = 0,
  114. bool $success = true,
  115. ?string $errorMessage = null
  116. ) {
  117. $this->service = $service;
  118. $this->credential = $credential;
  119. $this->requestId = $requestId;
  120. $this->method = $method;
  121. $this->url = $url;
  122. $this->headers = $headers;
  123. $this->params = $params;
  124. $this->response = $response;
  125. $this->statusCode = $statusCode;
  126. $this->responseTime = $responseTime;
  127. $this->success = $success;
  128. $this->errorMessage = $errorMessage;
  129. }
  130. /**
  131. * 获取事件数据数组
  132. *
  133. * @return array
  134. */
  135. public function toArray(): array
  136. {
  137. return [
  138. 'service_id' => $this->service->id,
  139. 'service_code' => $this->service->code,
  140. 'credential_id' => $this->credential->id,
  141. 'request_id' => $this->requestId,
  142. 'method' => $this->method,
  143. 'url' => $this->url,
  144. 'headers' => $this->headers,
  145. 'params' => $this->params,
  146. 'response' => $this->response,
  147. 'status_code' => $this->statusCode,
  148. 'response_time' => $this->responseTime,
  149. 'success' => $this->success,
  150. 'error_message' => $this->errorMessage,
  151. 'timestamp' => now()->toISOString(),
  152. ];
  153. }
  154. /**
  155. * 判断是否为错误调用
  156. *
  157. * @return bool
  158. */
  159. public function isError(): bool
  160. {
  161. return !$this->success || ($this->statusCode && $this->statusCode >= 400);
  162. }
  163. /**
  164. * 判断是否为慢调用
  165. *
  166. * @param int $threshold 阈值(毫秒)
  167. * @return bool
  168. */
  169. public function isSlowCall(int $threshold = 5000): bool
  170. {
  171. return $this->responseTime > $threshold;
  172. }
  173. /**
  174. * 获取日志级别
  175. *
  176. * @return string
  177. */
  178. public function getLogLevel(): string
  179. {
  180. if ($this->isError()) {
  181. return 'ERROR';
  182. }
  183. if ($this->isSlowCall()) {
  184. return 'WARNING';
  185. }
  186. return 'INFO';
  187. }
  188. }