fixed tab product

This commit is contained in:
Edward Yang 2022-07-18 21:13:51 +08:00
parent 192e020fc0
commit f28bbbdb92
2 changed files with 79 additions and 11 deletions

View File

@ -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;
}
}

View File

@ -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