```
feat(decorate): 优化首页商品列表逻辑并移除缓存机制 - 移除商品列表缓存机制,提高数据实时性 - 重构首页商品ID处理逻辑,支持合并装修ID与全店商品ID - 新增allShelfGoodsIds方法获取所有可售商品ID - 添加mergeGoodsIdStrings方法合并多个商品ID字符串 - 优化buildHomeGoodsContextFromContent方法统一处理内容解析 - 增强首页商品上下文构建,支持多种数据源合并 ```
这个提交包含在:
@@ -118,34 +118,68 @@ class Decorate extends Model
|
||||
if ($keywords !== '') {
|
||||
return $params;
|
||||
}
|
||||
if (!empty($params['goods_ids'])) {
|
||||
return $params;
|
||||
}
|
||||
|
||||
$ctx = self::resolveHomeGoodsListContext($platform);
|
||||
$homeIds = trim((string) ($ctx['goods_ids'] ?? ''));
|
||||
$homeCat = (int) ($ctx['category_id'] ?? 0);
|
||||
if ($homeIds === '') {
|
||||
return $params;
|
||||
}
|
||||
|
||||
$categoryId = isset($params['category_id']) ? (int) $params['category_id'] : 0;
|
||||
$scoped = $categoryId > 0;
|
||||
|
||||
if (!$scoped) {
|
||||
$params['goods_ids'] = $homeIds;
|
||||
unset($params['category_id']);
|
||||
$isHomeScope = $categoryId <= 0 || ($homeCat > 0 && $categoryId === $homeCat);
|
||||
if (!$isHomeScope) {
|
||||
return $params;
|
||||
}
|
||||
|
||||
if ($homeCat > 0 && $categoryId === $homeCat) {
|
||||
$params['goods_ids'] = $homeIds;
|
||||
unset($params['category_id']);
|
||||
// 首页:请求里已有 goods_ids(装修 ids)时也合并全店上架/隐藏,避免只返回 2 个
|
||||
$mergedIds = self::mergeGoodsIdStrings(
|
||||
$params['goods_ids'] ?? '',
|
||||
$homeIds,
|
||||
self::allShelfGoodsIds()
|
||||
);
|
||||
if ($mergedIds === '') {
|
||||
return $params;
|
||||
}
|
||||
|
||||
$params['goods_ids'] = $mergedIds;
|
||||
unset($params['category_id']);
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 商城当前可售(上架+隐藏)商品,按 sort/weigh 排序
|
||||
*/
|
||||
public static function allShelfGoodsIds()
|
||||
{
|
||||
$ids = Goods::where('status', 'in', ['up', 'hidden'])
|
||||
->order('weigh asc, id desc')
|
||||
->column('id');
|
||||
return self::normalizeGoodsIdsString(implode(',', $ids));
|
||||
}
|
||||
|
||||
protected static function mergeGoodsIdStrings(...$parts)
|
||||
{
|
||||
$idMap = [];
|
||||
foreach ($parts as $part) {
|
||||
$part = trim((string) $part);
|
||||
if ($part === '') {
|
||||
continue;
|
||||
}
|
||||
foreach (explode(',', $part) as $id) {
|
||||
$id = (int) $id;
|
||||
if ($id > 0) {
|
||||
$idMap[$id] = $id;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$idMap) {
|
||||
return '';
|
||||
}
|
||||
$ordered = Goods::where('id', 'in', array_values($idMap))
|
||||
->where('status', 'in', ['up', 'hidden'])
|
||||
->order('weigh asc, id desc')
|
||||
->column('id');
|
||||
return self::normalizeGoodsIdsString(implode(',', $ordered));
|
||||
}
|
||||
|
||||
protected static function enrichHomeGoodsBlocks(array $home)
|
||||
{
|
||||
$ctx = self::extractHomeGoodsContextFromBlocks($home);
|
||||
@@ -191,18 +225,9 @@ class Decorate extends Model
|
||||
continue;
|
||||
}
|
||||
$content = $block['content'] ?? [];
|
||||
if (!empty($content['ids'])) {
|
||||
return [
|
||||
'category_id' => (int) ($content['id'] ?? 0),
|
||||
'goods_ids' => self::normalizeGoodsIdsString($content['ids']),
|
||||
];
|
||||
}
|
||||
if (!empty($content['id'])) {
|
||||
$categoryId = (int) $content['id'];
|
||||
return [
|
||||
'category_id' => $categoryId,
|
||||
'goods_ids' => self::goodsIdsFromCategoryId($categoryId),
|
||||
];
|
||||
$ctx = self::buildHomeGoodsContextFromContent($content);
|
||||
if ($ctx['goods_ids'] !== '') {
|
||||
return $ctx;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,18 +236,9 @@ class Decorate extends Model
|
||||
continue;
|
||||
}
|
||||
$content = $home[$i]['content'] ?? [];
|
||||
if (!empty($content['ids'])) {
|
||||
return [
|
||||
'category_id' => (int) ($content['id'] ?? ($content['category_arr'][0]['id'] ?? 0)),
|
||||
'goods_ids' => self::normalizeGoodsIdsString($content['ids']),
|
||||
];
|
||||
}
|
||||
$categoryId = (int) ($content['id'] ?? ($content['category_arr'][0]['id'] ?? 0));
|
||||
if ($categoryId > 0) {
|
||||
return [
|
||||
'category_id' => $categoryId,
|
||||
'goods_ids' => self::goodsIdsFromCategoryId($categoryId),
|
||||
];
|
||||
$ctx = self::buildHomeGoodsContextFromContent($content);
|
||||
if ($ctx['goods_ids'] !== '' || $ctx['category_id'] > 0) {
|
||||
return $ctx;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -230,6 +246,47 @@ class Decorate extends Model
|
||||
return ['category_id' => 0, 'goods_ids' => ''];
|
||||
}
|
||||
|
||||
protected static function buildHomeGoodsContextFromContent(array $content)
|
||||
{
|
||||
$categoryId = (int) ($content['id'] ?? ($content['category_arr'][0]['id'] ?? 0));
|
||||
$ids = self::mergeGoodsIdStrings(
|
||||
!empty($content['ids']) ? self::normalizeGoodsIdsString($content['ids']) : '',
|
||||
self::extractGoodsIdsFromTimeData($content['timeData'] ?? null),
|
||||
$categoryId > 0 ? self::goodsIdsFromCategoryId($categoryId) : ''
|
||||
);
|
||||
return [
|
||||
'category_id' => $categoryId,
|
||||
'goods_ids' => $ids,
|
||||
];
|
||||
}
|
||||
|
||||
protected static function extractGoodsIdsFromTimeData($timeData)
|
||||
{
|
||||
if ($timeData === null || $timeData === '') {
|
||||
return '';
|
||||
}
|
||||
if (is_string($timeData)) {
|
||||
$decoded = json_decode($timeData, true);
|
||||
if (is_array($decoded)) {
|
||||
$timeData = $decoded;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
if (!is_array($timeData)) {
|
||||
return '';
|
||||
}
|
||||
$ids = [];
|
||||
foreach ($timeData as $row) {
|
||||
if (is_array($row) && !empty($row['id'])) {
|
||||
$ids[] = (int) $row['id'];
|
||||
} elseif (is_numeric($row)) {
|
||||
$ids[] = (int) $row;
|
||||
}
|
||||
}
|
||||
return self::normalizeGoodsIdsString(implode(',', $ids));
|
||||
}
|
||||
|
||||
protected static function normalizeGoodsIdsString($ids)
|
||||
{
|
||||
$ids = is_array($ids) ? implode(',', $ids) : (string) $ids;
|
||||
|
||||
@@ -90,17 +90,6 @@ class Goods extends Model
|
||||
|
||||
$goods = $goods->field('*,(sales + show_sales) as total_sales')->orderRaw($order)->order('id desc');
|
||||
|
||||
$cacheKey = 'goodslist-' . ($is_page ? 'page' : 'all') . '-' . md5(json_encode($params));
|
||||
|
||||
// 判断缓存
|
||||
$goodsCache = cache($cacheKey);
|
||||
|
||||
if ($goodsCache) {
|
||||
// 存在缓存直接 返回
|
||||
$goodsCache = json_decode($goodsCache, true);
|
||||
return $goodsCache ?: [];
|
||||
}
|
||||
|
||||
if ($is_page) {
|
||||
$goods = $goods->paginate($per_page ?? 10);
|
||||
$goodsData = $goods->items();
|
||||
|
||||
在新工单中引用
屏蔽一个用户