chaoyu/app/controller/Article.php

67 lines
2.7 KiB
PHP

<?php
namespace app\controller;
use app\model\{Article as MArticle, Category};
class Article extends Base
{
//详情
public function detail($id=0)
{
if($id <= 0){
return $this->error('错误页面');
}
$article = MArticle::getById($id);
if(empty($article)){
return $this->error('无此文章');
}
MArticle::updateById($id, ['views' => $article['views'] + 1]);
$category = Category::getById($article['category_id']);
$prev = MArticle::getPrevArticleByIdAndCategoryId($id, $article['category_id']);
$next = MArticle::getNextArticleByIdAndCategoryId($id, $article['category_id']);
$keywords = $article['seo_keywords'] ? $article['seo_keywords'] : $this->system['seo_keywords'];
$description = $article['seo_description'] ? $article['seo_description'] : $this->system['seo_description'];
$this->setSeo($article['title'], $keywords, $description);
$this->data['article'] = $article;
$this->data['category'] = $category;
$this->data['categoryId'] = $category['id'];
$this->data['prev'] = $prev;
$this->data['next'] = $next;
return $this->view($category['template_detail'] ?? '');
}
//列表页
public function index()
{
$categoryId = input('param.category_id/d', 0);
if($categoryId <= 0){
return $this->error('错误页面');
}
$category = Category::getById($categoryId);
if(empty($category)){
return $this->error('错误页面');
}
$childCategory = Category::getChildrenByParentId($categoryId);
$description = $category['description'] ? $category['description'] : $this->system['seo_description'];
$this->setSeo($category['title'], $this->system['seo_keywords'], $description);
$childList = [];
$defaultList = [];
// 若存在下级子栏目则优先分组显示子栏目内容
if(!empty($childCategory)) {
foreach ($childCategory as $child) {
$childList[] = [
'category' => $child,
'items' => MArticle::getListPageByCategory($child['id'], $child['number'] ? $child['number'] : 20),
];
}
} else {
$defaultList = MArticle::getListPageByCategory($categoryId, $category['number'] ? $category['number'] : 20);
}
$this->data['items'] = $defaultList;
$this->data['childList'] = $childList;
$this->data['category'] = $category;
$this->data['categoryId'] = $categoryId;
$this->data['childCategory'] = $childCategory;
return $this->view($category['template_list'] ?? '');
}
}