160 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			160 lines
		
	
	
		
			6.3 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']);
 | |
|         $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->templateDetailAssign($article, $category);
 | |
|         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);
 | |
|         }
 | |
|         $categoryChildren = Category::getChildrenByParentId($topCategory['id']);
 | |
|         $this->data['topCategory'] = $topCategory;
 | |
|         $this->data['categoryChildren'] = $categoryChildren;
 | |
|         switch($template) {
 | |
|             case 'products' :
 | |
|                 $this->assignProducts($topCategory, $category, $categoryChildren);
 | |
|                 break;
 | |
|             case 'news_center' :
 | |
|             case 'news' :
 | |
|                 $this->assignNews($topCategory, $category, $categoryChildren);
 | |
|                 break;
 | |
|             default :
 | |
|                 $this->data['items'] = MArticle::getListPageByCategory($category['id'], $category['number'] ? $category['number'] : 20);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // 详情数据绑定
 | |
|     private function templateDetailAssign($article, $category)
 | |
|     {
 | |
|         $template = strtolower($category['template_detail'] ?? '');
 | |
|         $TopCId = Category::firstGradeById($category['id']);
 | |
|         if($TopCId == $category['id']) {
 | |
|             $topCategory = $category;
 | |
|         } else {
 | |
|             $topCategory = Category::getById($TopCId);
 | |
|         }
 | |
|         $this->data['topCategory'] = $topCategory;
 | |
|         switch ($template) {
 | |
|             case 'product':
 | |
|                 $this->assignDetailForProduct($article, $topCategory);
 | |
|                 break;
 | |
|             default :
 | |
|                 $this->data['prev'] = MArticle::getPrevArticleByIdAndCategories($article['id'], [$article['category_id']], true, $article['sort'], true);
 | |
|                 $this->data['next'] = MArticle::getNextArticleByIdAndCategories($article['id'], [$article['category_id']], true, $article['sort'], true);
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
|     // 产品 - 展示当前分类和所有子类产品
 | |
|     private function assignProducts($topCategory, $category, $categoryChildren)
 | |
|     {
 | |
|         $keyword = input('param.keyword', '');
 | |
|         $cateIds[] = $category['id'];
 | |
|         if($topCategory['id'] == $category['id']) {
 | |
|             $children = $categoryChildren;
 | |
|         } else {
 | |
|             $children = Category::getChildrenByParentId($category['id']);
 | |
|         }
 | |
|         foreach ($children as $child) {
 | |
|             $cateIds[] = $child['id'];
 | |
|         }
 | |
|         $items = MArticle::getListPageByCategories($cateIds, $category['number'] ? $category['number'] : 20, $keyword);
 | |
|         $items->appends(['category_id'=>$category['id']]);
 | |
|         $this->data['items'] = $items;
 | |
|         $this->data['keyword'] = $keyword;
 | |
|     }
 | |
| 
 | |
|     // 新闻
 | |
|     private function assignNews($topCategory, $category, $categoryChildren)
 | |
|     {
 | |
|         if($topCategory['id'] == $category['id']) {
 | |
|             // 新闻中心
 | |
|             $cateList = [];
 | |
|             $newsChildrenFlip = array_flip(Category::$CIdList['news_children']);
 | |
|             foreach ($categoryChildren as $cate) {
 | |
|                 $num = 3;
 | |
|                 if($cate['id'] == Category::$CIdList['news_children']['dynamics']) {
 | |
|                     $num = 4;
 | |
|                 }
 | |
|                 $cate['items'] = MArticle::getLatestByCategory($cate['id'], $num, 1);
 | |
|                 $cateList[$newsChildrenFlip[$cate['id']]] = $cate;
 | |
|             }
 | |
|             $this->data['cateList'] = $cateList;
 | |
|         } else {
 | |
|             // 新闻子栏目
 | |
|             $keyword = input('param.keyword', '');
 | |
|             $this->data['items'] = MArticle::getListPageByCategory($category['id'], $category['number'] ? $category['number'] : 20, $keyword);
 | |
|             $this->data['keyword'] = $keyword;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // 产品详情
 | |
|     private function assignDetailForProduct($article, $topCategory)
 | |
|     {
 | |
|         $cateIds[] = $article['category_id'];
 | |
|         $currentCateId = input('param.source', 0);
 | |
| 		$categoryList = Category::getChildrenByParentId($topCategory['id']);
 | |
|         if($currentCateId == $topCategory['id']) {
 | |
|             foreach ($categoryList as $cate) {
 | |
|                 $cateIds[] = $cate['id'];
 | |
|             }
 | |
|         }
 | |
| 		$this->data['categoryChildren'] = $categoryList;
 | |
|         $this->data['prev'] = MArticle::getPrevArticleByIdAndCategories($article['id'], $cateIds, true, $article['sort'], true);
 | |
|         $this->data['next'] = MArticle::getNextArticleByIdAndCategories($article['id'], $cateIds, true, $article['sort'], true);
 | |
|         $this->data['currentCateId'] = $currentCateId;
 | |
|     }
 | |
| 
 | |
| 
 | |
| } |