fixed carts
This commit is contained in:
parent
17e49723d6
commit
79b44a5bc6
|
|
@ -4,40 +4,51 @@ namespace Beike\Shop\Http\Controllers;
|
|||
|
||||
use Beike\Models\ProductSku;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Beike\Shop\Services\CartService;
|
||||
|
||||
class CartController extends Controller
|
||||
{
|
||||
public function index()
|
||||
/**
|
||||
* @return View
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
$carts = CartService::list(current_customer());
|
||||
$amount = collect($carts)->sum('subtotal');
|
||||
$data = [
|
||||
'carts' => $carts,
|
||||
'quantity' => collect($carts)->sum('quantity'),
|
||||
'amount' => $amount,
|
||||
'amount_format' => currency_format($amount)
|
||||
];
|
||||
$data = CartService::reloadData();
|
||||
return view("cart", $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 选中购物车商品
|
||||
*
|
||||
* POST /carts/select {sku_ids:[product_sku_id, product_sku_id]}
|
||||
* @param Request $request
|
||||
* @return View
|
||||
*/
|
||||
public function select(Request $request)
|
||||
public function select(Request $request): View
|
||||
{
|
||||
$productSkuIds = $request->get('sku_ids');
|
||||
$customer = current_customer();
|
||||
CartService::select($customer, $productSkuIds);
|
||||
|
||||
$data = CartService::reloadData();
|
||||
return view("cart", $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* PUT /carts/{cart_id} {quantity: 123}
|
||||
* @param Request $request
|
||||
* @param $cartId
|
||||
* @return View
|
||||
*/
|
||||
public function update(Request $request)
|
||||
public function update(Request $request, $cartId): View
|
||||
{
|
||||
$customer = current_customer();
|
||||
$quantity = $request->get('quantity');
|
||||
CartService::updateQuantity($customer, $cartId, $quantity);
|
||||
|
||||
$data = CartService::reloadData();
|
||||
return view("cart", $data);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -18,10 +18,12 @@ use Illuminate\Support\Facades\Hash;
|
|||
class AccountService
|
||||
{
|
||||
/**
|
||||
* 注册用户
|
||||
*
|
||||
* @param array $data // ['email', 'password']
|
||||
* @return int
|
||||
*/
|
||||
public static function register(array $data)
|
||||
public static function register(array $data): int
|
||||
{
|
||||
$data['password'] = Hash::make($data['password']);
|
||||
$data['customer_group_id'] = setting('default_customer_group_id', 1); // default_customer_group_id为默认客户组名称
|
||||
|
|
|
|||
|
|
@ -11,11 +11,18 @@
|
|||
|
||||
namespace Beike\Shop\Services;
|
||||
|
||||
use Exception;
|
||||
use Beike\Models\Cart;
|
||||
use Beike\Shop\Http\Resources\CartList;
|
||||
|
||||
class CartService
|
||||
{
|
||||
/**
|
||||
* 获取购物车商品列表
|
||||
*
|
||||
* @param $customer
|
||||
* @return array
|
||||
*/
|
||||
public static function list($customer): array
|
||||
{
|
||||
if (empty($customer)) {
|
||||
|
|
@ -30,6 +37,10 @@ class CartService
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建购物车或者更新购物车数量
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function add($sku, int $quantity, $customer = null)
|
||||
{
|
||||
$customerId = $customer->id ?? 0;
|
||||
|
|
@ -37,7 +48,7 @@ class CartService
|
|||
$skuId = $sku->id;
|
||||
|
||||
if (empty($sku)) {
|
||||
throw new \Exception("无效的SKU ID");
|
||||
throw new Exception("无效的SKU ID");
|
||||
}
|
||||
$cart = Cart::query()
|
||||
->where('customer_id', $customerId)
|
||||
|
|
@ -59,4 +70,51 @@ class CartService
|
|||
}
|
||||
return $cart;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 选择购物车商品
|
||||
*
|
||||
* @param $customer
|
||||
* @param $productSkuIds
|
||||
*/
|
||||
public static function select($customer, $productSkuIds)
|
||||
{
|
||||
Cart::query()->where('customer_id', $customer->id)
|
||||
->whereIn('product_sku_id', $productSkuIds)
|
||||
->update(['selected', 1]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新购物车数量
|
||||
*/
|
||||
public static function updateQuantity($customer, $cartId, $quantity)
|
||||
{
|
||||
Cart::query()->where('customer_id', $customer->id)
|
||||
->where('id', $cartId)
|
||||
->update(['quantity', $quantity]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取购物车相关数据
|
||||
*
|
||||
* @param array $carts
|
||||
* @return array
|
||||
*/
|
||||
public static function reloadData(array $carts = []): array
|
||||
{
|
||||
if (empty($carts)) {
|
||||
$carts = CartService::list(current_customer());
|
||||
}
|
||||
$amount = collect($carts)->sum('subtotal');
|
||||
$data = [
|
||||
'carts' => $carts,
|
||||
'quantity' => collect($carts)->sum('quantity'),
|
||||
'amount' => $amount,
|
||||
'amount_format' => currency_format($amount)
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue