refactor: 产品分类
This commit is contained in:
@@ -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) {
|
||||
// 当分类数不只一个时,当前分类下有子分类,移除当前分类,只输出子分类
|
||||
@@ -151,7 +240,7 @@ class Product extends Common
|
||||
}
|
||||
View::assign('categorys_data', $categorys_data);
|
||||
|
||||
return View::fetch('category');
|
||||
return View::fetch('subcategory');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
71
app/index/view/product/category.bak.html
Normal file
71
app/index/view/product/category.bak.html
Normal file
@@ -0,0 +1,71 @@
|
||||
{extend name="public/base" /}
|
||||
{block name="style"}
|
||||
<link rel="stylesheet" href="__CSS__/category.css" type="text/css" />
|
||||
{/block}
|
||||
{block name="main"}
|
||||
<div class="orico_Page_products">
|
||||
{notempty name="focus_image"}
|
||||
<div class="focus_image">
|
||||
{volist name="focus_image" id="fimg"}
|
||||
<a {notempty name="fimg.link"}href="{$fimg.link}"{/notempty}><img src="{$fimg.image}" alt="" /></a>
|
||||
{/volist}
|
||||
</div>
|
||||
{/notempty}
|
||||
<!-- 首页主题内容 -->
|
||||
<div class="pageMain">
|
||||
{notempty name="categorys_data"}
|
||||
{volist name="categorys_data" id="vo"}
|
||||
<div class="ori-pd-title">
|
||||
<span>{$vo.name}</span>
|
||||
{eq name="vo.level" value="2"}
|
||||
<a href="{:url('product/category', ['id' => $vo.id])}">查看更多</a>
|
||||
{/eq}
|
||||
</div>
|
||||
{notempty name="vo.products"}
|
||||
<div class="ori-pd-list">
|
||||
{volist name="vo.products" id="vp"}
|
||||
<a class="oripditem" href="{:url('product/detail', ['id' => $vp.id])}">
|
||||
<div>
|
||||
{volist name="vp.sku" id="vs" key="vs_idx"}
|
||||
<img src="{$vs.main_image}" id="sku_image_{$vs.id}" class="prdimg {eq name='vs_idx' value='1'}prdimg-show{/eq}" />
|
||||
{/volist}
|
||||
</div>
|
||||
<div class="prdName">{$vp.name}</div>
|
||||
<div class="prddec">{$vp.spu}</div>
|
||||
{notempty name="vp.colors"}
|
||||
<div class="prd-colors">
|
||||
{volist name="vp.colors" id="vc" key="vc_idx"}
|
||||
<div class="prdolorit {eq name='vc_idx' value='1'}on{/eq}" data-sku_id="{$vc.sku_id}">
|
||||
{assign name="color_type" value=":rgb_or_image($vc.attr_value)" /}
|
||||
{eq name="color_type" value="IMAGE"}
|
||||
<img src="{$vc.attr_value}" />
|
||||
{elseif condition="$color_type == 'RGB'" /}
|
||||
<span class="rgb_hex" {:style(['background-color'=>$vc.attr_value])}></span>
|
||||
{/eq}
|
||||
</div>
|
||||
{/volist}
|
||||
</div>
|
||||
{/notempty}
|
||||
</a>
|
||||
{/volist}
|
||||
</div>
|
||||
{/notempty}
|
||||
{/volist}
|
||||
{/notempty}
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script type="text/javascript">
|
||||
$(function () {
|
||||
$('.prd-colors .prdolorit').click(function(event) {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
|
||||
var sku_id = $(this).data('sku_id');
|
||||
$('#sku_image_' + sku_id).addClass('prdimg-show').siblings().removeClass('prdimg-show');
|
||||
$(this).addClass('on').siblings().removeClass('on');
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{/block}
|
||||
@@ -1,71 +1,36 @@
|
||||
{extend name="public/base" /}
|
||||
{block name="style"}
|
||||
<link rel="stylesheet" href="__CSS__/category.css" type="text/css" />
|
||||
<link rel="stylesheet" href="__CSS__/product_category.css" type="text/css" />
|
||||
{/block}
|
||||
{block name="main"}
|
||||
<div class="orico_Page_products">
|
||||
{notempty name="focus_image"}
|
||||
<div class="focus_image">
|
||||
{volist name="focus_image" id="fimg"}
|
||||
<a {notempty name="fimg.link"}href="{$fimg.link}"{/notempty}><img src="{$fimg.image}" alt="" /></a>
|
||||
{/volist}
|
||||
</div>
|
||||
{/notempty}
|
||||
<!-- 首页主题内容 -->
|
||||
<div class="pageMain">
|
||||
{notempty name="categorys_data"}
|
||||
{volist name="categorys_data" id="vo"}
|
||||
<div class="ori-pd-title">
|
||||
<span>{$vo.name}</span>
|
||||
{eq name="vo.level" value="2"}
|
||||
<a href="{:url('product/category', ['id' => $vo.id])}">查看更多</a>
|
||||
{/eq}
|
||||
</div>
|
||||
{notempty name="vo.products"}
|
||||
<div class="ori-pd-list">
|
||||
{volist name="vo.products" id="vp"}
|
||||
<a class="oripditem" href="{:url('product/detail', ['id' => $vp.id])}">
|
||||
<div>
|
||||
{volist name="vp.sku" id="vs" key="vs_idx"}
|
||||
<img src="{$vs.main_image}" id="sku_image_{$vs.id}" class="prdimg {eq name='vs_idx' value='1'}prdimg-show{/eq}" />
|
||||
{/volist}
|
||||
</div>
|
||||
<div class="prdName">{$vp.name}</div>
|
||||
<div class="prddec">{$vp.spu}</div>
|
||||
{notempty name="vp.colors"}
|
||||
<div class="prd-colors">
|
||||
{volist name="vp.colors" id="vc" key="vc_idx"}
|
||||
<div class="prdolorit {eq name='vc_idx' value='1'}on{/eq}" data-sku_id="{$vc.sku_id}">
|
||||
{assign name="color_type" value=":rgb_or_image($vc.attr_value)" /}
|
||||
{eq name="color_type" value="IMAGE"}
|
||||
<img src="{$vc.attr_value}" />
|
||||
{elseif condition="$color_type == 'RGB'" /}
|
||||
<span class="rgb_hex" {:style(['background-color'=>$vc.attr_value])}></span>
|
||||
{/eq}
|
||||
</div>
|
||||
{/volist}
|
||||
</div>
|
||||
{/notempty}
|
||||
</a>
|
||||
{/volist}
|
||||
</div>
|
||||
{/notempty}
|
||||
{/volist}
|
||||
{/notempty}
|
||||
</div>
|
||||
<div class="orico_Page_category">
|
||||
<!--首页主题内容 -->
|
||||
<div class="pageMain">
|
||||
{volist name="list" id="vo"}
|
||||
<div class="cat">
|
||||
<div class="ori-pd-title">
|
||||
<div class="catname">{$vo.name}</div>
|
||||
<a class="catmore" href="{:url('product/subcategory', ['id' => $vo.id])}">
|
||||
<span class="tmore">{:lang('product_newpro.view_all')}</span>
|
||||
<img src="/static/index/images/more.png" class="catmoreImg" />
|
||||
</a>
|
||||
</div>
|
||||
<div class="ori-pd-list">
|
||||
{volist name="vo.products" id="pro"}
|
||||
<a class="oripditem" href="{:url('product/detail', ['id' => $pro.id])}">
|
||||
<div>
|
||||
<img src="{$pro.cover_image}" class="prdimg prdimg-show" />
|
||||
</div>
|
||||
<div class="prdName">{$pro.name}</div>
|
||||
<div class="prddec">{$pro.spu}</div>
|
||||
{eq name="pro.is_new" value="1"}
|
||||
<div class="newcp">New</div>
|
||||
{/eq}
|
||||
</a>
|
||||
{/volist}
|
||||
</div>
|
||||
</div>
|
||||
{/volist}
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script type="text/javascript">
|
||||
$(function () {
|
||||
$('.prd-colors .prdolorit').click(function(event) {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
|
||||
var sku_id = $(this).data('sku_id');
|
||||
$('#sku_image_' + sku_id).addClass('prdimg-show').siblings().removeClass('prdimg-show');
|
||||
$(this).addClass('on').siblings().removeClass('on');
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{/block}
|
||||
@@ -1,59 +0,0 @@
|
||||
{extend name="public/base" /}
|
||||
{block name="style"}
|
||||
<link rel="stylesheet" href="__CSS__/product_category.css" type="text/css" />
|
||||
{block name="main"}
|
||||
<div class="orico_Page_category">
|
||||
<!--首页主题内容 -->
|
||||
<div class="pageMain">
|
||||
<div class="cat">
|
||||
<div class="ori-pd-title">
|
||||
<div class="catname">Power Strip</div>
|
||||
<a class="catmore">
|
||||
<span class="tmore">View all</span>
|
||||
<img src="/static/index/images/more.png" class="catmoreImg" />
|
||||
</a>
|
||||
</div>
|
||||
<div class="ori-pd-list">
|
||||
<a class="oripditem">
|
||||
<div>
|
||||
<img src="/static/index/images/ORCIO-HSQ-02H-800-220.jpg" class="prdimg prdimg-show" />
|
||||
</div>
|
||||
<div class="prdName">ORICO 5 in 1 Travel Power Strip</div>
|
||||
<div class="prddec">ORICO-AP3-2A3U</div>
|
||||
<div class="newcp">New</div>
|
||||
</a>
|
||||
<a class="oripditem">
|
||||
<div>
|
||||
<img src="/static/index/images/ORCIO-HSQ-02H-800-220.jpg" class="prdimg prdimg-show" />
|
||||
</div>
|
||||
<div class="prdName">ORICO 5 in 1 Travel Power Strip</div>
|
||||
<div class="prddec">ORICO-AP3-2A3U</div>
|
||||
<div class="newcp">New</div>
|
||||
</a>
|
||||
<a class="oripditem">
|
||||
<div>
|
||||
<img src="/static/index/images/ORCIO-HSQ-02H-800-220.jpg" class="prdimg prdimg-show" />
|
||||
</div>
|
||||
<div class="prdName">ORICO 5 in 1 Travel Power Strip</div>
|
||||
<div class="prddec">ORICO-AP3-2A3U</div>
|
||||
</a>
|
||||
<a class="oripditem">
|
||||
<div>
|
||||
<img src="/static/index/images/ORCIO-HSQ-02H-800-220.jpg" class="prdimg prdimg-show" />
|
||||
</div>
|
||||
<div class="prdName">ORICO 5 in 1 Travel Power Strip</div>
|
||||
<div class="prddec">ORICO-AP3-2A3U</div>
|
||||
</a>
|
||||
<a class="oripditem">
|
||||
<div>
|
||||
<img src="/static/index/images/ORCIO-HSQ-02H-800-220.jpg" class="prdimg prdimg-show" />
|
||||
</div>
|
||||
<div class="prdName">ORICO 5 in 1 Travel Power Strip</div>
|
||||
<div class="prddec">ORICO-AP3-2A3U</div>
|
||||
<div class="newcp">New</div>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
@@ -1,6 +1,7 @@
|
||||
{extend name="public/base" /}
|
||||
{block name="style"}
|
||||
<link rel="stylesheet" href="__CSS__/product_subcategory.css" type="text/css" />
|
||||
{/block}
|
||||
{block name="main"}
|
||||
<div class="orico_Page_subcategory">
|
||||
<!--公共头部-->
|
||||
Reference in New Issue
Block a user