增加program详情页
This commit is contained in:
@@ -94,38 +94,48 @@ class Article extends Base
|
||||
|
||||
// 获取相关文章(同分类的其他文章)
|
||||
$relatedArticles = Db::table('yz_article')
|
||||
->where('cate', $article['cate'])
|
||||
->where('id', '<>', $id)
|
||||
->where('delete_time', null)
|
||||
->where('status', '<>', 3)
|
||||
->order('id DESC')
|
||||
->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
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// 获取评论列表
|
||||
// $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);
|
||||
|
||||
// 非 AJAX 请求返回视图
|
||||
View::assign([
|
||||
'article' => $article,
|
||||
'cateName' => $cateName,
|
||||
'prevArticle' => $prevArticle,
|
||||
'nextArticle' => $nextArticle,
|
||||
'relatedArticles' => $relatedArticles,
|
||||
// 'comments' => $comments
|
||||
'relatedArticles' => $relatedArticles
|
||||
]);
|
||||
|
||||
return View::fetch();
|
||||
|
||||
return view('detail');
|
||||
}
|
||||
|
||||
// 文章点赞
|
||||
@@ -229,4 +239,37 @@ class Article extends Base
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文章访问次数
|
||||
*/
|
||||
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()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user