KnowledgePointDetails.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Livewire\Integrations;
  3. use Livewire\Component;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. class KnowledgePointDetails extends Component
  7. {
  8. public $selectedNode = null;
  9. public $nodeDetails = null;
  10. public $isLoading = false;
  11. public $activeTab = 'overview'; // 'overview', 'children', 'skills'
  12. protected $listeners = [
  13. 'nodeDetailsLoaded' => 'handleNodeDetailsLoaded',
  14. 'nodeDetailsCleared' => 'handleNodeDetailsCleared',
  15. 'kpSelected' => 'handleKpSelected',
  16. ];
  17. public function handleNodeDetailsLoaded($details)
  18. {
  19. $this->nodeDetails = $details;
  20. $this->selectedNode = $details;
  21. $this->activeTab = 'overview';
  22. }
  23. public function handleNodeDetailsCleared()
  24. {
  25. $this->nodeDetails = null;
  26. $this->selectedNode = null;
  27. }
  28. public function handleKpSelected($kpCode)
  29. {
  30. $this->selectedNode = $kpCode;
  31. $this->loadNodeDetails($kpCode);
  32. }
  33. private function loadNodeDetails($kpCode)
  34. {
  35. $this->isLoading = true;
  36. try {
  37. // 从知识图谱服务获取节点详情
  38. $knowledgeApiBase = config('services.knowledge_api.base_url', 'http://localhost:5011');
  39. // 获取节点基本信息
  40. $detailResponse = Http::timeout(10)
  41. ->get($knowledgeApiBase . "/knowledge-points/{$kpCode}");
  42. if ($detailResponse->successful()) {
  43. $detail = $detailResponse->json();
  44. // 获取上游和下游节点(一级子知识点)
  45. $upstreamResponse = Http::timeout(10)
  46. ->get($knowledgeApiBase . "/knowledge-points/{$kpCode}/upstream", ['levels' => 1]);
  47. $downstreamResponse = Http::timeout(10)
  48. ->get($knowledgeApiBase . "/knowledge-points/{$kpCode}/downstream", ['levels' => 1]);
  49. $detail['upstream_nodes'] = $upstreamResponse->successful() ? $upstreamResponse->json()['nodes'] ?? [] : [];
  50. $detail['downstream_nodes'] = $downstreamResponse->successful() ? $downstreamResponse->json()['nodes'] ?? [] : [];
  51. // 获取题目统计数据
  52. $questionApiBase = config('services.question_bank_api.base_url', 'http://localhost:5015');
  53. $statsResponse = Http::timeout(10)
  54. ->get($questionApiBase . "/api/questions/statistics", ['kp_code' => $kpCode]);
  55. if ($statsResponse->successful()) {
  56. $detail['question_stats'] = $statsResponse->json();
  57. }
  58. $this->nodeDetails = $detail;
  59. $this->activeTab = 'overview';
  60. }
  61. } catch (\Exception $e) {
  62. // 错误处理
  63. Log::error('加载节点详情失败', [
  64. 'kp_code' => $kpCode,
  65. 'error' => $e->getMessage()
  66. ]);
  67. }
  68. $this->isLoading = false;
  69. }
  70. public function setActiveTab($tab)
  71. {
  72. $this->activeTab = $tab;
  73. }
  74. public function getTotalQuestionsProperty()
  75. {
  76. if (!$this->nodeDetails) return 0;
  77. return $this->nodeDetails['question_stats']['total_questions'] ?? 0;
  78. }
  79. public function getDirectQuestionsProperty()
  80. {
  81. if (!$this->nodeDetails) return 0;
  82. return $this->nodeDetails['question_stats']['direct_questions'] ?? 0;
  83. }
  84. public function getChildrenQuestionsProperty()
  85. {
  86. if (!$this->nodeDetails) return 0;
  87. return $this->nodeDetails['question_stats']['children_questions'] ?? 0;
  88. }
  89. public function getSkillsQuestionsProperty()
  90. {
  91. if (!$this->nodeDetails) return 0;
  92. return $this->nodeDetails['question_stats']['skills_total_questions'] ?? 0;
  93. }
  94. public function getSkillsCountProperty()
  95. {
  96. if (!$this->nodeDetails) return 0;
  97. return $this->nodeDetails['question_stats']['skills_count'] ?? 0;
  98. }
  99. public function getChildrenNodesProperty()
  100. {
  101. if (!$this->nodeDetails) return [];
  102. return array_merge(
  103. $this->nodeDetails['upstream_nodes'] ?? [],
  104. $this->nodeDetails['downstream_nodes'] ?? []
  105. );
  106. }
  107. public function render()
  108. {
  109. return view('livewire.integrations.knowledge-point-details');
  110. }
  111. }