kphcdr 1 jaar geleden
bovenliggende
commit
d39e787c8d

+ 8 - 0
app/Exceptions/Handler.php

@@ -5,6 +5,7 @@ namespace App\Exceptions;
 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
 use Illuminate\Support\Arr;
 use Illuminate\Validation\ValidationException;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
 class Handler extends ExceptionHandler
 {
@@ -60,5 +61,12 @@ class Handler extends ExceptionHandler
                 "code" => 400,
             ]);
         });
+
+        $this->renderable(function (NotFoundHttpException $e) {
+            return response()->json([
+                "error" => "数据不存在",
+                "code" => 400,
+            ]);
+        });
     }
 }

+ 18 - 1
app/Models/Goods/Goods.php

@@ -37,13 +37,20 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @method static \Illuminate\Database\Query\Builder|Goods withTrashed()
  * @method static \Illuminate\Database\Query\Builder|Goods withoutTrashed()
  * @mixin \Eloquent
+ * @property int                             $status 1 上架 2下架
+ * @method static \Illuminate\Database\Eloquent\Builder|Goods whereStatus($value)
+ * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\GoodsCategoryMap[] $map
+ * @property-read int|null $map_count
+ * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goods\GoodsSku[] $sku
+ * @property-read int|null $sku_count
  */
 class Goods extends BaseModel
 {
     use SoftDeletes;
 
     protected $table = "goods";
-
+    const STATUS_OK = 1;
+    const STATUS_OFF = 2;
     protected $casts = [
         "image_list" => "array",
         "spec" => "array",
@@ -53,4 +60,14 @@ class Goods extends BaseModel
     {
 
     }
+
+    public function map()
+    {
+        return $this->hasMany(GoodsCategoryMap::class, "goods_id", "id");
+    }
+
+    public function sku()
+    {
+        return $this->hasMany(GoodsSku::class, "goods_id", "id");
+    }
 }

+ 12 - 0
app/Models/Goods/GoodsCategoryMap.php

@@ -24,6 +24,8 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsCategoryMap whereIsPublic($value)
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsCategoryMap whereUpdatedAt($value)
  * @mixin \Eloquent
+ * @property-read \App\Models\Goods\Category|null $category
+ * @property-read \App\Models\Goods\Goods|null $goods
  */
 class GoodsCategoryMap extends BaseModel
 {
@@ -33,4 +35,14 @@ class GoodsCategoryMap extends BaseModel
     {
 
     }
+
+    public function category()
+    {
+        return $this->belongsTo(Category::class);
+    }
+
+    public function goods()
+    {
+        return $this->belongsTo(Goods::class);
+    }
 }

+ 7 - 2
app/Models/Goods/GoodsSku.php

@@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @property int                             $id
  * @property int                             $goods_id
  * @property string                          $sn 型号
- * @property string $3d_url 3D的跳转地址
+ * @property string                          $url_3d 3D的跳转地址
  * @property array                           $spec_attr_list 规格数据
  * @property int                             $is_use 是否使用
  * @property \Illuminate\Support\Carbon|null $created_at
@@ -21,7 +21,6 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku newQuery()
  * @method static \Illuminate\Database\Query\Builder|GoodsSku onlyTrashed()
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku query()
- * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku where3dUrl($value)
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku whereCreatedAt($value)
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku whereDeletedAt($value)
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku whereGoodsId($value)
@@ -30,6 +29,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku whereSn($value)
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku whereSpecAttrList($value)
  * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku whereUpdatedAt($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|GoodsSku whereUrl3d($value)
  * @method static \Illuminate\Database\Query\Builder|GoodsSku withTrashed()
  * @method static \Illuminate\Database\Query\Builder|GoodsSku withoutTrashed()
  * @mixin \Eloquent
@@ -48,4 +48,9 @@ class GoodsSku extends BaseModel
     {
 
     }
+
+    public function specAttr()
+    {
+
+    }
 }

+ 27 - 75
app/Modules/Admin/Controllers/Admin/GoodsController.php

@@ -3,50 +3,42 @@
 namespace App\Modules\Admin\Controllers\Admin;
 
 use App\Base\BaseController;
+use App\Models\Goods\Goods;
 use App\Modules\Admin\Services\GoodsService;
+use App\Modules\Admin\Services\SpecService;
 use Faker\Provider\Image;
 
 class GoodsController extends BaseController
 {
-    /** @var GoodsService $service */
-    protected $service;
+    protected SpecService $specService;
+    protected GoodsService $service;
 
-    public function __construct(GoodsService $authService)
+    public function __construct(GoodsService $service, SpecService $authService)
     {
-        $this->service = $authService;
+        $this->specService = $authService;
+        $this->service = $service;
     }
 
     public function paginate()
     {
-
-        return $this->ok([
-            "total" => 1,
-            "page_total" => 1,
-            "list" => [
-                [
-                    "id" => 1,
-                    "name" => "商品名称",
-                    "thumb" => Image::imageUrl(),
-                    "weight" => 1,
-                    "status" => 1,
-                ],
-            ],
+        $params = $this->valid([
+            "page_size" => "",
+            "name" => "",
+            "status" => "",
         ]);
+        return $this->ok($this->service->paginate($params));
     }
 
     public function changeStatus()
     {
-//        $id = request()->input("id", 0);
-//        $m = Setting::whereType(Setting::TYPE_BANNER)->where("id", $id)->firstOrFail();
-//        $value = $m->value ?? [];
-//
-//        $isUse = 1;
-//        if (Arr::get($value, "is_use")) {
-//            $isUse = 0;
-//        }
-//        $value['is_use'] = $isUse;
-//        $m->value = $value;
-//        $m->save();
+        $id = request()->input("id", 0);
+        $m = Goods::findOrFail($id);
+        if ($m->status == Goods::STATUS_OK) {
+            $m->status = Goods::STATUS_OFF;
+        } else {
+            $m->status = Goods::STATUS_OK;
+        }
+        $m->save();
         return $this->ok();
     }
 
@@ -80,50 +72,10 @@ class GoodsController extends BaseController
 
     public function info()
     {
-        return $this->ok([
-            "id" => 1,
-            "image_list" => [
-                Image::imageUrl(),
-                Image::imageUrl(),
-            ],
-            "category" => [
-                [
-                    "id" => 1,
-                    "is_public" => 1,
-                ],
-                [
-                    "id" => 2,
-                    "is_public" => 0,
-                ],
-            ],
-            "spec" => [
-                [
-                    "id" => 1,
-                ],
-            ],
-            "sku" => [
-                [
-                    "id" => 0,
-                    "sn" => "规格型号",
-                    "3d_url" => "",
-                    "spec_attr" => [
-                        [
-                            "id" => 1,
-                            "name" => "属性一",
-                        ],
-                        [
-                            "id" => 2,
-                            "name" => "属性二",
-                        ],
-                    ],
-                    "is_use" => 1,
-                ],
-            ],
-            "weight" => 1,
-            "desc_html" => "<h1>it's html</h1>",
-            "spec_attr_html" => "<h1>it's html</h1>",
-            "faq_html" => "<h1>it's html</h1>",
+        $params = $this->valid([
+            "id" => "required",
         ]);
+        return $this->ok($this->service->info($params));
     }
 
     public function store()
@@ -139,7 +91,7 @@ class GoodsController extends BaseController
             "page_size" => "",
         ]);
 
-        return $this->ok($this->service->specPaginate($data));
+        return $this->ok($this->specService->paginate($data));
     }
 
     public function specStore()
@@ -154,7 +106,7 @@ class GoodsController extends BaseController
             "is_custom" => "int",
         ]);
 
-        return $this->ok($this->service->specStore($params));
+        return $this->ok($this->specService->specStore($params));
     }
 
     public function specInfo()
