|
|
@@ -88,10 +88,8 @@ class HomeController extends APIController
|
|
|
]);
|
|
|
|
|
|
case 'category_carousel':
|
|
|
- $filters = $options['filters'] ?? [];
|
|
|
return array_merge($base, [
|
|
|
- 'title' => $options['title'] ?? '',
|
|
|
- 'categories' => $this->getCategoryCarouselData($filters),
|
|
|
+ 'images' => $options['images'] ?? [],
|
|
|
]);
|
|
|
|
|
|
case 'product_carousel':
|
|
|
@@ -111,95 +109,6 @@ class HomeController extends APIController
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取分类轮播数据
|
|
|
- */
|
|
|
- protected function getCategoryCarouselData(array $filters): array
|
|
|
- {
|
|
|
- // 1. 收集所有 parent_id(兼容字符串单值、字符串数组两种格式)
|
|
|
- $parentIds = $this->extractParentIds($filters);
|
|
|
-
|
|
|
- $sort = $filters['sort'] ?? 'asc';
|
|
|
- $limit = (int) ($filters['limit'] ?? 10);
|
|
|
-
|
|
|
- // 2. 无 parent_id:按 limit 查询
|
|
|
- if (empty($parentIds)) {
|
|
|
- $categories = $this->categoryRepository->getAll([
|
|
|
- 'status' => 1,
|
|
|
- 'sort' => $sort,
|
|
|
- 'limit' => $limit,
|
|
|
- 'locale' => core()->getRequestedLocaleCode(),
|
|
|
- ]);
|
|
|
-
|
|
|
- return $this->formatCategories($categories);
|
|
|
- }
|
|
|
-
|
|
|
- // 3. 多个 parent_id:按每个 parent 平均分配 limit 后合并去重
|
|
|
- $perParent = max((int) ceil($limit / count($parentIds)), 1);
|
|
|
- $result = [];
|
|
|
- $seenIds = [];
|
|
|
-
|
|
|
- foreach ($parentIds as $parentId) {
|
|
|
- $categories = $this->categoryRepository->getAll([
|
|
|
- 'status' => 1,
|
|
|
- 'sort' => $sort,
|
|
|
- 'limit' => $perParent,
|
|
|
- 'parent_id' => $parentId,
|
|
|
- 'locale' => core()->getRequestedLocaleCode(),
|
|
|
- ]);
|
|
|
-
|
|
|
- foreach ($categories as $cat) {
|
|
|
- if (isset($seenIds[$cat->id])) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- $seenIds[$cat->id] = true;
|
|
|
- $result[] = $cat;
|
|
|
- if (count($result) >= $limit) {
|
|
|
- break 2;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $this->formatCategories(collect($result));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 从 filters 中提取所有 parent_id(支持单值或多值数组)
|
|
|
- */
|
|
|
- protected function extractParentIds(array $filters): array
|
|
|
- {
|
|
|
- if (empty($filters['parent_id'])) {
|
|
|
- return [];
|
|
|
- }
|
|
|
-
|
|
|
- $values = $filters['parent_id'];
|
|
|
-
|
|
|
- // 已是数组
|
|
|
- if (is_array($values)) {
|
|
|
- return array_values(array_filter(array_map('intval', $values), fn ($v) => $v > 0));
|
|
|
- }
|
|
|
-
|
|
|
- // 单值字符串,去掉多余空白
|
|
|
- return array_values(array_filter(array_map('intval', explode(',', (string) $values)),
|
|
|
- fn ($v) => $v > 0));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 格式化分类集合为数组
|
|
|
- */
|
|
|
- protected function formatCategories($categories): array
|
|
|
- {
|
|
|
- return $categories->map(fn ($cat) => [
|
|
|
- 'id' => $cat->id,
|
|
|
- 'name' => $cat->name,
|
|
|
- 'slug' => $cat->slug,
|
|
|
- 'description' => $cat->description,
|
|
|
- 'image_url' => $cat->image_url,
|
|
|
- 'banner_url' => $cat->banner_url,
|
|
|
- 'product_count' => (int) ($cat->products_count ?? 0),
|
|
|
- ])->values()->toArray();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 获取产品轮播数据
|
|
|
*/
|