input('from', 0); $articlesId = []; if (!empty($request->input('anthology'))) { //子节点 $node = ArticleCollection::where('article_id', $request->input('id')) ->where('collect_id', $request->input('anthology'))->first(); if ($node) { $nodeList = ArticleCollection::where('collect_id', $request->input('anthology')) ->where('id', '>=', (int)$node->id) ->orderBy('id') ->skip($request->input('from', 0)) ->get(); $result = []; $count = 0; foreach ($nodeList as $curr) { if ($count > 0 && $curr->level <= $node->level) { break; } $result[] = $curr; } foreach ($result as $key => $value) { $articlesId[] = $value->article_id; } } } else { $articlesId[] = $request->input('id'); } $total = count($articlesId); $channels = explode(',', $request->input('channels')); $output = []; for ($i = $pageCurrent; $i < $pageCurrent + $pageSize; $i++) { if ($i >= $total) { break; } $curr = $articlesId[$i]; foreach ($channels as $channel) { # code... $article = $this->fetch($curr, $channel); if ($article === false) { Log::error('fetch fail'); } else { # code... $content = $article['html']; if (!empty($request->input('key'))) { if (strpos($content, $request->input('key')) !== false) { $output[] = $article; } } } } } return $this->ok([ 'rows' => $output, 'page' => [ 'size' => $pageSize, 'current' => $pageCurrent, 'total' => $total ], ]); } private function fetch($articleId, $channel, $token = null) { try { $api = config('mint.server.api.bamboo'); $basicUrl = $api . '/v2/article/'; $url = $basicUrl . $articleId;; $urlParam = [ 'mode' => 'read', 'format' => 'text', 'channel' => $channel, ]; Log::debug('http request', ['url' => $url, 'param' => $urlParam]); if ($token) { $response = Http::withToken($this->option('token'))->get($url, $urlParam); } else { $response = Http::get($url, $urlParam); } if ($response->failed()) { Log::error('http request error' . $response->json('message')); return false; } if (!$response->json('ok')) { return false; } $article = $response->json('data'); return $article; } catch (\Throwable $th) { // 处理请求过程中抛出的异常 Log::error('fetch', ['error' => $th]); return false; } } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } }