From 523934e7e9bba0c62dbeac6efce7f915376423d6 Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Wed, 14 Sep 2022 09:24:08 +0800 Subject: [PATCH] goods-cart --- ...2_08_19_151926_create_goods_cart_table.php | 2 -- routes/api.php | 1 - src/Http/Api/GoodsCartController.php | 33 +------------------ src/Http/Resources/GoodsCartResource.php | 5 --- src/Models/GoodsCart.php | 8 +---- 5 files changed, 2 insertions(+), 47 deletions(-) diff --git a/database/migrations/2022_08_19_151926_create_goods_cart_table.php b/database/migrations/2022_08_19_151926_create_goods_cart_table.php index 34cd13b..75c892f 100644 --- a/database/migrations/2022_08_19_151926_create_goods_cart_table.php +++ b/database/migrations/2022_08_19_151926_create_goods_cart_table.php @@ -18,7 +18,6 @@ return new class extends Migration $table->unsignedBigInteger('user_id')->comment('用户ID'); $table->unsignedBigInteger('goods_id')->comment('商品ID'); - $table->unsignedBigInteger('merchant_id')->nullable()->comment('店铺 ID'); $table->string('goods_name'); $table->string('goods_sn')->nullable(); @@ -26,7 +25,6 @@ return new class extends Migration $table->unsignedBigInteger('goods_sku_id')->nullable()->comment('商品sku ID'); $table->string('cover_image')->nullable()->comment('封面图'); $table->decimal('price', 12, 2)->comment('售价'); - $table->decimal('vip_price', 12, 2)->comment('售价'); $table->json('attr')->nullable()->comment('属性'); $table->json('spec')->nullable()->comment('规格'); $table->json('part')->nullable()->comment('配件'); diff --git a/routes/api.php b/routes/api.php index 8e98a6c..c829386 100644 --- a/routes/api.php +++ b/routes/api.php @@ -10,7 +10,6 @@ Route::group([ ], function () { Route::get('goods/category', [GoodsCategoryController::class, 'index']); - Route::get('goods/cart-by-merchant', [GoodsCartController::class, 'groupByMerchant']); Route::delete('goods/cart', [GoodsCartController::class, 'destroy']); Route::apiResource('goods/cart', GoodsCartController::class)->only(['index', 'store', 'update']); diff --git a/src/Http/Api/GoodsCartController.php b/src/Http/Api/GoodsCartController.php index 969ebcf..23fc753 100644 --- a/src/Http/Api/GoodsCartController.php +++ b/src/Http/Api/GoodsCartController.php @@ -6,49 +6,20 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Peidikeji\Goods\Http\Resources\GoodsCartResource; use Peidikeji\Goods\Models\Goods; -use Peidikeji\Merchant\Http\Resources\MerchantTinyResource; -use Peidikeji\Merchant\Models\Merchant; class GoodsCartController extends Controller { - public function index(Request $request) + public function index() { $user = auth('api')->user(); $query = $user->carts(); - if ($request->filled('merchant_id')) { - $id = $request->input('merchant_id'); - if ($id) { - $query->where('merchant_id', $id); - } else { - $query->whereNull('merchant_id'); - } - } - $list = $query->get(); return $this->json(GoodsCartResource::collection($list)); } - public function groupByMerchant() - { - $user = auth('api')->user(); - $list = $user->carts()->get()->groupBy('merchant_id'); - - $data = []; - foreach ($list as $id => $items) { - $subData = GoodsCartResource::collection($items); - $merchant = $id ? MerchantTinyResource::make(Merchant::findOrFail($id)) : ['id' => '', 'name' => '自营店铺']; - array_push($data, [ - 'merchant' => $merchant, - 'list' => $subData, - ]); - } - - return $this->json($data); - } - public function store(Request $request) { $request->validate([ @@ -77,14 +48,12 @@ class GoodsCartController extends Controller $info = $user->carts()->create([ 'goods_id' => $goods->id, 'goods_sn' => $goods->goods_sn, - 'merchant_id' => $goods->merchant_id, 'amount' => $amount, 'goods_sku_id' => $sku ? $sku->id : null, 'goods_sku_sn' => $sku ? $sku->sn : null, 'goods_name' => $sku ? $sku->name : $goods->name, 'cover_image' => $goods->cover_image, 'price' => $sku ? $sku->price : $goods->price, - 'vip_price' => $sku ? $sku->vip_price : $goods->vip_price, 'attr' => $goods->attr, 'spec' => $sku ? $sku->spec : null, 'part' => $request->input('part'), diff --git a/src/Http/Resources/GoodsCartResource.php b/src/Http/Resources/GoodsCartResource.php index 13eba26..d1ff60e 100644 --- a/src/Http/Resources/GoodsCartResource.php +++ b/src/Http/Resources/GoodsCartResource.php @@ -3,7 +3,6 @@ namespace Peidikeji\Goods\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; -use Peidikeji\Merchant\Http\Resources\MerchantTinyResource; class GoodsCartResource extends JsonResource { @@ -13,9 +12,6 @@ class GoodsCartResource extends JsonResource 'id' => $this->id, 'user_id' => $this->user_id, - 'merchant_id' => $this->merchant_id, - 'mercahnt' => MerchantTinyResource::make($this->whenLoaded('merchant')), - 'goods_id' => $this->goods_id, 'goods_sn' => $this->goods_sn, 'goods' => GoodsTinyResource::make($this->whenLoaded('goods')), @@ -31,7 +27,6 @@ class GoodsCartResource extends JsonResource 'part' => $this->part, 'price' => floatval($this->price), - 'vip_price' => floatval($this->vip_price), 'amount' => $this->amount, ]; } diff --git a/src/Models/GoodsCart.php b/src/Models/GoodsCart.php index b841f76..e533f21 100644 --- a/src/Models/GoodsCart.php +++ b/src/Models/GoodsCart.php @@ -3,14 +3,13 @@ namespace Peidikeji\Goods\Models; use Illuminate\Database\Eloquent\Model; -use Peidikeji\Merchant\Models\Merchant; use Peidikeji\User\Models\User; class GoodsCart extends Model { protected $table = 'goods_cart'; - protected $fillable = ['amount', 'attr', 'cover_image', 'goods_id', 'merchant_id', 'goods_name', 'goods_sku_id', 'goods_sku_sn', 'goods_sn', 'part', 'price', 'spec', 'user_id', 'vip_price']; + protected $fillable = ['amount', 'attr', 'cover_image', 'goods_id', 'goods_name', 'goods_sku_id', 'goods_sku_sn', 'goods_sn', 'part', 'price', 'spec', 'user_id']; protected $casts = [ 'attr' => 'json', @@ -28,11 +27,6 @@ class GoodsCart extends Model return $this->belongsTo(Goods::class, 'goods_id'); } - public function merchant() - { - return $this->belongsTo(Merchant::class, 'merchant_id'); - } - public function goodsSku() { return $this->belongsTo(GoodsSku::class, 'goods_sku_id');