|
@@ -59,7 +59,7 @@ class HomeController extends APIController
|
|
|
|
|
|
|
|
$sections = [];
|
|
$sections = [];
|
|
|
foreach ($customizations as $item) {
|
|
foreach ($customizations as $item) {
|
|
|
- $section = $this->formatSection($item, $locale);
|
|
|
|
|
|
|
+ $section = $this->formatSection($item, $locale, $channel);
|
|
|
if ($section !== null) {
|
|
if ($section !== null) {
|
|
|
$sections[] = $section;
|
|
$sections[] = $section;
|
|
|
}
|
|
}
|
|
@@ -93,7 +93,7 @@ class HomeController extends APIController
|
|
|
/**
|
|
/**
|
|
|
* 格式化单个主题区块
|
|
* 格式化单个主题区块
|
|
|
*/
|
|
*/
|
|
|
- protected function formatSection($item, string $locale): ?array
|
|
|
|
|
|
|
+ protected function formatSection($item, string $locale, $channel = null): ?array
|
|
|
{
|
|
{
|
|
|
$base = [
|
|
$base = [
|
|
|
'id' => $item->id,
|
|
'id' => $item->id,
|
|
@@ -125,7 +125,7 @@ class HomeController extends APIController
|
|
|
$filters = $options['filters'] ?? [];
|
|
$filters = $options['filters'] ?? [];
|
|
|
return array_merge($base, [
|
|
return array_merge($base, [
|
|
|
'title' => $options['title'] ?? '',
|
|
'title' => $options['title'] ?? '',
|
|
|
- 'products' => $this->getProductCarouselData($filters),
|
|
|
|
|
|
|
+ 'products' => $this->getProductCarouselData($filters, $locale, $channel),
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
case 'services_content':
|
|
case 'services_content':
|
|
@@ -141,11 +141,14 @@ class HomeController extends APIController
|
|
|
/**
|
|
/**
|
|
|
* 获取产品轮播数据
|
|
* 获取产品轮播数据
|
|
|
*/
|
|
*/
|
|
|
- protected function getProductCarouselData(array $filters): array
|
|
|
|
|
|
|
+ protected function getProductCarouselData(array $filters, string $locale, $channel = null): array
|
|
|
{
|
|
{
|
|
|
- $cacheKey = 'home_products:' . md5(json_encode($filters));
|
|
|
|
|
|
|
+ $channelCode = $channel->code ?? core()->getCurrentChannel()->code;
|
|
|
|
|
+ $channelId = $channel->id ?? core()->getCurrentChannel()->id;
|
|
|
|
|
|
|
|
- return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($filters) {
|
|
|
|
|
|
|
+ $cacheKey = 'home_products:' . $channelCode . ':' . $locale . ':' . md5(json_encode($filters));
|
|
|
|
|
+
|
|
|
|
|
+ return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($filters, $locale, $channelCode, $channelId) {
|
|
|
$params = [
|
|
$params = [
|
|
|
'status' => 1,
|
|
'status' => 1,
|
|
|
'visible_individually'=> 1,
|
|
'visible_individually'=> 1,
|
|
@@ -174,9 +177,18 @@ class HomeController extends APIController
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
$customerGroupId = 1;
|
|
$customerGroupId = 1;
|
|
|
}
|
|
}
|
|
|
- $channelId = core()->getCurrentChannel()->id;
|
|
|
|
|
|
|
|
|
|
- return $products->map(function ($product) use ($customerGroupId, $channelId) {
|
|
|
|
|
|
|
+ // 预先解析 url_key 属性 id,供下方按 locale 显式取值
|
|
|
|
|
+ $urlKeyAttributeId = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ $urlKeyAttributeId = app(\Webkul\Attribute\Repositories\AttributeRepository::class)
|
|
|
|
|
+ ->findOneByField('code', 'url_key')?->id;
|
|
|
|
|
+ } catch (\Throwable $e) {
|
|
|
|
|
+ $urlKeyAttributeId = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ $defaultLocale = core()->getDefaultLocaleCodeFromDefaultChannel();
|
|
|
|
|
+
|
|
|
|
|
+ return $products->map(function ($product) use ($customerGroupId, $channelId, $locale, $urlKeyAttributeId, $defaultLocale) {
|
|
|
$image = $product->images->first();
|
|
$image = $product->images->first();
|
|
|
$approvedReviews = $product->reviews->where('status', 'approved');
|
|
$approvedReviews = $product->reviews->where('status', 'approved');
|
|
|
|
|
|
|
@@ -187,6 +199,20 @@ class HomeController extends APIController
|
|
|
->first();
|
|
->first();
|
|
|
$minPrice = $index ? (float) $index->min_price : (float) $product->price;
|
|
$minPrice = $index ? (float) $index->min_price : (float) $product->price;
|
|
|
|
|
|
|
|
|
|
+ // 显式按 locale 取 url_key,避免依赖全局请求上下文(缓存/队列场景下易失效)
|
|
|
|
|
+ $urlKey = null;
|
|
|
|
|
+ if ($urlKeyAttributeId && $product->relationLoaded('attribute_values')) {
|
|
|
|
|
+ $urlKey = $product->attribute_values
|
|
|
|
|
+ ->where('attribute_id', $urlKeyAttributeId)
|
|
|
|
|
+ ->where('locale', $locale)
|
|
|
|
|
+ ->first()?->text_value
|
|
|
|
|
+ ?: $product->attribute_values
|
|
|
|
|
+ ->where('attribute_id', $urlKeyAttributeId)
|
|
|
|
|
+ ->where('locale', $defaultLocale)
|
|
|
|
|
+ ->first()?->text_value;
|
|
|
|
|
+ }
|
|
|
|
|
+ $urlKey ??= $product->url_key;
|
|
|
|
|
+
|
|
|
return [
|
|
return [
|
|
|
'id' => $product->id,
|
|
'id' => $product->id,
|
|
|
'sku' => $product->sku,
|
|
'sku' => $product->sku,
|
|
@@ -197,6 +223,7 @@ class HomeController extends APIController
|
|
|
'min_price' => $minPrice,
|
|
'min_price' => $minPrice,
|
|
|
'special_price' => $product->special_price,
|
|
'special_price' => $product->special_price,
|
|
|
'image_url' => $image->url ?? null,
|
|
'image_url' => $image->url ?? null,
|
|
|
|
|
+ 'urlKey' => $urlKey,
|
|
|
'is_new' => (bool) ($product->new ?? false),
|
|
'is_new' => (bool) ($product->new ?? false),
|
|
|
'is_featured' => (bool) ($product->featured ?? false),
|
|
'is_featured' => (bool) ($product->featured ?? false),
|
|
|
'short_description'=> $product->short_description,
|
|
'short_description'=> $product->short_description,
|