chaoyu/app/controller/Article.php

98 lines
3.2 KiB
PHP

<?php
namespace app\controller;
use app\model\{Article as MArticle, Category};
class Article extends Base
{
//列表页
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('错误页面');
}
$description = $category['description'] ? $category['description'] : $this->system['seo_description'];
$this->setSeo($category['title'], $this->system['seo_keywords'], $description);
$this->data['category'] = $category;
$this->data['categoryId'] = $categoryId;
$this->templateAssign($category);
return $this->view($category['template_list'] ?? '');
}
//详情
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']);
$TopCId = Category::firstGradeById($category['id']);
if($TopCId == $category['id']) {
$topCategory = $category;
} else {
$topCategory = Category::getById($TopCId);
}
$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['topCategory'] = $topCategory;
$this->data['prev'] = MArticle::getPrevArticleByIdAndCategoryId($id, $article['category_id'], true, $article['sort'], true);
$this->data['next'] = MArticle::getNextArticleByIdAndCategoryId($id, $article['category_id'], true, $article['sort'], true);
return $this->view($category['template_detail'] ?? '');
}
// 列表数据绑定
private function templateAssign($category)
{
$template = strtolower($category['template_list'] ?? '');
$TopCId = Category::firstGradeById($category['id']);
if($TopCId == $category['id']) {
$topCategory = $category;
} else {
$topCategory = Category::getById($TopCId);
}
$this->data['topCategory'] = $topCategory;
$this->data['categoryChildren'] = Category::getChildrenByParentId($topCategory['id']);
switch($template) {
case 'news' :
$this->assignNews($topCategory, $category);
break;
default :
$this->data['items'] = MArticle::getListPageByCategory($category['id'], $category['number'] ? $category['number'] : 20);
}
}
// 详情数据绑定
private function templateDetailAssign($article, $category)
{
}
// 新闻
private function assignNews($topCategory, $category)
{
}
}