Browse Source

修改分类,支持多分类,豆号分割

moshaorui 6 days ago
parent
commit
9844b5d264
1 changed files with 27 additions and 15 deletions
  1. 27 15
      app/Http/Controllers/ProductController.php

+ 27 - 15
app/Http/Controllers/ProductController.php

@@ -30,27 +30,39 @@ class ProductController extends Controller
 
     public function category($slug)
     {
-       // $products = DistProduct::paginate(10); // 每页显示10个产品
-//        return $this->liquidRenderer->render('products_categories.liquid', ['products' => $products]);
-
         // 获取分类信息
         // 获取分类信息,并限定 dist_id
-        $category = DistProductCategory::where(function ($query) use ($slug) {
-            $query->where('slug', $slug)
-                ->orWhere('id', $slug);
-        })
-            ->where('dist_id', getDistId())
-            ->firstOrFail();
+        $slugs = [];
+        if (strpos($slug, ',') !== false) {
+            // 包含逗号
+            $slugs = explode(',', $slug);
+        } else {
+            // 不包含逗号
+            $slugs = [$slug];
+        }
 
-        if (!$category) {
+        $categoryIds = [];
+        foreach ($slugs as $slug) {
+            $category = DistProductCategory::where(function ($query) use ($slug) {
+                $query->where('slug', $slug)
+                    ->orWhere('id', $slug);
+            })
+                ->where('dist_id', getDistId())
+                ->firstOrFail();
+            $categoryIds[] = $category->id;
+        }
+
+        if (empty($categoryIds)) {
             abort('404');
         }
-        $categoryIds = [$category->id];
-        if ($category) {
+
+        if ($categoryIds) {
             //找下一级分类
-            $subCategories = DistProductCategory::where('parent_id', $category->id)->get();
-            foreach ($subCategories as $subCategory) {
-                $categoryIds[] = $subCategory->id;
+            foreach ($categoryIds as $id) {
+                $subCategories = DistProductCategory::where('parent_id', $id)->get();
+                foreach ($subCategories as $subCategory) {
+                    $categoryIds[] = $subCategory->id;
+                }
             }
         }