where('delete_time', null) ->find(); if (!$newsCenterCategory) { return json([ 'code' => 200, 'msg' => 'success', 'list' => [], ]); } // 2. 获取新闻中心及其子分类的ID列表 $mainCategoryId = $newsCenterCategory['id']; // 查询子分类 $subCategories = ArticlesCategory::where('cid', $mainCategoryId) ->where('delete_time', null) ->column('id'); // 合并主分类和子分类ID $categoryIds = array_merge([$mainCategoryId], $subCategories); if (empty($categoryIds)) { return json([ 'code' => 200, 'msg' => 'success', 'list' => [], ]); } // 3. 查询符合条件的文章,按指定优先级排序,取最新4篇 $articles = Articles::published() ->whereIn('cate', $categoryIds) ->order([ 'recommend' => 'desc', 'publish_date' => 'desc', 'sort' => 'desc', 'id' => 'desc' ]) ->field([ 'id', 'title', 'content', 'publish_date', 'DAY(publish_date) AS `date`', 'DATE_FORMAT(publish_date, "%Y-%m") AS `month`', 'image', 'recommend' ]) ->limit(4) ->select(); // 4. 处理文章数据,去除HTML标签并生成desc字段 foreach ($articles as &$article) { // 使用PHP的strip_tags函数去除HTML标签,然后截取前100个字符 $article['desc'] = mb_substr(trim(strip_tags($article['content'])), 0, 100, 'UTF-8'); // 移除原始content字段,减少返回数据大小 unset($article['content']); } return json([ 'code' => 200, 'msg' => 'success', 'list' => $articles, ]); } catch (\Exception $e) { // 打印完整错误信息到日志 $errorMsg = '错误信息:' . $e->getMessage() . ' | 错误行号:' . $e->getLine() . ' | 执行SQL:' . Articles::getLastSql(); trace('新闻中心文章查询失败: ' . $errorMsg, 'error'); return json([ 'code' => 500, 'msg' => '新闻中心文章查询失败,请稍后重试', 'list' => [] ]); } } /** * 游客文章阅读量函数 * @return Json */ public function articleViews(): Json { $id = Request::param('id'); if (!$id) { return json([ 'code' => 400, 'msg' => '没有文章id', 'list' => [] ]); } try { $article = Articles::where('id', $id)->where('delete_time', null)->find(); if (!$article) { return json([ 'code' => 404, 'msg' => '文章不存在', 'list' => [] ]); } $article->inc('views'); $article->save(); return json([ 'code' => 200, 'msg' => '阅读量更新成功', 'data' => [ 'views' => $article->views ] ]); } catch (\Exception $e) { trace('阅读量更新失败: ' . $e->getMessage(), 'error'); return json([ 'code' => 500, 'msg' => '阅读量更新失败,请稍后重试', 'list' => [] ]); } } /** * 游客文章点赞函数 * @return Json */ public function articleLikes(): Json { $id = Request::param('id'); if (!$id) { return json([ 'code' => 400, 'msg' => '没有文章id', 'list' => [] ]); } try { $article = Articles::where('id', $id)->where('delete_time', null)->find(); if (!$article) { return json([ 'code' => 404, 'msg' => '文章不存在', 'list' => [] ]); } $article->inc('likes'); $article->save(); return json([ 'code' => 200, 'msg' => '点赞成功', 'data' => [ 'likes' => $article->likes ] ]); } catch (\Exception $e) { trace('点赞失败: ' . $e->getMessage(), 'error'); return json([ 'code' => 500, 'msg' => '点赞失败,请稍后重试', 'list' => [] ]); } } /** * 游客文章取消点赞函数 * @return Json */ public function articleUnlikes(): Json { $id = Request::param('id'); if (!$id) { return json([ 'code' => 400, 'msg' => '没有文章id', 'list' => [] ]); } try { $article = Articles::where('id', $id)->where('delete_time', null)->find(); if (!$article) { return json([ 'code' => 404, 'msg' => '文章不存在', 'list' => [] ]); } if ($article->likes <= 0) { return json([ 'code' => 400, 'msg' => '文章点赞数为0,不能取消点赞', 'list' => [] ]); } $article->dec('likes'); $article->save(); return json([ 'code' => 200, 'msg' => '取消点赞成功', 'data' => [ 'likes' => $article->likes ] ]); } catch (\Exception $e) { trace('取消点赞失败: ' . $e->getMessage(), 'error'); return json([ 'code' => 500, 'msg' => '取消点赞失败,请稍后重试', 'list' => [] ]); } } }