yemeishu 3 недель назад
Родитель
Сommit
ce1497a800
1 измененных файлов с 25 добавлено и 6 удалено
  1. 25 6
      app/Services/QuestionBankService.php

+ 25 - 6
app/Services/QuestionBankService.php

@@ -14,6 +14,11 @@ class QuestionBankService
         // 从配置文件读取base_url
         $this->baseUrl = config('services.question_bank.base_url', env('QUESTION_BANK_API_BASE', 'http://localhost:5015'));
         $this->baseUrl = rtrim($this->baseUrl, '/');
+
+        // 如果配置中不包含/api,则添加它
+        if (!str_ends_with($this->baseUrl, '/api')) {
+            $this->baseUrl .= '/api';
+        }
     }
 
     /**
@@ -213,9 +218,9 @@ class QuestionBankService
                 $params['callback_url'] = $callbackUrl;
             }
 
-            // 增加超时时间到60秒,确保有足够时间启动异步任务
-            // 注意:API是异步的,只需等待任务启动(1-2秒),不需要等待AI生成完成
-            $response = Http::timeout(60)
+            // 注意:这里的请求实际上是同步的,会等待响应
+            // 真正的异步应该使用 Http::async()
+            $response = Http::timeout(10) // 10秒超时应该足够获取响应
                 ->post($this->baseUrl . '/ai/generate-intelligent-questions', $params);
 
             if ($response->successful()) {
@@ -223,11 +228,23 @@ class QuestionBankService
             }
 
             Log::warning('题目生成API调用失败', [
-                'status' => $response->status()
+                'status' => $response->status(),
+                'body' => $response->body()
+            ]);
+        } catch (\Illuminate\Http\Client\ConnectionException $e) {
+            // 连接超时或网络错误
+            Log::error('题目生成连接异常', [
+                'error' => $e->getMessage(),
+                'message' => '可能的原因:1. AI服务未启动 2. 网络连接问题 3. 服务负载过高'
             ]);
+            return [
+                'success' => false,
+                'message' => '连接AI服务失败,请检查服务是否正常运行'
+            ];
         } catch (\Exception $e) {
             Log::error('题目生成异常', [
-                'error' => $e->getMessage()
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
             ]);
         }
 
@@ -819,8 +836,10 @@ class QuestionBankService
     public function checkHealth(): bool
     {
         try {
+            // 健康检查使用不带/api的路径
+            $healthUrl = str_replace('/api', '', $this->baseUrl) . '/health';
             $response = Http::timeout(5)
-                ->get($this->baseUrl . '/health');
+                ->get($healthUrl);
 
             return $response->successful();
         } catch (\Exception $e) {