goods-cart
parent
53fe51018b
commit
523934e7e9
|
|
@ -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('配件');
|
||||
|
|
|
|||
|
|
@ -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']);
|
||||
|
||||
|
|
|
|||
|
|
@ -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'),
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
Loading…
Reference in New Issue