| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  | <?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); | 
					
						
							| 
									
										
										
										
											2020-11-26 10:28:53 +08:00
										 |  |  |         $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; | 
					
						
							| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  |         $this->data['category'] = $category; | 
					
						
							|  |  |  |         $this->data['categoryId'] = $categoryId; | 
					
						
							| 
									
										
										
										
											2020-11-26 10:28:53 +08:00
										 |  |  |         $this->data['childCategory'] = $childCategory; | 
					
						
							| 
									
										
										
										
											2020-11-25 09:07:06 +08:00
										 |  |  |         return $this->view($category['template_list'] ?? ''); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |