From 14ff19ea930a3204f15b365c9580950e5e0dbd2d Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Sat, 12 Apr 2025 14:34:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=87=E7=AB=A0=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/index/controller/Article.php | 50 ++++++++++++++++++ app/index/model/ArticleCategoryModel.php | 25 +++++++++ app/index/model/ArticleModel.php | 6 +-- app/index/model/SysBannerItemModel.php | 12 +++++ app/index/route/route.php | 2 +- app/index/view/article/index.html | 67 ++++++++---------------- public/static/index/css/category.css | 43 ++++----------- 7 files changed, 123 insertions(+), 82 deletions(-) diff --git a/app/index/controller/Article.php b/app/index/controller/Article.php index e080e8db..6d7d85b7 100644 --- a/app/index/controller/Article.php +++ b/app/index/controller/Article.php @@ -3,6 +3,10 @@ declare (strict_types = 1); namespace app\index\controller; +use app\admin\controller\v1\BannerItem; +use app\index\model\ArticleCategoryModel; +use app\index\model\ArticleModel; +use app\index\model\SysBannerItemModel; use think\facade\View; class Article extends Common @@ -12,6 +16,52 @@ class Article extends Common */ public function index() { + // 获取上级分类 + $param = request()->param([ + 'pid', + 'cid', + 'page/d' => 1, + 'size/d' => 12, + ]); + + // 获取banner焦点图 + $banner = SysBannerItemModel::hasWhere('banner', [ + 'language_id' => $this->lang_id, + 'unique_label' => 'BANNER_67f9fc71e27db' + ]) + ->type('image') + ->visible(['id', 'title', 'image', 'link']) + ->find(); + View::assign('banner', $banner); + + // 获取分类列表 + $categorys = ArticleCategoryModel::field([ + 'id', + 'pid', + 'name' + ]) + ->language($this->lang_id) + ->parent($param['pid']??null) + ->isShow(true) + ->order(['sort' => 'asc', 'id' => 'desc']) + ->select(); + View::assign('categorys', $categorys); + + // 获取文章列表 + if (!$categorys->isEmpty()) { + $articles = ArticleModel::field([ + 'id', + 'title', + 'desc', + 'image' + ]) + ->category($param['cid']??$categorys[0]['id']) + ->paginate([ + 'list_rows' => $param['size'], + 'page' => $param['page'], + ]); + } + View::assign('articles', $articles??[]); return View::fetch('index'); } diff --git a/app/index/model/ArticleCategoryModel.php b/app/index/model/ArticleCategoryModel.php index 0f9d4da4..195494da 100644 --- a/app/index/model/ArticleCategoryModel.php +++ b/app/index/model/ArticleCategoryModel.php @@ -16,4 +16,29 @@ class ArticleCategoryModel extends ArticleCategoryBaseModel use SoftDelete; // 软删除字段 protected $deleteTime = 'deleted_at'; + + // 关联文章 + public function article() + { + return $this->hasMany(ArticleModel::class, 'category_id', 'id'); + } + + // 所属语言范围查询 + public function scopeLanguage($query, $language) + { + $query->where('language_id', '=', $language); + } + + // 所属上级分类范围查询 + public function scopeParent($query, $parent) + { + if (is_null($parent)) return; + $query->where('pid', '=', $parent); + } + + // 是否显示状态范围查询 + public function scopeIsShow($query, bool $is_show) + { + $query->where('is_show', '=', (int)$is_show); + } } diff --git a/app/index/model/ArticleModel.php b/app/index/model/ArticleModel.php index 4d6937aa..8903d51f 100644 --- a/app/index/model/ArticleModel.php +++ b/app/index/model/ArticleModel.php @@ -29,9 +29,9 @@ class ArticleModel extends ArticleBaseModel return $query->where('recommend', '=', (int)$stat); } - // 启用状态范围查询 - public function scopeEnabled($query, bool $stat = true) + // 文章分类范围查询 + public function scopeCategory($query, $category) { - return $query->where('enabled', '=', (int)$stat); + return $query->where('category_id', '=', $category); } } diff --git a/app/index/model/SysBannerItemModel.php b/app/index/model/SysBannerItemModel.php index 860da226..2948d528 100644 --- a/app/index/model/SysBannerItemModel.php +++ b/app/index/model/SysBannerItemModel.php @@ -17,6 +17,18 @@ class SysBannerItemModel extends SysBannerItemBaseModel // 软删除字段 protected $deleteTime = 'deleted_at'; + // 关联分类 + public function banner() + { + return $this->hasOne(SysBannerModel::class, 'id', 'banner_id'); + } + + // 类型范围查询 + public function scopeType($query, $type) + { + $query->where('type', '=', $type); + } + // 启用状态范围查询 public function scopeEnabled($query) { diff --git a/app/index/route/route.php b/app/index/route/route.php index 3acba5ae..32da2902 100644 --- a/app/index/route/route.php +++ b/app/index/route/route.php @@ -25,7 +25,7 @@ Route::group('product', function() { // 文章相关路由 Route::group('article', function() { // 文章列表页 - Route::get('index/:id', 'Article/index'); + Route::get('index/:pid', 'Article/index'); // 文章详情页 Route::get('detail/:id', 'Article/detail'); // 文章搜索页 diff --git a/app/index/view/article/index.html b/app/index/view/article/index.html index 5a5122dd..75145378 100644 --- a/app/index/view/article/index.html +++ b/app/index/view/article/index.html @@ -4,67 +4,44 @@ {/block} {block name="main"}
-
+