ArticleFtsController.php 4.7 KB

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