baseUrl = config('services.question_bank.base_url', env('QUESTION_BANK_API_BASE', 'http://fa.test/api.questions.callback')); $this->baseUrl = rtrim($this->baseUrl, '/'); } /** * 获取题目列表 */ public function listQuestions(int $page = 1, int $perPage = 50, array $filters = []): array { try { $response = Http::timeout(10) ->get($this->baseUrl . '/questions', [ 'page' => $page, 'per_page' => $perPage, ...$filters ]); if ($response->successful()) { return $response->json(); } Log::warning('题库API调用失败', [ 'status' => $response->status() ]); } catch (\Exception $e) { Log::error('获取题目列表失败', [ 'error' => $e->getMessage() ]); } return ['data' => [], 'meta' => ['total' => 0]]; } /** * 智能生成题目(异步模式) */ public function generateIntelligentQuestions(array $params, ?string $callbackUrl = null): array { try { // 添加回调 URL if ($callbackUrl) { $params['callback_url'] = $callbackUrl; } $response = Http::timeout(10) ->post($this->baseUrl . '/generate-intelligent-questions', $params); if ($response->successful()) { return $response->json(); } Log::warning('题目生成API调用失败', [ 'status' => $response->status() ]); } catch (\Exception $e) { Log::error('题目生成异常', [ 'error' => $e->getMessage() ]); } return ['success' => false, 'message' => '生成失败']; } /** * 获取任务状态 */ public function getTaskStatus(string $taskId): ?array { try { $response = Http::timeout(10) ->get($this->baseUrl . '/tasks/' . $taskId); if ($response->successful()) { return $response->json(); } Log::warning('获取任务状态失败', [ 'task_id' => $taskId, 'status' => $response->status() ]); } catch (\Exception $e) { Log::error('获取任务状态异常', [ 'task_id' => $taskId, 'error' => $e->getMessage() ]); } return null; } /** * 获取任务列表 */ public function listTasks(?string $status = null, int $page = 1, int $perPage = 10): array { try { $params = [ 'page' => $page, 'per_page' => $perPage ]; if ($status) { $params['status'] = $status; } $response = Http::timeout(10) ->get($this->baseUrl . '/tasks', $params); if ($response->successful()) { return $response->json(); } Log::warning('获取任务列表失败', [ 'status' => $response->status() ]); } catch (\Exception $e) { Log::error('获取任务列表异常', [ 'error' => $e->getMessage() ]); } return ['data' => [], 'meta' => ['total' => 0]]; } /** * 获取题目统计信息 */ public function getStatistics(): array { try { $response = Http::timeout(10) ->get($this->baseUrl . '/questions/statistics'); if ($response->successful()) { return $response->json(); } Log::warning('获取题目统计失败', [ 'status' => $response->status() ]); } catch (\Exception $e) { Log::error('获取题目统计异常', [ 'error' => $e->getMessage() ]); } return [ 'total' => 0, 'by_difficulty' => [], 'by_kp' => [], 'by_source' => [] ]; } /** * 根据知识点获取题目 */ public function getQuestionsByKpCode(string $kpCode, int $limit = 100): array { try { $response = Http::timeout(10) ->get($this->baseUrl . '/questions', [ 'kp_code' => $kpCode, 'limit' => $limit ]); if ($response->successful()) { return $response->json(); } } catch (\Exception $e) { Log::error('根据知识点获取题目失败', [ 'kp_code' => $kpCode, 'error' => $e->getMessage() ]); } return []; } /** * 删除题目 */ public function deleteQuestion(string $questionCode): bool { try { $response = Http::timeout(10) ->delete($this->baseUrl . "/questions/{$questionCode}"); // 只有返回204(删除成功)才返回true,404(不存在)返回false if ($response->status() === 204) { return true; } if ($response->status() === 404) { Log::warning('尝试删除不存在的题目', ['question_code' => $questionCode]); return false; } return false; } catch (\Exception $e) { Log::error('删除题目失败', [ 'question_code' => $questionCode, 'error' => $e->getMessage() ]); return false; } } /** * 检查服务健康状态 */ public function checkHealth(): bool { try { $response = Http::timeout(5) ->get($this->baseUrl . '/health'); return $response->successful(); } catch (\Exception $e) { Log::error('题库服务健康检查失败', [ 'error' => $e->getMessage() ]); return false; } } }