KnowledgePointDetails.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $detail['question_stats'] = app(\App\Services\QuestionServiceApi::class)
  52. ->getStatistics(['kp_code' => $kpCode]);
  53. $this->nodeDetails = $detail;
  54. $this->activeTab = 'overview';
  55. }
  56. } catch (\Exception $e) {
  57. // 错误处理
  58. Log::error('加载节点详情失败', [
  59. 'kp_code' => $kpCode,
  60. 'error' => $e->getMessage()
  61. ]);
  62. }
  63. $this->isLoading = false;
  64. }
  65. public function setActiveTab($tab)
  66. {
  67. $this->activeTab = $tab;
  68. }
  69. public function getTotalQuestionsProperty()
  70. {
  71. if (!$this->nodeDetails) return 0;
  72. return $this->nodeDetails['question_stats']['total_questions'] ?? 0;
  73. }
  74. public function getDirectQuestionsProperty()
  75. {
  76. if (!$this->nodeDetails) return 0;
  77. return $this->nodeDetails['question_stats']['direct_questions'] ?? 0;
  78. }
  79. public function getChildrenQuestionsProperty()
  80. {
  81. if (!$this->nodeDetails) return 0;
  82. return $this->nodeDetails['question_stats']['children_questions'] ?? 0;
  83. }
  84. public function getSkillsQuestionsProperty()
  85. {
  86. if (!$this->nodeDetails) return 0;
  87. return $this->nodeDetails['question_stats']['skills_total_questions'] ?? 0;
  88. }
  89. public function getSkillsCountProperty()
  90. {
  91. if (!$this->nodeDetails) return 0;
  92. return $this->nodeDetails['question_stats']['skills_count'] ?? 0;
  93. }
  94. public function getChildrenNodesProperty()
  95. {
  96. if (!$this->nodeDetails) return [];
  97. return array_merge(
  98. $this->nodeDetails['upstream_nodes'] ?? [],
  99. $this->nodeDetails['downstream_nodes'] ?? []
  100. );
  101. }
  102. public function render()
  103. {
  104. return view('livewire.integrations.knowledge-point-details');
  105. }
  106. }