@@ -163,7 +115,7 @@ class GoodsController extends BaseController
             "id" => "required",
         ]);
 
-        return $this->ok($this->service->specInfo($params));
+        return $this->ok($this->specService->specInfo($params));
     }
 
     public function specDelete()
@@ -172,6 +124,6 @@ class GoodsController extends BaseController
             "id" => "required",
         ]);
 
-        return $this->ok($this->service->specDelete($data));
+        return $this->ok($this->specService->specDelete($data));
     }
 }

+ 51 - 64
app/Modules/Admin/Services/GoodsService.php

@@ -3,88 +3,75 @@
 namespace App\Modules\Admin\Services;
 
 use App\Base\BaseService;
-use App\Exceptions\ClientException;
-use App\Models\Goods\Spec;
+use App\Models\Goods\Goods;
+use App\Models\Goods\GoodsCategoryMap;
+use App\Models\Goods\GoodsSku;
 use App\Models\Goods\SpecAttr;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Support\Arr;
-use Illuminate\Support\Facades\DB;
 
 class GoodsService extends BaseService
 {
-    public function specPaginate($params)
+    public function paginate($params)
     {
-        $p = Spec::query()->when($params['name'], function (Builder $query) use ($params) {
-            return $query->where("name", 'like', "%{$params['name']}%");
+        $p = Goods::query()->when($params['status'], function (Builder $query) use ($params) {
+            return $query->where("status", $params['status']);
+        })->when($params['name'], function (Builder $query) use ($params) {
+            return $query->where("name", "like", "%{$params['name']}%");
         })->orderByDesc("id")->paginate($params['page_size']);
 
         return [
             "total" => $p->total(),
             "page_total" => $p->lastPage(),
-            "list" => array_map(function (Spec $u) {
-                return $u->format(Spec::FORMAT_ATTR);
+            "list" => array_map(function (Goods $model) {
+
+                return [
+                    "id" => $model->id,
+                    "name" => $model->name,
+                    "thumb" => Arr::first($model->image_list),
+                    "weight" => $model->weight,
+                    "status" => $model->status,
+                    "created_at" => $model->created_at->format("Y-m-d H:i:s"),
+                ];
             }, $p->items()),
         ];
     }
 
-    public function specStore($params)
+    public function info($params)
     {
-        $id = $params['id'] ?? 0;
-        if ($id) {
-            $spec = Spec::find($id);
-        } else {
-            $spec = new Spec();
-        }
-        $spec->name = Arr::get($params, 'name');
-        $spec->index_weight = Arr::get($params, 'index_weight');
-        $spec->category_weight = Arr::get($params, 'category_weight');
-        $spec->search_weight = Arr::get($params, 'search_weight');
-        $spec->is_custom = (int)Arr::get($params, 'is_custom');
-
-        DB::transaction(function () use ($params, $spec) {
-            $spec->save();
-            $attr = Arr::get($params, "attr", []);
-            $holdIdArr = Arr::pluck($attr, "id");
-            SpecAttr::query()->where("spec_id", $spec->id)->whereNotIn("id", $holdIdArr)->delete();
-
-            foreach ($attr as $k => $v) {
-                $id = Arr::get($v, "id");
-                if ($id) {
-                    $m = SpecAttr::whereSpecId($spec->id)->find($id);
-                    if (is_null($m)) {
-                        logger()->error("无法保存这个属性值:" . $v['name'] ?? "");
-                        throw new ClientException("无法保存这个属性值:" . $v['name'] ?? "");
-                    }
-                } else {
-                    $m = new SpecAttr();
-                }
-                $m->spec_id = $spec->id;
-                $m->name = $v['name'] ?? "";
-                $m->save();
-            }
-
-        });
-
-        return true;
-    }
+        $id = $params['id'];
+        $model = Goods::findOrFail($id);
 
-    public function specInfo($data)
-    {
-        $id = $data['id'];
-        $spec = Spec::findOrFail($id);
-        return $spec->format(Spec::FORMAT_ATTR);
-    }
-
-    public function specDelete($data)
-    {
-        $id = $data['id'] ?? 0;
-        // @TODO kphcdr 判断是否有商品还在使用这个规则,否则无法删除
+        $return = [
+            "id" => $model->id,
+            "image_list" => $model->image_list,
+            "category" => $model->map->map(function (GoodsCategoryMap $map) {
+                return [
+                    "id" => $map->category->id,
+                    "is_public" => $map->is_public,
+                ];
+            }),
+            "spec" => $model->spec,
+            "sku" => $model->sku->map(function (GoodsSku $sku) {
+                return [
+                    "id" => $sku->id,
+                    "sn" => $sku->sn,
+                    "3d_url" => $sku->url_3d,
+                    "is_use" => $sku->is_use,
+                    "spec_attr" => array_map(function ($specAttrId) {
+                        return [
+                            "id" => $specAttrId,
+                            "name" => SpecAttr::where("id", $specAttrId)->value("name"),
+                        ];
+                    }, $sku->spec_attr_list),
+                ];
+            }),
+            "weight" => $model->weight,
+            "desc_html" => $model->desc_html,
+            "spec_attr_html" => $model->spec_attr_html,
+            "faq_html" => $model->faq_html,
+        ];
 
-        $spec = Spec::find($id);
-        if ($spec) {
-            SpecAttr::where("spec_id", $spec->id)->delete();
-            $spec->delete();
-        }
-        return true;
+        return $return;
     }
 }

+ 90 - 0
app/Modules/Admin/Services/SpecService.php

@@ -0,0 +1,90 @@
+<?php
+
+namespace App\Modules\Admin\Services;
+
+use App\Base\BaseService;
+use App\Exceptions\ClientException;
+use App\Models\Goods\Spec;
+use App\Models\Goods\SpecAttr;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\DB;
+
+class SpecService extends BaseService
+{
+    public function paginate($params)
+    {
+        $p = Spec::query()->when($params['name'], function (Builder $query) use ($params) {
+            return $query->where("name", 'like', "%{$params['name']}%");
+        })->orderByDesc("id")->paginate($params['page_size']);
+
+        return [
+            "total" => $p->total(),
+            "page_total" => $p->lastPage(),
+            "list" => array_map(function (Spec $u) {
+                return $u->format(Spec::FORMAT_ATTR);
+            }, $p->items()),
+        ];
+    }
+
+    public function specStore($params)
+    {
+        $id = $params['id'] ?? 0;
+        if ($id) {
+            $spec = Spec::find($id);
+        } else {
+            $spec = new Spec();
+        }
+        $spec->name = Arr::get($params, 'name');
+        $spec->index_weight = Arr::get($params, 'index_weight');
+        $spec->category_weight = Arr::get($params, 'category_weight');
+        $spec->search_weight = Arr::get($params, 'search_weight');
+        $spec->is_custom = (int)Arr::get($params, 'is_custom');
+
+        DB::transaction(function () use ($params, $spec) {
+            $spec->save();
+            $attr = Arr::get($params, "attr", []);
+            $holdIdArr = Arr::pluck($attr, "id");
+            SpecAttr::query()->where("spec_id", $spec->id)->whereNotIn("id", $holdIdArr)->delete();
+
+            foreach ($attr as $k => $v) {
+                $id = Arr::get($v, "id");
+                if ($id) {
+                    $m = SpecAttr::whereSpecId($spec->id)->find($id);
+                    if (is_null($m)) {
+                        logger()->error("无法保存这个属性值:" . $v['name'] ?? "");
+                        throw new ClientException("无法保存这个属性值:" . $v['name'] ?? "");
+                    }
+                } else {
+                    $m = new SpecAttr();
+                }
+                $m->spec_id = $spec->id;
+                $m->name = $v['name'] ?? "";
+                $m->save();
+            }
+
+        });
+
+        return true;
+    }
+
+    public function specInfo($data)
+    {
+        $id = $data['id'];
+        $spec = Spec::findOrFail($id);
+        return $spec->format(Spec::FORMAT_ATTR);
+    }
+
+    public function specDelete($data)
+    {
+        $id = $data['id'] ?? 0;
+        // @TODO kphcdr 判断是否有商品还在使用这个规则,否则无法删除
+
+        $spec = Spec::find($id);
+        if ($spec) {
+            SpecAttr::where("spec_id", $spec->id)->delete();
+            $spec->delete();
+        }
+        return true;
+    }
+}

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

@@ -54,6 +54,7 @@ return new class extends Migration {
             $table->text("desc_html")->nullable();
             $table->text("spec_attr_html")->nullable();
             $table->text("faq_html")->nullable();
+            $table->unsignedTinyInteger("status")->default(1)->comment("1 上架 2下架");
             $table->timestamps();
             $table->softDeletes();
         });
@@ -70,7 +71,7 @@ return new class extends Migration {
             $table->id();
             $table->unsignedInteger("goods_id")->default(0);
             $table->string("sn")->default("")->comment("型号");
-            $table->string("3d_url")->default("")->comment("3D的跳转地址");
+            $table->string("url_3d")->default("")->comment("3D的跳转地址");
             $table->string("spec_attr_list", 2000)->default("")->comment("规格数据");
             $table->unsignedTinyInteger("is_use")->default(1)->comment("是否使用");
             $table->timestamps();