api.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. use App\Services\QuestionServiceApi;
  3. use Illuminate\Support\Facades\Route;
  4. use Illuminate\Support\Facades\Log;
  5. /*
  6. |--------------------------------------------------------------------------
  7. | 题库管理 API 路由
  8. |--------------------------------------------------------------------------
  9. */
  10. // 接收题目生成回调
  11. Route::post('/questions/callback', function () {
  12. try {
  13. $data = request()->all();
  14. Log::info('Received question generation callback', $data);
  15. // 验证回调数据
  16. if (!isset($data['task_id']) || !isset($data['status'])) {
  17. return response()->json(['error' => 'Invalid callback data'], 400);
  18. }
  19. // 存储回调结果到 session 或缓存中,供前端查询
  20. session(['question_gen_callback_' . $data['task_id'] => $data]);
  21. return response()->json(['success' => true, 'message' => 'Callback received']);
  22. } catch (\Exception $e) {
  23. Log::error('Callback processing failed: ' . $e->getMessage());
  24. return response()->json(['error' => $e->getMessage()], 500);
  25. }
  26. })->name('api.questions.callback');
  27. // 获取题目生成回调结果
  28. Route::get('/questions/callback/{taskId}', function (string $taskId) {
  29. $callbackData = session('question_gen_callback_' . $taskId);
  30. if ($callbackData) {
  31. // 清除已读取的回调数据
  32. session()->forget('question_gen_callback_' . $taskId);
  33. return response()->json($callbackData);
  34. }
  35. return response()->json(['status' => 'pending'], 202);
  36. })->name('api.questions.callback.get');
  37. // 题目相关 API
  38. Route::get('/questions', function (QuestionServiceApi $service) {
  39. try {
  40. $page = (int) request()->get('page', 1);
  41. $perPage = (int) request()->get('per_page', 25);
  42. $filters = [
  43. 'kp_code' => request()->get('kp_code'),
  44. 'difficulty' => request()->get('difficulty'),
  45. 'search' => request()->get('search'),
  46. ];
  47. $response = $service->listQuestions($page, $perPage, $filters);
  48. return response()->json($response);
  49. } catch (\Exception $e) {
  50. \Log::error('Failed to fetch questions: ' . $e->getMessage());
  51. return response()->json([
  52. 'data' => [],
  53. 'meta' => [
  54. 'page' => 1,
  55. 'per_page' => 25,
  56. 'total' => 0,
  57. 'total_pages' => 0,
  58. ],
  59. 'error' => $e->getMessage(),
  60. ], 500);
  61. }
  62. });
  63. // 获取题目统计信息
  64. Route::get('/questions/statistics', function (QuestionServiceApi $service) {
  65. try {
  66. $stats = $service->getStatistics();
  67. return response()->json($stats);
  68. } catch (\Exception $e) {
  69. \Log::error('Failed to get question statistics: ' . $e->getMessage());
  70. return response()->json(['error' => $e->getMessage()], 500);
  71. }
  72. });
  73. // 语义搜索题目
  74. Route::post('/questions/search', function (QuestionServiceApi $service) {
  75. try {
  76. $data = request()->only(['query', 'limit']);
  77. $results = $service->searchQuestions($data['query'], $data['limit'] ?? 20);
  78. return response()->json($results);
  79. } catch (\Exception $e) {
  80. \Log::error('Question search failed: ' . $e->getMessage());
  81. return response()->json(['error' => $e->getMessage()], 500);
  82. }
  83. });
  84. // 获取单个题目详情
  85. Route::get('/questions/{id}', function (int $id, QuestionServiceApi $service) {
  86. try {
  87. $question = $service->getQuestionById($id);
  88. if (!$question) {
  89. return response()->json(['error' => 'Question not found'], 404);
  90. }
  91. return response()->json($question);
  92. } catch (\Exception $e) {
  93. \Log::error("Failed to get question {$id}: " . $e->getMessage());
  94. return response()->json(['error' => $e->getMessage()], 500);
  95. }
  96. });
  97. // AI 生成题目
  98. Route::post('/questions/generate', function (QuestionServiceApi $service) {
  99. try {
  100. $data = request()->only(['kp_code', 'keyword', 'count', 'strategy']);
  101. $result = $service->generateQuestions($data);
  102. return response()->json($result);
  103. } catch (\Exception $e) {
  104. \Log::error('Question generation failed: ' . $e->getMessage());
  105. return response()->json([
  106. 'success' => false,
  107. 'message' => $e->getMessage(),
  108. ], 500);
  109. }
  110. });
  111. // 删除题目
  112. Route::delete('/questions/{id}', function (int $id, QuestionServiceApi $service) {
  113. try {
  114. $deleted = $service->deleteQuestion($id);
  115. return response()->json([
  116. 'success' => $deleted,
  117. 'message' => $deleted ? 'Question deleted' : 'Failed to delete',
  118. ]);
  119. } catch (\Exception $e) {
  120. \Log::error("Failed to delete question {$id}: " . $e->getMessage());
  121. return response()->json([
  122. 'success' => false,
  123. 'message' => $e->getMessage(),
  124. ], 500);
  125. }
  126. });
  127. // 获取知识点选项
  128. Route::get('/knowledge-points', function (QuestionServiceApi $service) {
  129. try {
  130. $points = $service->getKnowledgePointOptions();
  131. return response()->json($points);
  132. } catch (\Exception $e) {
  133. \Log::error('Failed to get knowledge points: ' . $e->getMessage());
  134. return response()->json([], 500);
  135. }
  136. });
  137. /*
  138. |--------------------------------------------------------------------------
  139. | MathRecSys 集成 API 路由
  140. |--------------------------------------------------------------------------
  141. */
  142. use App\Http\Controllers\Api\StudentController;
  143. // 健康检查
  144. Route::get('/mathrecsys/health', [StudentController::class, 'checkServiceHealth'])->name('api.mathrecsys.health');
  145. // 学生相关 API
  146. Route::prefix('mathrecsys/students')->name('api.mathrecsys.students.')->group(function () {
  147. // 获取学生完整信息
  148. Route::get('{studentId}', [StudentController::class, 'show'])->name('show');
  149. // 获取个性化推荐
  150. Route::get('{studentId}/recommendations', [StudentController::class, 'getRecommendations'])->name('recommendations');
  151. // 获取学习轨迹
  152. Route::get('{studentId}/trajectory', [StudentController::class, 'getTrajectory'])->name('trajectory');
  153. // 获取学习建议
  154. Route::get('{studentId}/suggestions', [StudentController::class, 'getSuggestions'])->name('suggestions');
  155. // 智能分析题目
  156. Route::post('{studentId}/analyze', [StudentController::class, 'analyzeQuestion'])->name('analyze');
  157. // 更新掌握度
  158. Route::put('{studentId}/mastery', [StudentController::class, 'updateMastery'])->name('update-mastery');
  159. });
  160. // 班级分析 API
  161. Route::prefix('mathrecsys/classes')->name('api.mathrecsys.classes.')->group(function () {
  162. Route::get('{classId}/analysis', [StudentController::class, 'classAnalysis'])->name('analysis');
  163. });
  164. // 测试 API
  165. Route::get('/mathrecsys/test', function () {
  166. return response()->json([
  167. 'success' => true,
  168. 'message' => 'MathRecSys API integration is working',
  169. 'timestamp' => now()->toISOString()
  170. ]);
  171. })->name('api.mathrecsys.test');