| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace App\Module\ThirdParty\Events;
- use App\Module\ThirdParty\Models\ThirdPartyService;
- use App\Module\ThirdParty\Models\ThirdPartyCredential;
- use Illuminate\Foundation\Events\Dispatchable;
- use Illuminate\Queue\SerializesModels;
- /**
- * API调用事件
- *
- * 当第三方API被调用时触发此事件
- */
- class ApiCallEvent
- {
- use Dispatchable, SerializesModels;
- /**
- * 第三方服务实例
- *
- * @var ThirdPartyService
- */
- public ThirdPartyService $service;
- /**
- * 认证凭证实例
- *
- * @var ThirdPartyCredential
- */
- public ThirdPartyCredential $credential;
- /**
- * 请求ID
- *
- * @var string
- */
- public string $requestId;
- /**
- * HTTP方法
- *
- * @var string
- */
- public string $method;
- /**
- * 请求URL
- *
- * @var string
- */
- public string $url;
- /**
- * 请求头
- *
- * @var array
- */
- public array $headers;
- /**
- * 请求参数
- *
- * @var array
- */
- public array $params;
- /**
- * 响应数据
- *
- * @var array|null
- */
- public ?array $response;
- /**
- * 响应状态码
- *
- * @var int|null
- */
- public ?int $statusCode;
- /**
- * 响应时间(毫秒)
- *
- * @var int
- */
- public int $responseTime;
- /**
- * 是否成功
- *
- * @var bool
- */
- public bool $success;
- /**
- * 错误信息
- *
- * @var string|null
- */
- public ?string $errorMessage;
- /**
- * 创建事件实例
- *
- * @param ThirdPartyService $service
- * @param ThirdPartyCredential $credential
- * @param string $requestId
- * @param string $method
- * @param string $url
- * @param array $headers
- * @param array $params
- * @param array|null $response
- * @param int|null $statusCode
- * @param int $responseTime
- * @param bool $success
- * @param string|null $errorMessage
- */
- public function __construct(
- ThirdPartyService $service,
- ThirdPartyCredential $credential,
- string $requestId,
- string $method,
- string $url,
- array $headers,
- array $params,
- ?array $response = null,
- ?int $statusCode = null,
- int $responseTime = 0,
- bool $success = true,
- ?string $errorMessage = null
- ) {
- $this->service = $service;
- $this->credential = $credential;
- $this->requestId = $requestId;
- $this->method = $method;
- $this->url = $url;
- $this->headers = $headers;
- $this->params = $params;
- $this->response = $response;
- $this->statusCode = $statusCode;
- $this->responseTime = $responseTime;
- $this->success = $success;
- $this->errorMessage = $errorMessage;
- }
- /**
- * 获取事件数据数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'service_id' => $this->service->id,
- 'service_code' => $this->service->code,
- 'credential_id' => $this->credential->id,
- 'request_id' => $this->requestId,
- 'method' => $this->method,
- 'url' => $this->url,
- 'headers' => $this->headers,
- 'params' => $this->params,
- 'response' => $this->response,
- 'status_code' => $this->statusCode,
- 'response_time' => $this->responseTime,
- 'success' => $this->success,
- 'error_message' => $this->errorMessage,
- 'timestamp' => now()->toISOString(),
- ];
- }
- /**
- * 判断是否为错误调用
- *
- * @return bool
- */
- public function isError(): bool
- {
- return !$this->success || ($this->statusCode && $this->statusCode >= 400);
- }
- /**
- * 判断是否为慢调用
- *
- * @param int $threshold 阈值(毫秒)
- * @return bool
- */
- public function isSlowCall(int $threshold = 5000): bool
- {
- return $this->responseTime > $threshold;
- }
- /**
- * 获取日志级别
- *
- * @return string
- */
- public function getLogLevel(): string
- {
- if ($this->isError()) {
- return 'ERROR';
- }
-
- if ($this->isSlowCall()) {
- return 'WARNING';
- }
-
- return 'INFO';
- }
- }
|