From 426e583fc9286c64a4faed38df70cef24529ed5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E6=B3=BD=E7=BD=91?= <”357099073@qq.com“> Date: Sat, 10 May 2025 00:46:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E7=AB=A0=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E4=BB=A5=E5=8F=8A=E7=9B=B8=E5=85=B3=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/index/controller/Article.php | 232 + app/index/view/article/detail.php | 533 + app/index/view/component/foot.php | 3 + app/index/view/component/head.php | 14 + app/index/view/component/header-simple.php | 114 + app/index/view/component/main.php | 4 +- .../20240321_create_article_view_table.php | 17 + public/static/css/bootstrap.min.css | 10295 +++++++++------- public/static/layui/css/layui.css | 2 +- public/static/layui/layui.js | 2 +- .../temp/69170ce622adbb0032543cdbee52d3fd.php | 6 +- .../temp/9e370511bc8ad4f6efb5d7be433411c3.php | 49 + .../temp/d407556bb15ad99658fbd9b0fbe4c5e4.php | 707 ++ 13 files changed, 7846 insertions(+), 4132 deletions(-) create mode 100644 app/index/controller/Article.php create mode 100644 app/index/view/article/detail.php create mode 100644 app/index/view/component/foot.php create mode 100644 app/index/view/component/head.php create mode 100644 app/index/view/component/header-simple.php create mode 100644 database/migrations/20240321_create_article_view_table.php create mode 100644 runtime/index/temp/9e370511bc8ad4f6efb5d7be433411c3.php create mode 100644 runtime/index/temp/d407556bb15ad99658fbd9b0fbe4c5e4.php diff --git a/app/index/controller/Article.php b/app/index/controller/Article.php new file mode 100644 index 0000000..ac6e0ac --- /dev/null +++ b/app/index/controller/Article.php @@ -0,0 +1,232 @@ + 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 + ] + ]); + } +} diff --git a/app/index/view/article/detail.php b/app/index/view/article/detail.php new file mode 100644 index 0000000..551827f --- /dev/null +++ b/app/index/view/article/detail.php @@ -0,0 +1,533 @@ +{include file="component/head" /} +{include file="component/header-simple" /} +