From c17d3b62e7733d10420d3d7a121fa24a3a61b4e5 Mon Sep 17 00:00:00 2001 From: zwesy Date: Thu, 3 Dec 2020 15:33:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E4=BA=A7=E5=93=81?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E6=95=B0=E6=8D=AE=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Article.php | 99 +++++++++++++++++++++++++------------- app/model/Article.php | 60 ++++++++++++++++++++--- view/article/product.html | 38 ++++++++++++++- view/article/products.html | 29 ++++++++++- 4 files changed, 182 insertions(+), 44 deletions(-) diff --git a/app/controller/Article.php b/app/controller/Article.php index d18d26a..4235f55 100644 --- a/app/controller/Article.php +++ b/app/controller/Article.php @@ -5,6 +5,26 @@ 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) { @@ -17,51 +37,62 @@ class Article extends Base } 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']); + $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['prev'] = $prev; - $this->data['next'] = $next; + $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'] ?? ''); } - //列表页 - public function index() + + // 列表数据绑定 + private function templateAssign($category) { - $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), - ]; - } + $template = strtolower($category['template_list'] ?? ''); + $TopCId = Category::firstGradeById($category['id']); + if($TopCId == $category['id']) { + $topCategory = $category; } else { - $defaultList = MArticle::getListPageByCategory($categoryId, $category['number'] ? $category['number'] : 20); + $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); } - $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'] ?? ''); } + + // 详情数据绑定 + private function templateDetailAssign($article, $category) + { + + } + + + // 新闻 + private function assignNews($topCategory, $category) + { + + + } + + + + } \ No newline at end of file diff --git a/app/model/Article.php b/app/model/Article.php index d5d52cc..194eae1 100644 --- a/app/model/Article.php +++ b/app/model/Article.php @@ -29,23 +29,67 @@ class Article extends Base ->select() ->toArray(); } - //根据文章ID和栏目ID获取下一篇文章 - public static function getNextArticleByIdAndCategoryId($id, $categoryId) + + /** + * 根据文章ID和栏目ID获取下一篇文章 + * @param int $id 当前文章ID + * @param int $categoryId 当前文章栏目ID + * @param bool $orderSort 是否以“sort”字段排序 + * @param int $currentSort 当前文章的排序号 + * @param bool $sortDesc 是否以sort字段倒叙排列 + * @return array + */ + public static function getNextArticleByIdAndCategoryId($id, $categoryId, $orderSort = false, $currentSort = 0, $sortDesc = false) { - return self::where('id','<',$id) + $whereMap = []; + if($orderSort && $currentSort > 0) { + if($sortDesc) { + $whereMap[] = ['sort', '<', $currentSort]; + $order = ['sort'=> 'desc']; + } else { + $whereMap[] = ['sort', '>', $currentSort]; + $order = ['sort'=> 'asc']; + } + } else { + $whereMap[] = ['id', '>', $id]; + $order = ['id'=> 'asc']; + } + return self::where($whereMap) ->where('category_id', $categoryId) ->where('status', 1) - ->order('sort desc') + ->order($order) ->findOrEmpty() ->toArray(); } - //根据文章ID和栏目ID获取上一篇文章 - public static function getPrevArticleByIdAndCategoryId($id, $categoryId) + + /** + * 根据文章ID和栏目ID获取上一篇文章 + * @param int $id 当前文章ID + * @param int $categoryId 当前文章栏目ID + * @param bool $orderSort 是否以“sort”字段排序 + * @param int $currentSort 当前文章的排序号 + * @param bool $sortDesc 是否以sort字段倒叙排列 + * @return array + */ + public static function getPrevArticleByIdAndCategoryId($id, $categoryId, $orderSort = false, $currentSort = 0, $sortDesc = false) { - return self::where('id','>',$id) + $whereMap = []; + if($orderSort && $currentSort > 0) { + if($sortDesc) { + $whereMap[] = ['sort', '>', $currentSort]; + $order = ['sort'=> 'asc']; + } else { + $whereMap[] = ['sort', '<', $currentSort]; + $order = ['sort'=> 'desc']; + } + } else { + $whereMap[] = ['id', '<', $id]; + $order = ['id'=> 'desc']; + } + return self::where($whereMap) ->where('category_id',$categoryId) ->where('status', 1) - ->order('sort asc') + ->order($order) ->findOrEmpty() ->toArray(); } diff --git a/view/article/product.html b/view/article/product.html index a49a9eb..15f397c 100644 --- a/view/article/product.html +++ b/view/article/product.html @@ -1,2 +1,38 @@ {layout name="layout" /} -新闻详情 \ No newline at end of file + + +
+
+
+ {$topCategory.title ?? ''} +

{:nl2br($topCategory.description ?? '')}

+
+
+
+ + +
+
+
+
+ {$article.title} +

{$article.create_time|date="Y/m/d"}

+
+
+ {:nl2br($article.content)} +
+
+

+ {if isset($prev) && count($prev) >0} + 上一篇 + {/if} + {if (isset($prev) && count($prev) >0) && (isset($next) && count($next) >0)} / {/if} + {if isset($next) && count($next) >0} + 下一篇 + {/if} +

+ 返回 +
+
+
+
\ No newline at end of file diff --git a/view/article/products.html b/view/article/products.html index f2f57ff..b839f4d 100644 --- a/view/article/products.html +++ b/view/article/products.html @@ -1,3 +1,30 @@ {layout name="layout" /} -新闻列表 \ No newline at end of file + +
+
+
+ {$category.title ?? ''} +

{:nl2br($category.description ?? '')}

+
+
+
+ +
+
+
+ {if isset($items)} +
+ +
+
+ {$items->render()|raw} +
+ {/if} +
+
+
\ No newline at end of file