99 lines
3.1 KiB
PHP
99 lines
3.1 KiB
PHP
<?php
|
|
/*
|
|
* @Author: YwxApp <ywx@ywxapp.cn>
|
|
* @Date: 2026-08-10 15:31:07
|
|
* @LastEditors: YwxApp <ywx@ywxapp.cn>
|
|
* @LastEditTime: 2026-08-13 17:07:28
|
|
* @Description:
|
|
* @FilePath: \ywxapp_dev\addon\articles\controller\Article.php
|
|
* @CustomString: Copyright (c) 2026 YwxApp
|
|
*/
|
|
// +----------------------------------------------------------------------
|
|
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | 文章CMS前台文章详情 / 搜索 / 评论
|
|
// +----------------------------------------------------------------------
|
|
declare (strict_types = 1);
|
|
|
|
namespace addon\articles\controller;
|
|
|
|
use think\facade\Db;
|
|
use addon\articles\model\Article as ArticleModel;
|
|
use addon\articles\model\ArticleComment;
|
|
|
|
class Article extends ArticlesFrontend
|
|
{
|
|
public function read($id)
|
|
{
|
|
$article = ArticleModel::getDetail((int) $id);
|
|
if (!$article) {
|
|
$this->result->error('文章不存在或已删除');
|
|
}
|
|
// 浏览量 +1
|
|
ArticleModel::where('id', $id)->inc('view_count')->update();
|
|
|
|
$commentsPage = ArticleComment::getByArticle((int) $id, (int) input('page', 1), 10);
|
|
$comments = [
|
|
'data' => $commentsPage->items(),
|
|
'render' => $commentsPage->render(),
|
|
];
|
|
|
|
$this->view->assign([
|
|
'article' => $article,
|
|
'prev' => ArticleModel::prev($article->id, $article->cid),
|
|
'next' => ArticleModel::next($article->id, $article->cid),
|
|
'related' => ArticleModel::related($article->id, $article->cid, 5),
|
|
'hot' => ArticleModel::hot(10),
|
|
'comments'=> $comments,
|
|
'nav_active' => $article->cid,
|
|
]);
|
|
return $this->fetch('article/detail');
|
|
}
|
|
|
|
// 兼容 route: articles/article/detail/:id
|
|
public function detail($id)
|
|
{
|
|
return $this->read($id);
|
|
}
|
|
|
|
public function search()
|
|
{
|
|
$keyword = trim(input('keyword', ''));
|
|
$page = (int) input('page', 1);
|
|
$list = ArticleModel::listArticles(['keyword' => $keyword], $page, 10);
|
|
|
|
$this->view->assign([
|
|
'keyword' => $keyword,
|
|
'list' => $list->items(),
|
|
'total' => $list->total(),
|
|
'page' => $list->render(),
|
|
'nav_active' => '',
|
|
]);
|
|
return $this->fetch('search/index');
|
|
}
|
|
|
|
public function comment()
|
|
{
|
|
if (!$this->auth || !$this->auth->isLogin) {
|
|
$this->result->error('请先登录后再评论');
|
|
}
|
|
$aid = (int) input('aid', 0);
|
|
$content = trim(input('content', ''));
|
|
if (!$aid || !$content) {
|
|
$this->result->error('参数错误');
|
|
}
|
|
$uid = $this->auth->info['uid'] ?? 0;
|
|
ArticleComment::create([
|
|
'uid' => $uid,
|
|
'aid' => $aid,
|
|
'pid' => 0,
|
|
'content' => $content,
|
|
'status' => 1,
|
|
'create_at' => time(),
|
|
'update_at' => time(),
|
|
]);
|
|
ArticleModel::where('id', $aid)->inc('comment_count')->update();
|
|
$this->result->success('评论成功');
|
|
}
|
|
}
|