| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | namespace app\controller; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use app\model\{Article as MArticle, Category}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Article extends Base | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |     //列表页
 | 
					
						
							|  |  |  |     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'] ?? ''); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  |     //详情
 | 
					
						
							|  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  |         $this->data['article'] = $article; | 
					
						
							|  |  |  |         $this->data['category'] = $category; | 
					
						
							|  |  |  |         $this->data['categoryId'] = $category['id']; | 
					
						
							| 
									
										
										
										
											2020-12-04 15:55:52 +08:00
										 |  |  |         $this->templateDetailAssign($article, $category); | 
					
						
							| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  |         return $this->view($category['template_detail'] ?? ''); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 列表数据绑定
 | 
					
						
							|  |  |  |     private function templateAssign($category) | 
					
						
							| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |         $template = strtolower($category['template_list'] ?? ''); | 
					
						
							|  |  |  |         $TopCId = Category::firstGradeById($category['id']); | 
					
						
							|  |  |  |         if($TopCId == $category['id']) { | 
					
						
							|  |  |  |             $topCategory = $category; | 
					
						
							| 
									
										
										
										
											2020-11-26 10:28:53 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |             $topCategory = Category::getById($TopCId); | 
					
						
							| 
									
										
										
										
											2020-11-26 10:28:53 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-04 10:11:22 +08:00
										 |  |  |         $categoryChildren = Category::getChildrenByParentId($topCategory['id']); | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |         $this->data['topCategory'] = $topCategory; | 
					
						
							| 
									
										
										
										
											2020-12-04 10:11:22 +08:00
										 |  |  |         $this->data['categoryChildren'] = $categoryChildren; | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |         switch($template) { | 
					
						
							| 
									
										
										
										
											2020-12-04 10:11:22 +08:00
										 |  |  |             case 'products' : | 
					
						
							|  |  |  |                 $this->assignProducts($topCategory, $category, $categoryChildren); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2020-12-04 14:33:02 +08:00
										 |  |  |             case 'news_center' : | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |             case 'news' : | 
					
						
							| 
									
										
										
										
											2020-12-04 14:33:02 +08:00
										 |  |  |                 $this->assignNews($topCategory, $category, $categoryChildren); | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             default : | 
					
						
							|  |  |  |                 $this->data['items'] = MArticle::getListPageByCategory($category['id'], $category['number'] ? $category['number'] : 20); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 详情数据绑定
 | 
					
						
							|  |  |  |     private function templateDetailAssign($article, $category) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-04 15:55:52 +08:00
										 |  |  |         $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); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 14:33:02 +08:00
										 |  |  |     // 产品 - 展示当前分类和所有子类产品
 | 
					
						
							| 
									
										
										
										
											2020-12-04 10:11:22 +08:00
										 |  |  |     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']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-04 14:33:02 +08:00
										 |  |  |         $items = MArticle::getListPageByCategories($cateIds, $category['number'] ? $category['number'] : 20, $keyword); | 
					
						
							|  |  |  |         $items->appends(['category_id'=>$category['id']]); | 
					
						
							|  |  |  |         $this->data['items'] = $items; | 
					
						
							|  |  |  |         $this->data['keyword'] = $keyword; | 
					
						
							| 
									
										
										
										
											2020-12-04 10:11:22 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 新闻
 | 
					
						
							| 
									
										
										
										
											2020-12-04 14:33:02 +08:00
										 |  |  |     private function assignNews($topCategory, $category, $categoryChildren) | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-04 14:33:02 +08:00
										 |  |  |         if($topCategory['id'] == $category['id']) { | 
					
						
							|  |  |  |             // 新闻中心
 | 
					
						
							|  |  |  |             $cateList = []; | 
					
						
							|  |  |  |             $newsChildrenFlip = array_flip(Category::$CIdList['news_children']); | 
					
						
							|  |  |  |             foreach ($categoryChildren as $cate) { | 
					
						
							| 
									
										
										
										
											2020-12-07 18:45:22 +08:00
										 |  |  |                 $num = 3; | 
					
						
							|  |  |  |                 if($cate['id'] == Category::$CIdList['news_children']['dynamics']) { | 
					
						
							|  |  |  |                     $num = 4; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 $cate['items'] = MArticle::getLatestByCategory($cate['id'], $num, 1); | 
					
						
							| 
									
										
										
										
											2020-12-04 14:33:02 +08:00
										 |  |  |                 $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; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 15:55:52 +08:00
										 |  |  |     // 产品详情
 | 
					
						
							|  |  |  |     private function assignDetailForProduct($article, $topCategory) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cateIds[] = $article['category_id']; | 
					
						
							|  |  |  |         $currentCateId = input('param.source', 0); | 
					
						
							|  |  |  |         if($currentCateId == $topCategory['id']) { | 
					
						
							|  |  |  |             $categoryList = Category::getChildrenByParentId($topCategory['id']); | 
					
						
							|  |  |  |             foreach ($categoryList as $cate) { | 
					
						
							|  |  |  |                 $cateIds[] = $cate['id']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 15:33:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  | } |