KnowledgePointsListComponent.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Livewire\Integrations;
  3. use Livewire\Component;
  4. use App\Services\KnowledgeServiceApi;
  5. use Illuminate\Support\Str;
  6. class KnowledgePointsListComponent extends Component
  7. {
  8. public $selectedKpCode = null;
  9. public $search = '';
  10. public $phaseFilter = null;
  11. public $categoryFilter = null;
  12. public $knowledgePoints = [];
  13. public $filteredPoints = [];
  14. public $isLoading = false;
  15. protected $listeners = [
  16. 'selectKp' => 'selectKp',
  17. 'clearSelection' => 'clearSelection',
  18. ];
  19. public function mount()
  20. {
  21. $this->loadKnowledgePoints();
  22. }
  23. public function loadKnowledgePoints()
  24. {
  25. $this->isLoading = true;
  26. try {
  27. $service = app(KnowledgeServiceApi::class);
  28. $points = $service->listKnowledgePoints(perPage: 200);
  29. $this->knowledgePoints = $points->toArray();
  30. $this->applyFilters();
  31. } catch (\Exception $e) {
  32. \Log::error('加载知识点列表失败', ['error' => $e->getMessage()]);
  33. $this->dispatch('error', message: '加载知识点列表失败');
  34. }
  35. $this->isLoading = false;
  36. }
  37. public function updatedSearch()
  38. {
  39. $this->applyFilters();
  40. }
  41. public function updatedPhaseFilter()
  42. {
  43. $this->applyFilters();
  44. }
  45. public function updatedCategoryFilter()
  46. {
  47. $this->applyFilters();
  48. }
  49. public function applyFilters()
  50. {
  51. $filtered = collect($this->knowledgePoints);
  52. // 按学段筛选
  53. if ($this->phaseFilter) {
  54. $filtered = $filtered->where('phase', $this->phaseFilter);
  55. }
  56. // 按类别筛选
  57. if ($this->categoryFilter) {
  58. $filtered = $filtered->where('category', $this->categoryFilter);
  59. }
  60. // 按搜索词筛选
  61. if ($this->search) {
  62. $searchTerm = Str::lower($this->search);
  63. $filtered = $filtered->filter(function ($point) use ($searchTerm) {
  64. return Str::contains(Str::lower($point['cn_name'] ?? ''), $searchTerm)
  65. || Str::contains(Str::lower($point['kp_code'] ?? ''), $searchTerm)
  66. || Str::contains(Str::lower($point['description'] ?? ''), $searchTerm);
  67. });
  68. }
  69. $this->filteredPoints = $filtered->values()->toArray();
  70. }
  71. public function selectKp($kpCode)
  72. {
  73. $this->selectedKpCode = $kpCode;
  74. $this->dispatch('kpSelected', kpCode: $kpCode);
  75. }
  76. public function clearSelection()
  77. {
  78. $this->selectedKpCode = null;
  79. $this->dispatch('clearGraphSelection');
  80. }
  81. public function getFilterOptionsProperty()
  82. {
  83. $phases = collect($this->knowledgePoints)
  84. ->pluck('phase')
  85. ->filter()
  86. ->unique()
  87. ->sort()
  88. ->values()
  89. ->toArray();
  90. $categories = collect($this->knowledgePoints)
  91. ->pluck('category')
  92. ->filter()
  93. ->unique()
  94. ->sort()
  95. ->values()
  96. ->toArray();
  97. return [
  98. 'phases' => $phases,
  99. 'categories' => $categories,
  100. ];
  101. }
  102. public function render()
  103. {
  104. return view('livewire.integrations.knowledge-points-list-component');
  105. }
  106. }