Bläddra i källkod

Merge branch 'dev-rewardPoints' into dev

bianjunhui 1 dag sedan
förälder
incheckning
f964b5ea70
1 ändrade filer med 31 tillägg och 6 borttagningar
  1. 31 6
      packages/Webkul/Shop/src/Http/Controllers/API/HomeController.php

+ 31 - 6
packages/Webkul/Shop/src/Http/Controllers/API/HomeController.php

@@ -36,6 +36,8 @@ class HomeController extends APIController
      */
     public function index(): JsonResponse
     {
+        $startTime = microtime(true);
+
         $locale = core()->getRequestedLocaleCode();
 
         // 通过 X-Channel header 区分 PC(default) / M(wap) 端
@@ -77,9 +79,14 @@ class HomeController extends APIController
             ];
         });
 
+        $elapsed = round((microtime(true) - $startTime) * 1000);
+
+        // \Log::info("[Home API] {$channel->code}:{$locale} | 耗时: {$elapsed}ms");
+
         return response()->json([
             'success' => true,
             'data'    => $data,
+            '_debug'  => ['elapsed_ms' => $elapsed],
         ]);
     }
 
@@ -154,14 +161,32 @@ class HomeController extends APIController
                 $params['featured'] = 1;
             }
 
-            $products = $this->productRepository->getAll($params);
-
-            return $products->map(function ($product) {
-                $image = $product->images->first()
-                    ?? $product->base_image;
+            // 使用 ProductRepository 查询,通过 params 传参,不走原生 SQL
+            $products = $this->productRepository
+                ->with(['images', 'reviews', 'price_indices'])
+                ->getAll($params);
+
+            // 批量加载 price_indices 后用内存计算 min_price,避免每个产品查一次
+            $customerGroupId = null;
+            try {
+                $customerGroupId = app(\Webkul\Customer\Repositories\CustomerRepository::class)
+                    ->getCurrentGroup()->id;
+            } catch (\Exception $e) {
+                $customerGroupId = 1;
+            }
+            $channelId = core()->getCurrentChannel()->id;
 
+            return $products->map(function ($product) use ($customerGroupId, $channelId) {
+                $image = $product->images->first();
                 $approvedReviews = $product->reviews->where('status', 'approved');
 
+                // 直接从 price_indices 计算 min_price,避免调 getMinimalPrice()
+                $index = $product->price_indices
+                    ->where('customer_group_id', $customerGroupId)
+                    ->where('channel_id', $channelId)
+                    ->first();
+                $minPrice = $index ? (float) $index->min_price : (float) $product->price;
+
                 return [
                     'id'               => $product->id,
                     'sku'              => $product->sku,
@@ -169,7 +194,7 @@ class HomeController extends APIController
                     'slug'             => $product->slug,
                     'type'             => $product->type,
                     'price'            => (float) $product->price,
-                    'min_price'        => $product->getTypeInstance()->getMinimalPrice(),
+                    'min_price'        => $minPrice,
                     'special_price'    => $product->special_price,
                     'image_url'        => $image->url ?? null,
                     'is_new'           => (bool) ($product->new ?? false),