| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace App\Module\ThirdParty\Services;
- use App\Module\ThirdParty\Dto\Config;
- use App\Module\ThirdParty\Dto\Credential;
- use App\Module\ThirdParty\Models\ThirdPartyService as ServiceModel;
- use App\Module\ThirdParty\Models\ThirdPartyLog;
- use App\Module\ThirdParty\Enums\LOG_LEVEL;
- use Illuminate\Http\Request;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Support\Facades\Log;
- /**
- * Webhook接收器基类
- *
- * 专门用于接收第三方Webhook通知
- * 与BaseWebhook不同,这个类专注于接收而不是发送
- */
- abstract class WebhookReceiver
- {
- /**
- * 服务代码
- */
- protected string $serviceCode;
- /**
- * 服务配置
- */
- protected ?ServiceModel $service = null;
- /**
- * 请求对象
- */
- protected Request $request;
- /**
- * 请求ID
- */
- protected string $requestId;
- /**
- * 开始时间
- */
- protected float $startTime;
- /**
- * 构造函数
- *
- * @param string $serviceCode 服务代码
- * @param Request $request 请求对象
- * @param ServiceModel $service 服务配置对象
- */
- public function __construct(string $serviceCode, Request $request, ServiceModel $service)
- {
- $this->serviceCode = $serviceCode;
- $this->request = $request;
- $this->service = $service;
- $this->requestId = uniqid('webhook_', true);
- $this->startTime = microtime(true);
- }
- /**
- * 处理Webhook请求
- *
- * @param string $action 操作类型
- * @return JsonResponse
- */
- public function handle(string $action): JsonResponse
- {
- try {
- // 验证请求格式
- $this->validateRequest();
- // 执行具体的处理逻辑
- $result = $this->handler($action, $this->request);
- // 记录成功日志
- $this->logWebhook($action, $this->request->all(), $result, true);
- return response()->json([
- 'success' => true,
- 'data' => $result
- ]);
- } catch (\Exception $e) {
- // 记录失败日志
- $this->logWebhook($action, $this->request->all(), ['error' => $e->getMessage()], false);
- Log::error("Webhook处理失败: {$this->serviceCode}/{$action}", [
- 'error' => $e->getMessage(),
- 'request_id' => $this->requestId,
- 'request_data' => $this->request->all(),
- ]);
- return response()->json([
- 'success' => false,
- 'error' => $e->getMessage(),
- 'request_id' => $this->requestId,
- ], 400);
- }
- }
- /**
- * 具体的Webhook处理逻辑(由子类实现)
- *
- * @param string $action 操作类型
- * @param Request $request 请求对象
- * @return array
- */
- abstract protected function handler(string $action, Request $request): array;
- /**
- *
- * @return Config
- */
- abstract protected function getConfigDto():Config;
- /**
- * 验证请求格式(由子类重写)
- *
- * @throws \Exception
- */
- protected function validateRequest(): void
- {
- // 默认不做验证,子类可以重写此方法
- }
- /**
- * 记录Webhook日志
- *
- * @param string $action 操作类型
- * @param array $requestData 请求数据
- * @param array $result 处理结果
- * @param bool $success 是否成功
- */
- protected function logWebhook(string $action, array $requestData, array $result, bool $success): void
- {
- $responseTime = (int)((microtime(true) - $this->startTime) * 1000);
- // 在响应体中包含action信息
- $logResult = array_merge($result, ['webhook_action' => $action]);
- ThirdPartyLog::create([
- 'service_id' => $this->service->id,
- 'credential_id' => null, // Webhook接收器不需要凭证
- 'request_id' => $this->requestId,
- 'method' => 'WEBHOOK',
- 'url' => $this->request->fullUrl(),
- 'request_headers' => json_encode($this->request->headers->all()),
- 'request_body' => json_encode($requestData),
- 'response_status' => $success ? 200 : 400,
- 'response_headers' => json_encode([]),
- 'response_body' => json_encode($logResult),
- 'response_time' => $responseTime,
- 'success' => $success,
- 'error_message' => $success ? null : ($result['error'] ?? '未知错误'),
- 'level' => $success ? LOG_LEVEL::INFO->value : LOG_LEVEL::ERROR->value,
- 'ip_address' => $this->request->ip(),
- 'user_agent' => $this->request->userAgent(),
- 'called_at' => now(),
- ]);
- }
- /**
- * 获取服务配置
- *
- * @param string|null $key 配置键名,为空则返回全部配置
- * @return mixed
- */
- protected function getConfig(?string $key = null)
- {
- $config = $this->service->config ?? [];
- if ($key === null) {
- return $config;
- }
- return $config[$key] ?? null;
- }
- /**
- * 获取服务信息
- *
- * @return ServiceModel
- */
- protected function getService(): ServiceModel
- {
- return $this->service;
- }
- /**
- * 获取请求ID
- *
- * @return string
- */
- protected function getRequestId(): string
- {
- return $this->requestId;
- }
- /**
- * 获取请求对象
- *
- * @return Request
- */
- protected function getRequest(): Request
- {
- return $this->request;
- }
- }
|