KnowledgeServiceApi.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Http\Client\PendingRequest;
  4. use Illuminate\Http\Client\RequestException;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Http;
  8. class KnowledgeServiceApi
  9. {
  10. public function __construct(
  11. protected string $baseUrl = '',
  12. protected int $timeout = 10,
  13. protected int $cacheTtl = 300,
  14. ) {
  15. $this->baseUrl = rtrim($this->baseUrl ?: config('knowledge.base_url'), '/');
  16. $this->timeout = $this->timeout ?: (int) config('knowledge.timeout', 10);
  17. $this->cacheTtl = $this->cacheTtl ?: (int) config('knowledge.cache_ttl', 300);
  18. }
  19. /**
  20. * Fetch and cache all knowledge points by iterating through paginated API responses.
  21. *
  22. * @return Collection<int, array<string, mixed>>
  23. */
  24. public function listKnowledgePoints(int $perPage = 200, array $filters = []): Collection
  25. {
  26. $cacheKey = sprintf(
  27. 'knowledge-points-all-%d-%s',
  28. $perPage,
  29. md5(json_encode($filters))
  30. );
  31. return Cache::remember(
  32. $cacheKey,
  33. now()->addSeconds($this->cacheTtl),
  34. function () use ($perPage, $filters): Collection {
  35. $page = 1;
  36. $results = collect();
  37. while (true) {
  38. $response = $this->paginateKnowledgePoints($page, $perPage, $filters);
  39. $chunk = collect($response['data'] ?? []);
  40. if ($chunk->isEmpty()) {
  41. break;
  42. }
  43. $results = $results->concat($chunk);
  44. $meta = $response['meta'] ?? [];
  45. $hasNext = $meta['has_next'] ?? ($page < ($meta['total_pages'] ?? $page));
  46. if (! $hasNext) {
  47. break;
  48. }
  49. $page++;
  50. }
  51. return $results->values();
  52. },
  53. );
  54. }
  55. /**
  56. * @return array{data: array<int, array<string, mixed>>, meta: array<string, mixed>}
  57. */
  58. public function paginateKnowledgePoints(int $page = 1, int $perPage = 50, array $filters = []): array
  59. {
  60. $query = array_filter([
  61. 'page' => $page,
  62. 'per_page' => $perPage,
  63. 'phase' => $filters['phase'] ?? null,
  64. 'category' => $filters['category'] ?? null,
  65. 'grade' => $filters['grade'] ?? null,
  66. 'search' => $filters['search'] ?? null,
  67. ], fn ($value) => filled($value));
  68. $response = $this->request('GET', '/knowledge-points', $query);
  69. if (is_array($response) && array_key_exists('data', $response)) {
  70. return [
  71. 'data' => $response['data'] ?? [],
  72. 'meta' => $response['meta'] ?? [],
  73. ];
  74. }
  75. return [
  76. 'data' => is_array($response) ? $response : [],
  77. 'meta' => [
  78. 'page' => $page,
  79. 'per_page' => $perPage,
  80. 'total' => is_array($response) ? count($response) : 0,
  81. 'total_pages' => 1,
  82. 'has_next' => false,
  83. 'has_prev' => $page > 1,
  84. ],
  85. ];
  86. }
  87. /**
  88. * @return array<string, mixed>
  89. */
  90. public function getKnowledgePointDetail(string $kpCode): array
  91. {
  92. return Cache::remember(
  93. key: "knowledge-point-detail-{$kpCode}",
  94. ttl: now()->addSeconds($this->cacheTtl),
  95. callback: fn () => $this->request('GET', "/graph/node/{$kpCode}"),
  96. );
  97. }
  98. /**
  99. * 获取包含上下游节点的完整图谱数据
  100. */
  101. public function getFullGraphData(string $kpCode): array
  102. {
  103. $data = $this->getKnowledgePointDetail($kpCode);
  104. if (empty($data)) {
  105. return $data;
  106. }
  107. $data['child_nodes'] = $this->findChildNodes($kpCode);
  108. $data['parent_nodes'] = [];
  109. if (!empty($data['parents']) && is_array($data['parents'])) {
  110. foreach ($data['parents'] as $parentCode) {
  111. $parentDetail = $this->getKnowledgePointDetail($parentCode);
  112. if ($parentDetail) {
  113. $data['parent_nodes'][] = $parentDetail;
  114. }
  115. }
  116. }
  117. return $data;
  118. }
  119. /**
  120. * 反向查找子节点(以当前节点为父节点的节点)
  121. */
  122. private function findChildNodes(string $kpCode): array
  123. {
  124. // 缓存键
  125. $cacheKey = "knowledge-point-children-{$kpCode}";
  126. return Cache::remember(
  127. key: $cacheKey,
  128. ttl: now()->addSeconds($this->cacheTtl),
  129. callback: function () use ($kpCode) {
  130. try {
  131. \Log::info("开始查找子节点: {$kpCode}");
  132. // 获取所有知识点
  133. $allPoints = $this->listKnowledgePoints();
  134. \Log::info("获取到知识点数量: " . $allPoints->count());
  135. // 查找以当前节点为父节点的知识点
  136. $children = $allPoints->filter(function ($point) use ($kpCode) {
  137. return in_array($kpCode, $point['parents'] ?? []);
  138. })->values()->toArray();
  139. \Log::info("找到子节点数量: " . count($children));
  140. return $children;
  141. } catch (\Exception $e) {
  142. \Log::error("查找子节点失败: " . $e->getMessage());
  143. \Log::error($e->getTraceAsString());
  144. return [];
  145. }
  146. }
  147. );
  148. }
  149. /**
  150. * @return Collection<int, array<string, mixed>>
  151. */
  152. public function listSkills(?string $kpCode = null, int $limit = 200): Collection
  153. {
  154. return Cache::remember(
  155. key: "knowledge-skills-{$kpCode}-{$limit}",
  156. ttl: now()->addSeconds($this->cacheTtl),
  157. callback: fn () => collect($this->request('GET', '/skills', array_filter([
  158. 'kp_code' => $kpCode,
  159. 'limit' => $limit,
  160. ]))),
  161. );
  162. }
  163. /**
  164. * Search knowledge points by keyword
  165. *
  166. * @return Collection<int, array<string, mixed>>
  167. */
  168. public function searchKnowledgePoints(string $keyword, int $limit = 50): Collection
  169. {
  170. // Ensure keyword is not empty
  171. $keyword = trim($keyword);
  172. if ($keyword === '') {
  173. return collect();
  174. }
  175. // Use query params array for proper encoding (no cache during debugging)
  176. try {
  177. $response = $this->request('GET', 'knowledge-points/search', [
  178. 'keyword' => $keyword,
  179. 'limit' => $limit,
  180. ]);
  181. } catch (\Exception $e) {
  182. // Log error but don't fail completely
  183. \Log::error('Search API error: ' . $e->getMessage());
  184. return collect();
  185. }
  186. return collect($response);
  187. }
  188. /**
  189. * @throws RequestException
  190. * @return mixed
  191. */
  192. protected function request(string $method, string $path, array $params = []): mixed
  193. {
  194. $response = $this->http()->send($method, ltrim($path, '/'), [
  195. 'query' => $params,
  196. ]);
  197. return $response->throw()->json();
  198. }
  199. protected function http(): PendingRequest
  200. {
  201. return Http::baseUrl($this->baseUrl)
  202. ->acceptJson()
  203. ->timeout($this->timeout)
  204. ->retry(2, 200)
  205. ->withHeaders([
  206. 'User-Agent' => 'Laravel/' . (app()->version()),
  207. 'Accept' => 'application/json',
  208. ]);
  209. }
  210. }