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; } }