浏览代码

首页数据更改

bianjunhui 1 月之前
父节点
当前提交
4068884d22
共有 1 个文件被更改,包括 35 次插入 和 8 次删除
  1. 35 8
      packages/Webkul/Shop/src/Http/Controllers/API/HomeController.php

+ 35 - 8
packages/Webkul/Shop/src/Http/Controllers/API/HomeController.php

@@ -59,7 +59,7 @@ class HomeController extends APIController
 
             $sections = [];
             foreach ($customizations as $item) {
-                $section = $this->formatSection($item, $locale);
+                $section = $this->formatSection($item, $locale, $channel);
                 if ($section !== null) {
                     $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 = [
             'id'        => $item->id,
@@ -125,7 +125,7 @@ class HomeController extends APIController
                 $filters = $options['filters'] ?? [];
                 return array_merge($base, [
                     'title'    => $options['title'] ?? '',
-                    'products' => $this->getProductCarouselData($filters),
+                    'products' => $this->getProductCarouselData($filters, $locale, $channel),
                 ]);
 
             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 = [
                 'status'              => 1,
                 'visible_individually'=> 1,
@@ -174,9 +177,18 @@ class HomeController extends APIController
             } catch (\Exception $e) {
                 $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();
                 $approvedReviews = $product->reviews->where('status', 'approved');
 
@@ -187,6 +199,20 @@ class HomeController extends APIController
                     ->first();
                 $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 [
                     'id'               => $product->id,
                     'sku'              => $product->sku,
@@ -197,6 +223,7 @@ class HomeController extends APIController
                     'min_price'        => $minPrice,
                     'special_price'    => $product->special_price,
                     'image_url'        => $image->url ?? null,
+                    'urlKey'           => $urlKey,
                     'is_new'           => (bool) ($product->new ?? false),
                     'is_featured'      => (bool) ($product->featured ?? false),
                     'short_description'=> $product->short_description,