From fd3afc55959d84930ef0961ee623a2cf569d6f38 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Mon, 4 Jul 2022 17:09:09 +0800 Subject: [PATCH] fixed checkout --- .../Http/Controllers/CheckoutController.php | 4 +- beike/Shop/Services/CheckoutService.php | 118 ++++++++++-------- 2 files changed, 70 insertions(+), 52 deletions(-) diff --git a/beike/Shop/Http/Controllers/CheckoutController.php b/beike/Shop/Http/Controllers/CheckoutController.php index 4922da56..7b6d39d0 100644 --- a/beike/Shop/Http/Controllers/CheckoutController.php +++ b/beike/Shop/Http/Controllers/CheckoutController.php @@ -18,14 +18,14 @@ class CheckoutController extends Controller { public function index(Request $request) { - $data = CheckoutService::checkoutData(); + $data = (new CheckoutService)->checkoutData(); return view('checkout', $data); } public function update(Request $request) { $requestData = $request->all(); - $data = CheckoutService::update($requestData); + $data = (new CheckoutService)->update($requestData); return view('checkout', $data); } } diff --git a/beike/Shop/Services/CheckoutService.php b/beike/Shop/Services/CheckoutService.php index 331ae151..5148528c 100644 --- a/beike/Shop/Services/CheckoutService.php +++ b/beike/Shop/Services/CheckoutService.php @@ -11,21 +11,86 @@ namespace Beike\Shop\Services; -use Beike\Repositories\AddressRepo; +use Beike\Models\Cart; +use Beike\Models\Customer; use Beike\Repositories\PluginRepo; -use Beike\Repositories\SettingRepo; +use Beike\Repositories\AddressRepo; use Beike\Repositories\CountryRepo; use Beike\Shop\Http\Resources\Checkout\PaymentMethodItem; use Beike\Shop\Http\Resources\Checkout\ShippingMethodItem; class CheckoutService { + private $customer; + private $cart; + + public function __construct($customer = null) + { + if (is_int($customer) || empty($customer)) { + $this->customer = current_customer(); + } + if (empty($this->customer) && !($this->customer instanceof Customer)) { + throw new \Exception("购物车客户无效"); + } + $this->cart = Cart::query() + ->where('customer_id', $customer->id) + ->firstOrCreate(); + } + + /** + * 更新结账页数据 + * + * @param $requestData ['shipping_address_id'=>1, 'payment_address_id'=>2, 'shipping_method'=>'code', 'payment_method'=>'code'] + * @return array + */ + public function update($requestData): array + { + $shippingAddressId = $requestData['shipping_address_id'] ?? 0; + $paymentAddressId = $requestData['payment_address_id'] ?? 0; + $shippingMethod = $requestData['shipping_method'] ?? ''; + $paymentMethod = $requestData['payment_method'] ?? ''; + if ($shippingAddressId) { + $this->updateShippingAddressId($shippingAddressId); + } + if ($paymentAddressId) { + $this->updatePaymentAddressId($shippingAddressId); + } + if ($shippingMethod) { + $this->updateShippingMethod($shippingMethod); + } + if ($paymentMethod) { + $this->updatePaymentMethod($paymentMethod); + } + return $this->checkoutData(); + } + + + private function updateShippingAddressId($shippingAddressId) + { + $this->cart->update(['shipping_address_id', $shippingAddressId]); + } + + private function updatePaymentAddressId($paymentAddressId) + { + $this->cart->update(['payment_address_id', $paymentAddressId]); + } + + private function updateShippingMethod($shippingMethod) + { + $this->cart->update(['shipping_method_code', $shippingMethod]); + } + + private function updatePaymentMethod($paymentMethod) + { + $this->cart->update(['payment_method_code', $paymentMethod]); + } + /** * 获取结账页数据 * * @return array */ - public static function checkoutData(): array + public function checkoutData(): array { $customer = current_customer(); @@ -53,51 +118,4 @@ class CheckoutService ]; return $data; } - - /** - * 更新结账页数据 - * - * @param $requestData ['shipping_address_id'=>1, 'payment_address_id'=>2, 'shipping_method'=>'code', 'payment_method'=>'code'] - * @return array - */ - public static function update($requestData): array - { - $shippingAddressId = $requestData['shipping_address_id'] ?? 0; - $paymentAddressId = $requestData['payment_address_id'] ?? 0; - $shippingMethod = $requestData['shipping_method'] ?? ''; - $paymentMethod = $requestData['payment_method'] ?? ''; - if ($shippingAddressId) { - self::updateShippingAddressId($shippingAddressId); - } - if ($paymentAddressId) { - self::updatePaymentAddressId($shippingAddressId); - } - if ($shippingMethod) { - self::updateShippingMethod($shippingMethod); - } - if ($paymentMethod) { - self::updatePaymentMethod($paymentMethod); - } - return self::checkoutData(); - } - - private static function updateShippingAddressId($shippingAddressId) - { - - } - - private static function updatePaymentAddressId($shippingAddressId) - { - - } - - private static function updateShippingMethod($shippingAddressId) - { - - } - - private static function updatePaymentMethod($paymentMethod) - { - - } }