kphcdr преди 1 година
родител
ревизия
c88583cdc8

+ 8 - 1
app/Models/Goods/Category.php

@@ -40,7 +40,9 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @method static \Illuminate\Database\Query\Builder|Category withTrashed()
  * @method static \Illuminate\Database\Query\Builder|Category withoutTrashed()
  * @mixin \Eloquent
- * @property-read Category|null              $parent
+ * @property-read Category|null                                       $parent
+ * @property-read \Illuminate\Database\Eloquent\Collection|Category[] $childS
+ * @property-read int|null                                            $child_s_count
  */
 class Category extends BaseModel
 {
@@ -70,4 +72,9 @@ class Category extends BaseModel
     {
         return $this->belongsTo(Category::class, "parent_id");
     }
+
+    public function childS()
+    {
+        return $this->hasMany(Category::class, "parent_id", "id");
+    }
 }

+ 9 - 0
app/Models/Goods/Goods.php

@@ -4,6 +4,7 @@ namespace App\Models\Goods;
 
 use App\Base\BaseModel;
 use Illuminate\Database\Eloquent\SoftDeletes;
+use Illuminate\Support\Arr;
 
 /**
  * App\Models\Goods\Goods
@@ -43,6 +44,9 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @property-read int|null $map_count
  * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\GoodsSku[] $sku
  * @property-read int|null $sku_count
+ * @property-read mixed $thumb
+ * @property int $view_total
+ * @method static \Illuminate\Database\Eloquent\Builder|Goods whereViewTotal($value)
  */
 class Goods extends BaseModel
 {
@@ -70,4 +74,9 @@ class Goods extends BaseModel
     {
         return $this->hasMany(GoodsSku::class, "goods_id", "id");
     }
+
+    public function getThumbAttribute()
+    {
+        return Arr::first($this->image_list);
+    }
 }

+ 3 - 26
app/Modules/Mini/Controllers/PageController.php

@@ -50,32 +50,9 @@ class PageController extends BaseController
     {
         return $this->ok([
             "banner" => $this->service->banner(),
-            "categoryTree" => [
-                [
-                    "id" => 1,
-                    "name" => "一级分类",
-                    "child" => [
-                        [
-                            "id" => 1,
-                            "name" => "二级分类",
-                        ],
-                    ],
-                ],
-            ],
-            "hot_goods" => [
-                [
-                    "id" => 1,
-                    "name" => "商品名称",
-                    "thumb" => Image::imageUrl(),
-                ],
-            ],
-            "recommend_goods" => [
-                [
-                    "id" => 1,
-                    "name" => "商品名称",
-                    "thumb" => Image::imageUrl(),
-                ],
-            ],
+            "categoryTree" => $this->service->categoryTree(),
+            "hot_goods" => $this->service->hotGoods(10),
+            "recommend_goods" => $this->service->recommendedGoods(10),
         ]);
     }
 

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

@@ -3,7 +3,10 @@
 namespace App\Modules\Mini\Services;
 
 use App\Base\BaseService;
+use App\Models\Goods\Category;
+use App\Models\Goods\Goods;
 use App\Models\Setting;
+use Illuminate\Support\Facades\DB;
 
 class PageService extends BaseService
 {
@@ -22,4 +25,60 @@ class PageService extends BaseService
             ];
         })->values();
     }
+
+    public function categoryTree()
+    {
+        $categoryS = Category::with("childS")->where("parent_id", 0)->where("index_weight", ">", 0)->get(["id", "name", "thumb", "parent_id", "level", "weight"]);
+        return $categoryS->map(function (Category $category) {
+            return [
+                "id" => $category->id,
+                "name" => $category->name,
+                "thumb" => $category->thumb,
+                "child" => $this->childTree($category),
+            ];
+        });
+    }
+
+    protected function childTree(Category $category)
+    {
+        if ($category->childS->isEmpty()) {
+            return [];
+        }
+
+        return $category->childS->map(function (Category $category) {
+            return [
+                "id" => $category->id,
+                "name" => $category->name,
+                "thumb" => $category->thumb,
+                "child" => $this->childTree($category),
+            ];
+        });
+    }
+
+    public function hotGoods($num = 10)
+    {
+        $goods = Goods::orderByDesc("view_total")->limit($num)->get();
+
+        return $goods->map(function (Goods $g) {
+            return [
+                "id" => $g->id,
+                "name" => $g->name,
+                "thumb" => $g->thumb,
+                "view_total" => $g->view_total,
+            ];
+        });
+    }
+
+    public function recommendedGoods($num = 10)
+    {
+        $goods = Goods::orderBy(DB::raw("rand() "))->limit($num)->get();
+
+        return $goods->map(function (Goods $g) {
+            return [
+                "id" => $g->id,
+                "name" => $g->name,
+                "thumb" => $g->thumb,
+            ];
+        });
+    }
 }

+ 1 - 0
database/migrations/2023_04_28_071852_create_goods_table.php

@@ -55,6 +55,7 @@ return new class extends Migration {
             $table->text("spec_attr_html")->nullable();
             $table->text("faq_html")->nullable();
             $table->unsignedTinyInteger("status")->default(1)->comment("1 上架 2下架");
+            $table->unsignedInteger("view_total")->default(0)->comment("浏览量");
             $table->timestamps();
             $table->softDeletes();
         });