bianjunhui 2 dienas atpakaļ
vecāks
revīzija
02c1104b4d

+ 28 - 6
packages/Webkul/Admin/src/Resources/views/settings/themes/edit/category-carousel.blade.php

@@ -104,11 +104,12 @@
                     v-if="options.filters.length"
                     v-if="options.filters.length"
                     v-for="(filter, index) in options.filters"
                     v-for="(filter, index) in options.filters"
                 >
                 >
-                    <!-- Hidden Input -->
+                    <!-- Hidden Input: 多 parent_id 用逗号拼接成一个 input,避开同名覆盖问题 -->
                     <input
                     <input
+                        v-if="index === 0"
                         type="hidden"
                         type="hidden"
-                        :name="'{{ $currentLocale->code }}[options][filters][' + filter.key + '][]'"
-                        :value="filter.value"
+                        :name="'{{ $currentLocale->code }}[options][filters][' + filter.key + ']'"
+                        :value="getFilterValue(filter.key)"
                     > 
                     > 
                 
                 
                     <!-- Details -->
                     <!-- Details -->
@@ -332,15 +333,25 @@
                 }
                 }
 
 
                 // 把对象形式(保存后)还原成数组形式(用于渲染)
                 // 把对象形式(保存后)还原成数组形式(用于渲染)
-                // 也兼容老格式(保存的是单值字符串)
+                // 支持三种格式:
+                //   1. 老格式:单值字符串    "5"
+                //   2. 字符串数组:         ["4","2","3"]
+                //   3. 逗号分隔字符串:      "4,2,3"
                 const raw = this.options.filters;
                 const raw = this.options.filters;
                 const reserved = ['sort', 'limit', 'title'];
                 const reserved = ['sort', 'limit', 'title'];
                 const list = [];
                 const list = [];
 
 
                 Object.keys(raw).forEach(key => {
                 Object.keys(raw).forEach(key => {
                     if (reserved.includes(key)) return;
                     if (reserved.includes(key)) return;
-                    const vals = Array.isArray(raw[key]) ? raw[key] : [raw[key]];
-                    vals.forEach(v => list.push({ key: key, value: v }));
+                    let vals = raw[key];
+                    if (typeof vals === 'string') {
+                        vals = vals.indexOf(',') >= 0
+                            ? vals.split(',')
+                            : [vals];
+                    } else if (! Array.isArray(vals)) {
+                        vals = [String(vals)];
+                    }
+                    vals.forEach(v => list.push({ key: key, value: String(v).trim() }));
                 });
                 });
 
 
                 this.options.filters = list;
                 this.options.filters = list;
@@ -366,6 +377,17 @@
                 handleFilter(event) {
                 handleFilter(event) {
                     this.filters.applied = this.filters.available.find(filter => filter.code == event.target.value);
                     this.filters.applied = this.filters.available.find(filter => filter.code == event.target.value);
                 },
                 },
+
+                /**
+                 * 把同一 key 的多个 filter 值聚合成一个逗号分隔字符串,
+                 * 避开同名表单 input 互相覆盖的问题。
+                 */
+                getFilterValue(key) {
+                    return this.options.filters
+                        .filter(f => f.key === key)
+                        .map(f => f.value)
+                        .join(',');
+                },
             },
             },
         });
         });
     </script>
     </script>

+ 1 - 92
packages/Webkul/Shop/src/Http/Controllers/API/HomeController.php

@@ -88,10 +88,8 @@ class HomeController extends APIController
                 ]);
                 ]);
 
 
             case 'category_carousel':
             case 'category_carousel':
-                $filters = $options['filters'] ?? [];
                 return array_merge($base, [
                 return array_merge($base, [
-                    'title'      => $options['title'] ?? '',
-                    'categories' => $this->getCategoryCarouselData($filters),
+                    'images' => $options['images'] ?? [],
                 ]);
                 ]);
 
 
             case 'product_carousel':
             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();
-    }
-
     /**
     /**
      * 获取产品轮播数据
      * 获取产品轮播数据
      */
      */