From 9b40f0792d7b23e2e53fdb36418a17a0a1761807 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Thu, 15 May 2025 18:07:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BA=A7=E5=93=81=E5=88=86?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/index/controller/Product.php | 97 +++++++++++++++- app/index/view/product/category.bak.html | 71 ++++++++++++ app/index/view/product/category.html | 105 ++++++------------ app/index/view/product/product_category.html | 59 ---------- ...duct_subcategory.html => subcategory.html} | 1 + 5 files changed, 200 insertions(+), 133 deletions(-) create mode 100644 app/index/view/product/category.bak.html delete mode 100644 app/index/view/product/product_category.html rename app/index/view/product/{product_subcategory.html => subcategory.html} (96%) diff --git a/app/index/controller/Product.php b/app/index/controller/Product.php index 3a9625ca..52a8205d 100644 --- a/app/index/controller/Product.php +++ b/app/index/controller/Product.php @@ -22,12 +22,100 @@ use think\helper\Arr; */ class Product extends Common { - // 产品分类 + // 产品分类 - 查看顶层分类 public function category() { // 参数 $param = request()->param(['id']); + // 获取分类及产品信息 + $categorys_data = ProductCategoryModel::field(['id', 'pid', 'name', 'path', 'level']) + ->language($this->lang_id) + ->displayed(true) + ->children($param['id']) + ->order(['pid' => 'asc', 'sort' => 'asc', 'id' => 'desc']) + ->select() + ->toArray(); + + $list = []; + if (!empty($categorys_data)) { + // 分组分类 + $list = array_filter($categorys_data, fn($it) => $it['level'] == 2); + foreach ($list as &$it) { + $it['children'] = array_column(array_filter($categorys_data, fn($v) => in_array($it['id'], explode(',', $v['path']))), 'id'); + } + unset($it); + + // 获取分类下的产品信息 + if (!empty($list)) { + $product_model = new ProductModel; + $sql = $product_model->field([ + 'id', + 'category_id', + 'spu', + 'name', + 'cover_image', + 'is_new', + '(' . $list[0]['id'] . ')' => 'group_mark' + ]) + ->byCategory(data_get($list[0], 'children')) + ->language($this->lang_id) + ->enabled(true) + ->onSale(true) + ->onShelves(true) + ->append(['p' => $list[0]['id']]) + ->order(['sort' => 'asc', 'id' => 'desc']) + ->limit(5) + ->buildSql(); + $query = \think\facade\Db::table("($sql) as a"); + foreach ($list as $it) { + $query = $query->union(function($query) use($product_model, $it) { + $query->model($product_model) + ->name($product_model->getName()) + ->field([ + 'id', + 'category_id', + 'spu', + 'name', + 'cover_image', + 'is_new', + '(' . $it['id'] . ')' => 'group_mark' + ]) + ->byCategory($it['children']) + ->language($this->lang_id) + ->enabled(true) + ->onSale(true) + ->onShelves(true) + ->order(['sort' => 'asc', 'id' => 'desc']) + ->limit(5); + }); + } + $pros = $query->select(); + if (!empty($pros)) { + $pros_map = []; + foreach ($pros as $pro) { + $pros_map[$pro['group_mark']][] = $pro; + } + foreach ($list as &$it) { + if (isset($pros_map[$it['id']])) { + $it['products'] = $pros_map[$it['id']]; + } + unset($it['children']); + } + unset($it); + } + } + } + View::assign('list', $list); + + return View::fetch('category'); + } + // 产品分类 - 查看子类 + public function subcategory() + { + // 参数 + $param = request()->param(['id']); + $focus_image = []; // 获取产品分类页焦点横幅 $banner = SysBannerModel::with(['items' => function($query) { @@ -60,6 +148,7 @@ class Product extends Common ->child($param['id'], true) ->order(['sort' => 'asc', 'id' => 'desc']) ->select(); + if (!$categorys_data->isEmpty()) { if ($categorys_data->count() > 1) { // 当分类数不只一个时,当前分类下有子分类,移除当前分类,只输出子分类 @@ -70,8 +159,8 @@ class Product extends Common $categorys_data = $categorys_data->toArray(); $products = ProductModel::field([ - 'id', - 'category_id', + 'id', + 'category_id', 'spu', 'name', 'short_name', @@ -151,7 +240,7 @@ class Product extends Common } View::assign('categorys_data', $categorys_data); - return View::fetch('category'); + return View::fetch('subcategory'); } /** diff --git a/app/index/view/product/category.bak.html b/app/index/view/product/category.bak.html new file mode 100644 index 00000000..6363511d --- /dev/null +++ b/app/index/view/product/category.bak.html @@ -0,0 +1,71 @@ +{extend name="public/base" /} +{block name="style"} + +{/block} +{block name="main"} +
+
+
-
-
-
-
-
-
-