ArticleFtsController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * 文章全文搜索
  4. */
  5. namespace App\Http\Controllers;
  6. use App\Models\Article;
  7. use App\Models\ArticleCollection;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use Illuminate\Support\Facades\Http;
  11. use Illuminate\Support\Facades\Log;
  12. class ArticleFtsController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. * http://127.0.0.1:8000/api/v2/article-fts?id=df6c6609-6fc1-42d0-9ef1-535ef3e702c9&anthology=697c9169-cb9d-4a60-8848-92745e467bab&channesl=7fea264d-7a26-40f8-bef7-bc95102760fb
  17. *
  18. * @return Response
  19. */
  20. public function index(Request $request)
  21. {
  22. //
  23. $pageSize = 10;
  24. $pageCurrent = $request->input('from', 0);
  25. $articlesId = [];
  26. if (! empty($request->input('anthology'))) {
  27. // 子节点
  28. $node = ArticleCollection::where('article_id', $request->input('id'))
  29. ->where('collect_id', $request->input('anthology'))->first();
  30. if ($node) {
  31. $nodeList = ArticleCollection::where('collect_id', $request->input('anthology'))
  32. ->where('id', '>=', (int) $node->id)
  33. ->orderBy('id')
  34. ->skip($request->input('from', 0))
  35. ->get();
  36. $result = [];
  37. $count = 0;
  38. foreach ($nodeList as $curr) {
  39. if ($count > 0 && $curr->level <= $node->level) {
  40. break;
  41. }
  42. $result[] = $curr;
  43. }
  44. foreach ($result as $key => $value) {
  45. $articlesId[] = $value->article_id;
  46. }
  47. }
  48. } else {
  49. $articlesId[] = $request->input('id');
  50. }
  51. $total = count($articlesId);
  52. $channels = explode(',', $request->input('channels'));
  53. $output = [];
  54. for ($i = $pageCurrent; $i < $pageCurrent + $pageSize; $i++) {
  55. if ($i >= $total) {
  56. break;
  57. }
  58. $curr = $articlesId[$i];
  59. foreach ($channels as $channel) {
  60. // code...
  61. $article = $this->fetch($curr, $channel);
  62. if ($article === false) {
  63. Log::error('fetch fail');
  64. } else {
  65. // code...
  66. $content = $article['html'];
  67. if (! empty($request->input('key'))) {
  68. if (strpos($content, $request->input('key')) !== false) {
  69. $output[] = $article;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. return $this->ok([
  76. 'rows' => $output,
  77. 'page' => [
  78. 'size' => $pageSize,
  79. 'current' => $pageCurrent,
  80. 'total' => $total,
  81. ],
  82. ]);
  83. }
  84. private function fetch($articleId, $channel, $token = null)
  85. {
  86. try {
  87. $api = config('mint.server.api.bamboo');
  88. $basicUrl = $api.'/v2/article/';
  89. $url = $basicUrl.$articleId;
  90. $urlParam = [
  91. 'mode' => 'read',
  92. 'format' => 'text',
  93. 'channel' => $channel,
  94. ];
  95. if ($token) {
  96. $response = Http::withToken($this->option('token'))->get($url, $urlParam);
  97. } else {
  98. $response = Http::get($url, $urlParam);
  99. }
  100. if ($response->failed()) {
  101. Log::error('http request error'.$response->json('message'));
  102. return false;
  103. }
  104. if (! $response->json('ok')) {
  105. return false;
  106. }
  107. $article = $response->json('data');
  108. return $article;
  109. } catch (\Throwable $th) {
  110. // 处理请求过程中抛出的异常
  111. Log::error('fetch', ['error' => $th]);
  112. return false;
  113. }
  114. }
  115. /**
  116. * Store a newly created resource in storage.
  117. *
  118. * @return Response
  119. */
  120. public function store(Request $request)
  121. {
  122. //
  123. }
  124. /**
  125. * Display the specified resource.
  126. *
  127. * @param int $id
  128. * @return Response
  129. */
  130. public function show($id)
  131. {
  132. //
  133. }
  134. /**
  135. * Update the specified resource in storage.
  136. *
  137. * @param int $id
  138. * @return Response
  139. */
  140. public function update(Request $request, $id)
  141. {
  142. //
  143. }
  144. /**
  145. * Remove the specified resource from storage.
  146. *
  147. * @param int $id
  148. * @return Response
  149. */
  150. public function destroy($id)
  151. {
  152. //
  153. }
  154. }