Bläddra i källkod

教材相关 api

yemeishu 1 vecka sedan
förälder
incheckning
6f4a26122a

+ 63 - 5
app/Http/Controllers/Api/TextbookApiController.php

@@ -109,15 +109,20 @@ class TextbookApiController extends Controller
     public function getByGrade(int $grade, Request $request): JsonResponse
     {
         try {
-            // 根据年级自动判断学段
-            $stage = $this->getStageByGrade($grade);
+            $schoolingSystem = (string) $request->get('schooling_system', '');
+            $stageParam = $request->get('stage');
 
             $params = [
                 'grade' => $grade,
-                'stage' => $stage,
                 'per_page' => 100,
             ];
 
+            if ($stageParam) {
+                $params['stage'] = $this->convertStageToCode((string) $stageParam);
+            } elseif ($schoolingSystem === '5-4') {
+                $params['stage'] = $this->getStageByGrade($grade, $schoolingSystem);
+            }
+
             if ($request->has('semester')) {
                 $params['semester'] = $this->convertSemesterToCode($request->get('semester'));
             }
@@ -138,7 +143,8 @@ class TextbookApiController extends Controller
                 'data' => $textbooks,
                 'meta' => [
                     'grade' => $grade,
-                    'stage' => $this->getStageLabel($stage),
+                    'stage' => $params['stage'] ?? null,
+                    'stage_label' => isset($params['stage']) ? $this->getStageLabel($params['stage']) : null,
                     'total' => count($textbooks),
                 ]
             ]);
@@ -243,6 +249,49 @@ class TextbookApiController extends Controller
         }
     }
 
+    /**
+     * 年级枚举
+     */
+    public function getGradeEnums(): JsonResponse
+    {
+        $schoolingSystem = (string) request()->get('schooling_system', '');
+        $grades = [];
+
+        $primaryMax = $schoolingSystem === '5-4' ? 5 : 6;
+        foreach (range(1, $primaryMax) as $grade) {
+            $grades[] = [
+                'grade' => $grade,
+                'label' => $this->getGradeLabel($grade, 'primary'),
+                'stage' => 'primary',
+                'stage_label' => $this->getStageLabel('primary'),
+            ];
+        }
+
+        $juniorStart = $schoolingSystem === '5-4' ? 6 : 7;
+        foreach (range($juniorStart, 9) as $grade) {
+            $grades[] = [
+                'grade' => $grade,
+                'label' => $this->getGradeLabel($grade, 'junior'),
+                'stage' => 'junior',
+                'stage_label' => $this->getStageLabel('junior'),
+            ];
+        }
+
+        foreach (range(10, 12) as $grade) {
+            $grades[] = [
+                'grade' => $grade,
+                'label' => $this->getGradeLabel($grade, 'senior'),
+                'stage' => 'senior',
+                'stage_label' => $this->getStageLabel('senior'),
+            ];
+        }
+
+        return response()->json([
+            'success' => true,
+            'data' => $grades,
+        ]);
+    }
+
     /**
      * 获取教材目录
      *
@@ -416,8 +465,17 @@ class TextbookApiController extends Controller
     /**
      * 根据年级判断学段
      */
-    private function getStageByGrade(int $grade): string
+    private function getStageByGrade(int $grade, string $schoolingSystem = ''): string
     {
+        if ($schoolingSystem === '5-4') {
+            if ($grade >= 1 && $grade <= 5) {
+                return 'primary';
+            }
+            if ($grade >= 6 && $grade <= 9) {
+                return 'junior';
+            }
+        }
+
         if ($grade >= 1 && $grade <= 6) {
             return 'primary';
         } elseif ($grade >= 7 && $grade <= 9) {

+ 9 - 0
app/Services/TextbookApiService.php

@@ -210,6 +210,15 @@ class TextbookApiService
             if (isset($params['stage'])) {
                 $query->where('stage', $params['stage']);
             }
+            if (isset($params['grade'])) {
+                $query->where('grade', $params['grade']);
+            }
+            if (array_key_exists('semester', $params) && $params['semester'] !== null) {
+                $query->where('semester', $params['semester']);
+            }
+            if (isset($params['status'])) {
+                $query->where('status', $params['status']);
+            }
             $textbooks = $query->orderBy('id')->get();
             return ['data' => $textbooks->toArray(), 'meta' => ['total' => $textbooks->count()]];
         }

+ 4 - 0
routes/api.php

@@ -767,6 +767,10 @@ Route::get('/textbooks/grade/{grade}', [TextbookApiController::class, 'getByGrad
 Route::get('/textbooks/series', [TextbookApiController::class, 'getSeries'])
     ->name('api.textbooks.series');
 
+// 获取年级枚举
+Route::get('/textbooks/grades', [TextbookApiController::class, 'getGradeEnums'])
+    ->name('api.textbooks.grades');
+
 // 获取单个教材详情
 Route::get('/textbooks/{id}', [TextbookApiController::class, 'show'])
     ->name('api.textbooks.show');