fixed cart

This commit is contained in:
Edward Yang 2022-06-28 11:51:27 +08:00
parent d421521dbf
commit 4872dc48f9
4 changed files with 32 additions and 6 deletions

View File

@ -1,6 +1,7 @@
<?php
use Beike\Models\AdminUser;
use Beike\Models\Customer;
use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\Authenticatable;
@ -46,11 +47,21 @@ function shop_route($route, $params = []): string
*
* @return Authenticatable|null
*/
function logged_admin_user(): ?Authenticatable
function current_user(): ?Authenticatable
{
return auth()->guard(AdminUser::AUTH_GUARD)->user();
}
/**
* 获取前台当前登录客户
*
* @return Authenticatable|null
*/
function current_customer(): ?Authenticatable
{
return auth()->guard(Customer::AUTH_GUARD)->user();
}
/**
* 获取缩略图
*

View File

@ -3,8 +3,8 @@
namespace Beike\Shop\Http\Controllers;
use Beike\Models\ProductSku;
use Beike\Services\CartService;
use Illuminate\Http\Request;
use Beike\Shop\Services\CartService;
class CartController extends Controller
{
@ -12,13 +12,20 @@ class CartController extends Controller
{
$skuId = $request->sku_id;
$quantity = $request->quantity ?? 1;
$customer = current_customer();
$sku = ProductSku::query()
->whereRelation('product', 'active', '=', true)
->findOrFail($skuId);
$cart = (new CartService)->add($sku, $quantity);
$cart = CartService::add($customer, $sku, $quantity);
return $cart;
}
public function miniCart()
{
$customer = current_customer();
return CartService::list($customer);
}
}

View File

@ -18,6 +18,7 @@ Route::prefix('/')
Route::get('/', [HomeController::class, 'index'])->name('home.index');
Route::post('carts', [CartController::class, 'store'])->name('carts.store');
Route::get('carts/mini', [CartController::class, 'miniCart'])->name('carts.mini');
Route::get('categories', [CategoryController::class, 'index'])->name('categories.index');
Route::get('categories/{category}', [CategoryController::class, 'show'])->name('categories.show');

View File

@ -9,7 +9,7 @@
* @modified 2022-01-05 10:12:57
*/
namespace Beike\Services;
namespace Beike\Shop\Services;
use Beike\Models\Cart;
@ -17,10 +17,17 @@ use Beike\Models\ProductSku;
class CartService
{
public function add(ProductSku $sku, int $quantity)
public static function list($customer)
{
$cartList = Cart::query()->where('customer_id', $customer->id)->get();
return $cartList;
}
public static function add($customer, ProductSku $sku, int $quantity)
{
$cart = Cart::query()->create([
'customer_id' => 0,
'customer_id' => $customer->id,
'product_id' => $sku->product_id,
'product_sku_id' => $sku->id,
'quantity' => $quantity,