276 lines
7.8 KiB
PHP
276 lines
7.8 KiB
PHP
<?php
|
||
/**
|
||
* 文章控制器
|
||
*/
|
||
namespace app\index\controller;
|
||
use app\index\controller\Base;
|
||
use think\facade\Db;
|
||
use think\facade\View;
|
||
use think\facade\Request;
|
||
|
||
class Article extends Base
|
||
{
|
||
// 文章列表页
|
||
public function list()
|
||
{
|
||
// 获取分类ID
|
||
$cateId = Request::param('cate/d', 0);
|
||
|
||
// 构建查询条件
|
||
$where = [
|
||
['delete_time', '=', null],
|
||
['status', '=', 1]
|
||
];
|
||
|
||
if ($cateId > 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')
|
||
->alias('a')
|
||
->join('yz_article_category c', 'a.cate = c.id')
|
||
->where('a.cate', $article['cate'])
|
||
->where('a.id', '<>', $id)
|
||
->where('a.delete_time', null)
|
||
->where('a.status', '=', 2)
|
||
->field([
|
||
'a.id',
|
||
'a.title',
|
||
'a.desc',
|
||
'IF(a.image IS NULL OR a.image = "", c.image, a.image) as image'
|
||
])
|
||
->order('a.id DESC')
|
||
->limit(3)
|
||
->select()
|
||
->toArray();
|
||
|
||
// 如果是 AJAX 请求,返回 JSON 数据
|
||
if (Request::isAjax()) {
|
||
return json([
|
||
'code' => 1,
|
||
'msg' => '获取成功',
|
||
'data' => [
|
||
'article' => $article,
|
||
'cateName' => $cateName,
|
||
'prevArticle' => $prevArticle,
|
||
'nextArticle' => $nextArticle,
|
||
'relatedArticles' => $relatedArticles
|
||
]
|
||
]);
|
||
}
|
||
|
||
// 非 AJAX 请求返回视图
|
||
View::assign([
|
||
'article' => $article,
|
||
'cateName' => $cateName,
|
||
'prevArticle' => $prevArticle,
|
||
'nextArticle' => $nextArticle,
|
||
'relatedArticles' => $relatedArticles
|
||
]);
|
||
|
||
return view('detail');
|
||
}
|
||
|
||
// 文章点赞
|
||
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
|
||
]
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 更新文章访问次数
|
||
*/
|
||
public function updateViews()
|
||
{
|
||
if (!Request::isPost()) {
|
||
return json(['code' => 0, 'msg' => '非法请求']);
|
||
}
|
||
|
||
$id = Request::post('id');
|
||
if (!$id) {
|
||
return json(['code' => 0, 'msg' => '参数错误']);
|
||
}
|
||
|
||
try {
|
||
// 更新访问次数
|
||
$article = Db::table('yz_article')->where('id', $id)->find();
|
||
if (!$article) {
|
||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||
}
|
||
|
||
// 更新访问次数
|
||
Db::table('yz_article')->where('id', $id)->inc('views')->update();
|
||
|
||
// 获取更新后的访问次数
|
||
$newViews = Db::table('yz_article')->where('id', $id)->value('views');
|
||
|
||
return json(['code' => 1, 'msg' => '更新成功', 'data' => ['views' => $newViews]]);
|
||
} catch (\Exception $e) {
|
||
return json(['code' => 0, 'msg' => '更新失败:' . $e->getMessage()]);
|
||
}
|
||
}
|
||
}
|