|
|
@@ -16,19 +16,38 @@ class KnowledgePointTreeController
|
|
|
*
|
|
|
* @param Request $request
|
|
|
* @return JsonResponse
|
|
|
+ *
|
|
|
+ * 支持参数:
|
|
|
+ * - stage: 学段筛选(primary=小学1-6, junior=初中7-9, senior=高中10-12)
|
|
|
*/
|
|
|
public function index(Request $request): JsonResponse
|
|
|
{
|
|
|
try {
|
|
|
- // 使用缓存避免重复查询
|
|
|
- $cacheKey = 'knowledge-point-tree-' . md5($request->getQueryString() ?? '');
|
|
|
- $treeData = Cache::remember($cacheKey, 3600, function () {
|
|
|
- return $this->buildTreeFromDatabase();
|
|
|
+ // 获取学段参数
|
|
|
+ $stage = $request->input('stage');
|
|
|
+
|
|
|
+ // 验证学段参数
|
|
|
+ if ($stage && !in_array($stage, ['primary', 'junior', 'senior'])) {
|
|
|
+ return response()->json([
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => '无效的学段参数,可选值:primary(小学)、junior(初中)、senior(高中)',
|
|
|
+ 'data' => [],
|
|
|
+ ], 400);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用缓存避免重复查询(缓存 key 包含 stage 参数)
|
|
|
+ $cacheKey = 'knowledge-point-tree-' . ($stage ?? 'all') . '-' . md5($request->getQueryString() ?? '');
|
|
|
+ $treeData = Cache::remember($cacheKey, 3600, function () use ($stage) {
|
|
|
+ return $this->buildTreeFromDatabase($stage);
|
|
|
});
|
|
|
|
|
|
return response()->json([
|
|
|
'success' => true,
|
|
|
'data' => $treeData,
|
|
|
+ 'meta' => [
|
|
|
+ 'stage' => $stage,
|
|
|
+ 'stage_label' => $this->getStageLabel($stage),
|
|
|
+ ],
|
|
|
]);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取知识点树形结构失败', [
|
|
|
@@ -44,16 +63,49 @@ class KnowledgePointTreeController
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取学段中文标签
|
|
|
+ */
|
|
|
+ private function getStageLabel(?string $stage): ?string
|
|
|
+ {
|
|
|
+ return match ($stage) {
|
|
|
+ 'primary' => '小学',
|
|
|
+ 'junior' => '初中',
|
|
|
+ 'senior' => '高中',
|
|
|
+ default => null,
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 从数据库构建树形结构
|
|
|
*
|
|
|
+ * @param string|null $stage 学段筛选:primary(小学1-6)、junior(初中7-9)、senior(高中10-12)
|
|
|
* @return array
|
|
|
*/
|
|
|
- private function buildTreeFromDatabase(): array
|
|
|
+ private function buildTreeFromDatabase(?string $stage = null): array
|
|
|
{
|
|
|
- // 从数据库获取所有知识点
|
|
|
- $knowledgePoints = KnowledgePoint::query()
|
|
|
- ->orderBy('kp_code')
|
|
|
+ // 从数据库获取知识点
|
|
|
+ $query = KnowledgePoint::query();
|
|
|
+
|
|
|
+ // 根据学段筛选年级范围
|
|
|
+ if ($stage) {
|
|
|
+ $gradeRange = match ($stage) {
|
|
|
+ 'primary' => [1, 6], // 小学 1-6 年级
|
|
|
+ 'junior' => [7, 9], // 初中 7-9 年级
|
|
|
+ 'senior' => [10, 12], // 高中 10-12 年级
|
|
|
+ default => null,
|
|
|
+ };
|
|
|
+
|
|
|
+ if ($gradeRange) {
|
|
|
+ $query->whereBetween('grade', $gradeRange);
|
|
|
+ Log::info('知识点按学段筛选', [
|
|
|
+ 'stage' => $stage,
|
|
|
+ 'grade_range' => $gradeRange,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $knowledgePoints = $query->orderBy('kp_code')
|
|
|
->get()
|
|
|
->toArray();
|
|
|
|