ArticleFtsController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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->get('from',0);
  23. $articlesId = [];
  24. if(!empty($request->get('anthology'))){
  25. //子节点
  26. $node = ArticleCollection::where('article_id',$request->get('id'))
  27. ->where('collect_id',$request->get('anthology'))->first();
  28. if($node){
  29. $nodeList = ArticleCollection::where('collect_id',$request->get('anthology'))
  30. ->where('id','>=',(int)$node->id)
  31. ->orderBy('id')
  32. ->skip($request->get('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->get('id');
  48. }
  49. $total = count($articlesId);
  50. $channels = explode(',',$request->get('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->get('key'))){
  66. if(strpos($content,$request->get('key')) !== false){
  67. $output[] = $article;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. return $this->ok(['rows'=>$output,
  74. 'page'=>[
  75. 'size' => $pageSize,
  76. 'current' => $pageCurrent,
  77. 'total' => $total
  78. ],]);
  79. }
  80. private function fetch($articleId,$channel,$token=null){
  81. try {
  82. $api = config('mint.server.api.bamboo');
  83. $basicUrl = $api . '/v2/article/';
  84. $url = $basicUrl . $articleId;;
  85. $urlParam = [
  86. 'mode' => 'read',
  87. 'format' => 'text',
  88. 'channel' => $channel,
  89. ];
  90. Log::debug('http request',['url'=>$url,'param'=>$urlParam]);
  91. if($token){
  92. $response = Http::withToken($this->option('token'))->get($url,$urlParam);
  93. }else{
  94. $response = Http::get($url,$urlParam);
  95. }
  96. if($response->failed()){
  97. Log::error('http request error'.$response->json('message'));
  98. return false;
  99. }
  100. if(!$response->json('ok')){
  101. return false;
  102. }
  103. $article = $response->json('data');
  104. return $article;
  105. }catch (\Throwable $th) {
  106. // 处理请求过程中抛出的异常
  107. Log::error('fetch',['error'=>$th]);
  108. return false;
  109. }
  110. }
  111. /**
  112. * Store a newly created resource in storage.
  113. *
  114. * @param \Illuminate\Http\Request $request
  115. * @return \Illuminate\Http\Response
  116. */
  117. public function store(Request $request)
  118. {
  119. //
  120. }
  121. /**
  122. * Display the specified resource.
  123. *
  124. * @param int $id
  125. * @return \Illuminate\Http\Response
  126. */
  127. public function show($id)
  128. {
  129. //
  130. }
  131. /**
  132. * Update the specified resource in storage.
  133. *
  134. * @param \Illuminate\Http\Request $request
  135. * @param int $id
  136. * @return \Illuminate\Http\Response
  137. */
  138. public function update(Request $request, $id)
  139. {
  140. //
  141. }
  142. /**
  143. * Remove the specified resource from storage.
  144. *
  145. * @param int $id
  146. * @return \Illuminate\Http\Response
  147. */
  148. public function destroy($id)
  149. {
  150. //
  151. }
  152. }