2
0

ArticleFtsController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. Log::debug('http request', ['url' => $url, 'param' => $urlParam]);
  94. if ($token) {
  95. $response = Http::withToken($this->option('token'))->get($url, $urlParam);
  96. } else {
  97. $response = Http::get($url, $urlParam);
  98. }
  99. if ($response->failed()) {
  100. Log::error('http request error' . $response->json('message'));
  101. return false;
  102. }
  103. if (!$response->json('ok')) {
  104. return false;
  105. }
  106. $article = $response->json('data');
  107. return $article;
  108. } catch (\Throwable $th) {
  109. // 处理请求过程中抛出的异常
  110. Log::error('fetch', ['error' => $th]);
  111. return false;
  112. }
  113. }
  114. /**
  115. * Store a newly created resource in storage.
  116. *
  117. * @param \Illuminate\Http\Request $request
  118. * @return \Illuminate\Http\Response
  119. */
  120. public function store(Request $request)
  121. {
  122. //
  123. }
  124. /**
  125. * Display the specified resource.
  126. *
  127. * @param int $id
  128. * @return \Illuminate\Http\Response
  129. */
  130. public function show($id)
  131. {
  132. //
  133. }
  134. /**
  135. * Update the specified resource in storage.
  136. *
  137. * @param \Illuminate\Http\Request $request
  138. * @param int $id
  139. * @return \Illuminate\Http\Response
  140. */
  141. public function update(Request $request, $id)
  142. {
  143. //
  144. }
  145. /**
  146. * Remove the specified resource from storage.
  147. *
  148. * @param int $id
  149. * @return \Illuminate\Http\Response
  150. */
  151. public function destroy($id)
  152. {
  153. //
  154. }
  155. }