0) { $where[] = ['cate', '=', $cateId]; } // 获取文章列表 $articles = Db::table('yz_article') ->where($where) ->order('id DESC') ->paginate([ 'list_rows' => 10, 'query' => Request::instance()->param() ]); // 获取分类信息 $category = null; if ($cateId > 0) { $category = Db::table('yz_article_category') ->where('id', $cateId) ->where('delete_time', null) ->where('status', 3) ->find(); } // 获取所有分类 $categories = Db::table('yz_article_category') ->where('delete_time', null) ->where('status', 3) ->select() ->toArray(); // 将变量传递给视图 View::assign([ 'articles' => $articles, 'category' => $category, 'categories' => $categories ]); return view('list'); } // 文章详情页 public function detail() { $id = Request::param('id/d', 0); $article = Db::table('yz_article')->where('id', $id)->find(); if (!$article) { return json(['code' => 0, 'msg' => '文章不存在或已被删除']); } // 获取分类名称 $cateName = Db::table('yz_article_category') ->where('id', $article['cate']) ->value('name'); // 获取上一篇和下一篇文章 $prevArticle = Db::table('yz_article') ->where('id', '<', $id) ->where('delete_time', null) ->where('status', '<>', 3) ->order('id DESC') ->find(); $nextArticle = Db::table('yz_article') ->where('id', '>', $id) ->where('delete_time', null) ->where('status', '<>', 3) ->order('id ASC') ->find(); // 获取相关文章(同分类的其他文章) $relatedArticles = Db::table('yz_article') ->where('cate', $article['cate']) ->where('id', '<>', $id) ->where('delete_time', null) ->where('status', '<>', 3) ->order('id DESC') ->limit(3) ->select() ->toArray(); // 获取评论列表 // $comments = Db::table('yz_article_comment') // ->where('article_id', $id) // ->where('delete_time', null) // ->order('id DESC') // ->select() // ->toArray(); // 更新浏览量(使用Cookie和IP双重验证) $ip = Request::ip(); $cookieKey = 'article_view_' . $id; $viewCookie = Request::cookie($cookieKey); View::assign([ 'article' => $article, 'cateName' => $cateName, 'prevArticle' => $prevArticle, 'nextArticle' => $nextArticle, 'relatedArticles' => $relatedArticles, // 'comments' => $comments ]); return View::fetch(); } // 文章点赞 public function like() { if (!Request::isAjax()) { return json(['code' => 0, 'msg' => '非法请求']); } $id = Request::param('id/d', 0); // 更新点赞数 $result = Db::table('yz_article') ->where('id', $id) ->where('delete_time', null) ->inc('likes', 1) ->update(); if ($result) { return json(['code' => 1, 'msg' => '点赞成功']); } else { return json(['code' => 0, 'msg' => '点赞失败']); } } // 提交评论 public function comment() { if (!Request::isAjax() || !Request::isPost()) { return json(['code' => 0, 'msg' => '非法请求']); } $articleId = Request::param('article_id/d', 0); $content = Request::param('content/s', ''); $parentId = Request::param('parent_id/d', 0); if (empty($content)) { return json(['code' => 0, 'msg' => '评论内容不能为空']); } // 检查文章是否存在 $article = Db::table('yz_article') ->where('id', $articleId) ->where('delete_time', null) ->where('status', 3) ->find(); if (!$article) { return json(['code' => 0, 'msg' => '文章不存在或已被删除']); } // 添加评论 // $data = [ // 'article_id' => $articleId, // 'content' => $content, // 'parent_id' => $parentId, // 'user_id' => $this->getUserId(), // 'user_name' => $this->getUserName(), // 'status' => 1, // 'create_time' => time() // ]; // $result = Db::table('yz_article_comment')->insert($data); // if ($result) { // return json(['code' => 1, 'msg' => '评论成功']); // } else { // return json(['code' => 0, 'msg' => '评论失败']); // } } // 获取当前用户ID(示例方法,实际应根据您的用户系统实现) private function getUserId() { // 这里应该返回当前登录用户的ID return 1; // 示例返回值 } // 获取当前用户名(示例方法,实际应根据您的用户系统实现) private function getUserName() { // 这里应该返回当前登录用户的用户名 return '游客'; // 示例返回值 } // 获取访问统计 public function viewStats() { $id = Request::param('id/d', 0); // 获取总访问量 $totalViews = Db::table('yz_article') ->where('id', $id) ->value('views'); return json([ 'code' => 1, 'data' => [ 'total' => $totalViews ] ]); } }