From f28bbbdb92a2bc9c7cbbd91284f1ee61f1bec88a Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Mon, 18 Jul 2022 21:13:51 +0800 Subject: [PATCH] fixed tab product --- beike/Repositories/ProductRepo.php | 62 ++++++++++++++++++++++++------ beike/Services/DesignService.php | 28 ++++++++++++++ 2 files changed, 79 insertions(+), 11 deletions(-) diff --git a/beike/Repositories/ProductRepo.php b/beike/Repositories/ProductRepo.php index 1838be8f..f49b57dd 100644 --- a/beike/Repositories/ProductRepo.php +++ b/beike/Repositories/ProductRepo.php @@ -12,8 +12,10 @@ namespace Beike\Repositories; use Beike\Models\Product; -use Beike\Shop\Http\Resources\ProductList; use Illuminate\Database\Eloquent\Builder; +use Beike\Shop\Http\Resources\ProductList; +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\HigherOrderBuilderProxy; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; class ProductRepo @@ -46,7 +48,27 @@ class ProductRepo } - public static function getBuilder($data = []): Builder + /** + * 通过产品ID获取产品列表 + * @param $productIds + * @return AnonymousResourceCollection + */ + public static function getProductsByIds($productIds): AnonymousResourceCollection + { + $builder = self::getBuilder(['product_ids' => $productIds]); + $products = $builder->get(); + $items = ProductList::collection($products); + return $items; + } + + + /** + * 获取产品筛选对象 + * + * @param array $data + * @return Builder + */ + public static function getBuilder(array $data = []): Builder { $builder = Product::query()->with('description', 'skus', 'master_sku'); @@ -59,6 +81,11 @@ class ProductRepo } }); } + + if (isset($data['product_ids'])) { + $builder->whereIn('id', $data['product_ids']); + } + if (isset($data['sku']) || isset($data['model'])) { $builder->whereHas('skus', function ($query) use ($data) { if (isset($data['sku'])) { @@ -70,16 +97,19 @@ class ProductRepo }); } + if (isset($data['model'])) { $builder->whereHas('skus', function ($query) use ($data) { $query->where('sku', 'like', "%{$data['sku']}%"); }); } + if (isset($data['name'])) { $builder->whereHas('description', function ($query) use ($data) { $query->where('name', 'like', "%{$data['name']}%"); }); } + if (isset($data['active'])) { $builder->where('active', (int)$data['active']); } @@ -113,7 +143,7 @@ class ProductRepo /** * 获取商品名称 * @param $id - * @return \Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed|string + * @return HigherOrderBuilderProxy|mixed|string */ public static function getName($id) { @@ -132,14 +162,7 @@ class ProductRepo */ public static function getNames($productIds): array { - if (empty($productIds)) { - return []; - } - $products = Product::query() - ->with(['description']) - ->whereIn('id', $productIds) - ->get(); - + $products = self::getListByProductIds($productIds); return $products->map(function ($product) { return [ 'id' => $product->id, @@ -147,4 +170,21 @@ class ProductRepo ]; })->toArray(); } + + + /** + * 通过产品ID获取产品列表 + * @return array|Builder[]|Collection + */ + public static function getListByProductIds($productIds) + { + if (empty($productIds)) { + return []; + } + $products = Product::query() + ->with(['description']) + ->whereIn('id', $productIds) + ->get(); + return $products; + } } diff --git a/beike/Services/DesignService.php b/beike/Services/DesignService.php index ade36ea4..20cca45f 100644 --- a/beike/Services/DesignService.php +++ b/beike/Services/DesignService.php @@ -11,6 +11,7 @@ namespace Beike\Services; +use Beike\Repositories\ProductRepo; use Illuminate\Support\Str; class DesignService @@ -42,6 +43,8 @@ class DesignService return self::handleSlideShow($content); } elseif ($moduleCode == 'image401') { return self::handleImage401($content); + } elseif ($moduleCode == 'tab_product') { + return self::handleTabProducts($content); } return $content; } @@ -85,6 +88,31 @@ class DesignService } + /** + * 处理选项卡商品列表模块 + * + * @param $content + * @return array + */ + private static function handleTabProducts($content): array + { + $tabs = $content['tabs'] ?? []; + if (empty($tabs)) { + return []; + } + + foreach ($tabs as $index => $tab) { + $tabs[$index]['title'] = $tab['title'][current_language_code()]; + $productsIds = $tab['products']; + if ($productsIds) { + $tabs[$index]['products'] = ProductRepo::getProductsByIds($productsIds); + } + } + $content['tabs'] = $tabs; + return $content; + } + + /** * 处理图片以及链接 * @throws \Exception