Browse Source

分类相关

kphcdr 1 year ago
parent
commit
f543a47bda

+ 5 - 0
app/Modules/Mini/Controllers/PageController.php

@@ -58,6 +58,11 @@ class PageController extends BaseController
 
     public function categoryGoods()
     {
+        $params = $this->valid([
+            "id" => "required",
+            "page_size" => "int",
+        ]);
+        $this->service->categoryGoods($params);
         return $this->ok([
             "total" => 1,
             "page_total" => 1,

+ 37 - 0
app/Modules/Mini/Services/PageService.php

@@ -5,7 +5,10 @@ namespace App\Modules\Mini\Services;
 use App\Base\BaseService;
 use App\Models\Goods\Category;
 use App\Models\Goods\Goods;
+use App\Models\Goods\GoodsCategoryMap;
 use App\Models\Setting;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\DB;
 
 class PageService extends BaseService
@@ -81,4 +84,38 @@ class PageService extends BaseService
             ];
         });
     }
+
+    public function childCategoryIdArr($id)
+    {
+        $cids = Category::where("parent_id", $id)->get(["id"])->pluck("id")->toArray();
+
+        $cidss = Category::whereIn("parent_id", $cids)->get(['id'])->pluck("id")->toArray();
+
+        return array_merge($cids, $cidss, [$id]);
+    }
+
+    public function categoryGoods($params)
+    {
+        $id = $params['id'];
+        $pageSize = Arr::get($params, "page_size");
+        $cidArr = $this->childCategoryIdArr($id);
+
+        $gidArr = GoodsCategoryMap::where("category_id", $cidArr)->get(['goods_id'])->pluck("goods_id")->unique()->toArray();
+
+        $p = Goods::where("status", Goods::STATUS_OK)->orderByDesc("weight")->whereIn("id", $gidArr)->paginate($pageSize);
+
+        return [
+            "total" => $p->total(),
+            "page_total" => $p->lastPage(),
+            "list" => array_map(function (Goods $model) {
+                return [
+                    "id" => $model->id,
+                    "name" => $model->name,
+                    "thumb" => $model->thumb,
+                    "weight" => $model->weight,
+                ];
+            }, $p->items()),
+        ];
+
+    }
 }