2 Commits

Author SHA1 Message Date
a105d575b8 refactor: 调整新品页数据 2025-05-14 17:27:59 +08:00
d04f7b035a refactor: 数据迁移程序 2025-05-14 16:25:55 +08:00
3 changed files with 44 additions and 20 deletions

View File

@@ -51,7 +51,7 @@ class DataMigration extends Command
// $this->migrateProductRelated(); // $this->migrateProductRelated();
// 迁移产品购买链接 // 迁移产品购买链接
$this->migrateProductPurchaseLinks(); // $this->migrateProductPurchaseLinks();
// 迁移文章 // 迁移文章
// $this->migrateArticle([ // $this->migrateArticle([
@@ -174,18 +174,25 @@ class DataMigration extends Command
->select() ->select()
->toArray(); ->toArray();
$tree = array_to_tree($category, 0, 'pid', 1);
// 处理数据 // 处理数据
$this->handlerProductCategory(array_to_tree($category, 0, 'pid', 1), $tco_category_map); $this->handlerProductCategory($tree, $tco_category_map);
} }
private function handlerProductCategory($category, $map) { private function handlerProductCategory($category, $map, $path = []) {
foreach ($category as $val) { foreach ($category as $val) {
$key = sprintf("%s_%s", $val['id'], $val['country_code']); $key = sprintf("%s_%s", $val['id'], $val['country_code']);
if ($val['pid'] == 0) {
$path = [];
} else {
if (!in_array($val['pid'], $path)) $path[] = $val['pid'];
}
$item = [ $item = [
'id' => $val['id'], 'id' => $val['id'],
'language_id' => $val['country_code'] == 'ZH' ? 1 : 2, 'language_id' => $val['country_code'] == 'ZH' ? 1 : 2,
'unique_id' => uniqid('PRO_CATE_'), 'unique_id' => uniqid('PRO_CATE_'),
'pid' => $val['pid'], 'pid' => $val['pid'],
'name' => $val['name'], 'name' => $val['name'],
'path' => implode(',', $path),
'icon' => $val['icon'], 'icon' => $val['icon'],
'desc' => $val['description'], 'desc' => $val['description'],
'related_tco_category' => isset($map[$key]) ? implode(',', $map[$key]) : '', 'related_tco_category' => isset($map[$key]) ? implode(',', $map[$key]) : '',
@@ -195,7 +202,7 @@ class DataMigration extends Command
]; ];
Db::name('product_category')->insert($item); Db::name('product_category')->insert($item);
if (isset($val['children'])) { if (isset($val['children'])) {
$this->handlerProductCategory($val['children'], $map); $this->handlerProductCategory($val['children'], $map, $path);
} }
} }
} }

View File

@@ -376,24 +376,45 @@ class Product extends Common
->order(['sort' => 'asc', 'id' => 'desc']) ->order(['sort' => 'asc', 'id' => 'desc'])
->select(); ->select();
if (!$products->isEmpty()) { if (!$products->isEmpty()) {
// 按分类分组产品
$products_map = [];
foreach ($products as $product) {
$products_map[$product['category_id']][] = $product;
}
// 获取产品分类信息 // 获取产品分类信息
$categorys = ProductCategoryModel::field(['id', 'name']) $categorys = ProductCategoryModel::field(['id', 'name', 'path', 'level'])
->byPks(array_keys($products_map)) ->where(function($query) use($products) {
$query->where('id', 'IN', Arr::pluck($products, 'category_id'))->whereOr('level', '=', 2);
})
->language($this->lang_id) ->language($this->lang_id)
->displayed(true) ->displayed(true)
->order(['sort' => 'asc', 'id' => 'desc']) ->order(['sort' => 'asc', 'id' => 'asc'])
->select(); ->select();
if (!$categorys->isEmpty()) { if (!$categorys->isEmpty()) {
// 根据产品分类path信息分组
$map = [];
foreach ($categorys as $category) { foreach ($categorys as $category) {
$newpros[] = [ $map[$category['id']] = $category;
'category' => $category, }
'products' => $products_map[$category['id']] ?? [], $pro_map = [];
]; foreach ($products as $pro) {
if (!isset($map[$pro['category_id']])) {
continue;
}
$m = $map[$pro['category_id']];
$pro_map[$m['path']][] = $pro;
}
// 获取二级分类下的产品信息
foreach ($categorys as $val) {
if ($val['level'] != 2) {
continue;
}
foreach ($pro_map as $k => $pro) {
if (in_array($val['id'], explode(',', $k))) {
$newpros[] = [
'category' => $val,
'products' => $pro,
];
break;
}
}
} }
} }
} }

View File

@@ -16,10 +16,6 @@
<div class="cat"> <div class="cat">
<h1 class="ori-pd-title"> <h1 class="ori-pd-title">
<div class="catname">{$vo.category.name}</div> <div class="catname">{$vo.category.name}</div>
<a class="catmore">
<span class="tmore">{:lang('product_newpro.view_all')}</span>
<img src="/static/index/images/more.png" class="catmoreImg" />
</a>
</h1> </h1>
<div class="ori-pd-list"> <div class="ori-pd-list">
{volist name="vo.products" id="pro"} {volist name="vo.products" id="pro"}