diff --git a/app/index/controller/Product.php b/app/index/controller/Product.php index 3a9625ca..dc2270b8 100644 --- a/app/index/controller/Product.php +++ b/app/index/controller/Product.php @@ -13,6 +13,7 @@ use app\index\model\ProductRelatedModel; use app\index\model\ProductSkuAttrModel; use app\index\model\ProductSkuModel; use app\index\model\SysBannerModel; +use app\index\model\SysBannerProdCateMappingModel; use app\index\validate\ProductInquiryValidate; use think\facade\View; use think\helper\Arr; @@ -22,21 +23,25 @@ use think\helper\Arr; */ class Product extends Common { - // 产品分类 - public function category() + // 获取分类页的banner图焦点横幅 + private function getCategoryBanner($category_id) { - // 参数 - $param = request()->param(['id']); - $focus_image = []; // 获取产品分类页焦点横幅 - $banner = SysBannerModel::with(['items' => function($query) { + $banner = SysBannerModel::with(['items' => function($query) use($category_id) { $query->withoutField([ 'status', 'created_at', 'updated_at', 'deleted_at' ]) + ->whereExists(function($q) use($category_id) { + $r = $q->getModel()->getTable(); + $m = new SysBannerProdCateMappingModel; + $q->model($m)->name($m->getName()) + ->where("banner_item_id = $r.id") + ->where('product_category_id' , '=', $category_id); + }) ->where('status', '=', 1) ->order(['sort' => 'asc', 'id' => 'desc']); }]) @@ -51,7 +56,109 @@ class Product extends Common } $focus_image = data_get($banner_map, 'BANNER_6808abd813d78')?->items->toArray(); } - View::assign('focus_image', $focus_image); + return $focus_image; + } + // 产品分类 - 查看顶层分类 + public function category() + { + // 参数 + $param = request()->param(['id']); + + // 获取产品分类页焦点横幅 + View::assign('focus_image', $this->getCategoryBanner($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 $k => &$it) { + unset($it['children']); + if (!isset($pros_map[$it['id']])) { + unset($list[$k]); + continue; + } + $it['products'] = $pros_map[$it['id']]; + } + unset($it); + } + } + } + View::assign('list', $list); + + return View::fetch('category'); + } + // 产品分类 - 查看子类 + public function subcategory() + { + // 参数 + $param = request()->param(['id']); + + // 获取产品分类页焦点横幅 + View::assign('focus_image', $this->getCategoryBanner($param['id'])); // 获取分类及产品信息 $categorys_data = ProductCategoryModel::field(['id', 'name', 'level']) @@ -60,6 +167,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 +178,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 +259,7 @@ class Product extends Common } View::assign('categorys_data', $categorys_data); - return View::fetch('category'); + return View::fetch('subcategory'); } /** @@ -399,7 +507,7 @@ class Product extends Common $m = $map[$pro['category_id']]; $pro_map[$m['path']][] = $pro; } - + // 获取二级分类下的产品信息 foreach ($categorys as $val) { if ($val['level'] != 2) { @@ -407,7 +515,7 @@ class Product extends Common } foreach ($pro_map as $k => $pro) { - if (in_array($val['id'], explode(',', $k))) { + if (in_array($val['id'], explode(',', strval($k)))) { $newpros[] = [ 'category' => $val, 'products' => $pro, diff --git a/app/index/model/SysBannerProdCateMappingModel.php b/app/index/model/SysBannerProdCateMappingModel.php new file mode 100644 index 00000000..54050b5c --- /dev/null +++ b/app/index/model/SysBannerProdCateMappingModel.php @@ -0,0 +1,15 @@ + -{/block} -{block name="main"} -
- {notempty name="focus_image"} -
- {volist name="focus_image" id="fimg"} - - {/volist} -
- {/notempty} - -
- {notempty name="categorys_data"} - {volist name="categorys_data" id="vo"} -
- {$vo.name} - {eq name="vo.level" value="2"} - 查看更多 - {/eq} -
- {notempty name="vo.products"} - - {/notempty} - {/volist} - {/notempty} -
-
-{/block} -{block name="script"} - +{extend name="public/base" /} +{block name="style"} + +{/block} +{block name="main"} +
+ + {notempty name="focus_image"} +
+ {volist name="focus_image" id="fi"} + + + + {/volist} +
+ {/notempty} + +
+ {volist name="list" id="vo"} +
+ + {notempty name="vo.products"} + + {/notempty} +
+ {/volist} +
+
+{/block} +{block name="script"} + {/block} \ No newline at end of file diff --git a/app/index/view/product/product_category.html b/app/index/view/product/product_category.html deleted file mode 100644 index cbff7f93..00000000 --- a/app/index/view/product/product_category.html +++ /dev/null @@ -1,59 +0,0 @@ -{extend name="public/base" /} -{block name="style"} - -{block name="main"} -
- -
- -
-
-{/block} \ No newline at end of file diff --git a/app/index/view/product/product_subcategory.html b/app/index/view/product/product_subcategory.html deleted file mode 100644 index 681ac0f1..00000000 --- a/app/index/view/product/product_subcategory.html +++ /dev/null @@ -1,34 +0,0 @@ -{extend name="public/base" /} -{block name="style"} - -{block name="main"} -
- - - - - -
-

Power Strip

- -
-
-{/block} \ No newline at end of file diff --git a/app/index/view/product/subcategory.html b/app/index/view/product/subcategory.html new file mode 100644 index 00000000..6e878fb6 --- /dev/null +++ b/app/index/view/product/subcategory.html @@ -0,0 +1,86 @@ +{extend name="public/base" /} +{block name="style"} + +{/block} +{block name="main"} +
+ {notempty name="focus_image"} +
+ {volist name="focus_image" id="fi"} + + + + {/volist} +
+ {/notempty} + +
+ {notempty name="categorys_data"} + {volist name="categorys_data" id="vo"} +
+ {$vo.name} +
+ {notempty name="vo.products"} + + {/notempty} + {/volist} + {/notempty} +
+
+{/block} +{block name="script"} + +{/block} \ No newline at end of file diff --git a/app/index/view/public/header.html b/app/index/view/public/header.html index 59c2385c..5b5c9d95 100644 --- a/app/index/view/public/header.html +++ b/app/index/view/public/header.html @@ -25,16 +25,15 @@ {volist name="header_categorys" id="vo" key="idx"} -