diff --git a/beike/Shop/Services/CartService.php b/beike/Shop/Services/CartService.php index dd7e7965..4a275c4a 100644 --- a/beike/Shop/Services/CartService.php +++ b/beike/Shop/Services/CartService.php @@ -145,14 +145,12 @@ class CartService $selected = collect($carts)->where('selected', 1); $quantity = $selected->sum('quantity'); $amount = $selected->sum('subtotal'); - $totalService = new TotalService($carts); $data = [ 'carts' => $carts, 'quantity' => $quantity, 'amount' => $amount, 'amount_format' => currency_format($amount), - 'totals' => $totalService->getTotals(), ]; return $data; } diff --git a/beike/Shop/Services/CheckoutService.php b/beike/Shop/Services/CheckoutService.php index c43ca928..5afd4ad7 100644 --- a/beike/Shop/Services/CheckoutService.php +++ b/beike/Shop/Services/CheckoutService.php @@ -122,6 +122,7 @@ class CheckoutService $cartList = CartService::list($customer, true); $carts = CartService::reloadData($cartList); + $totalService = (new TotalService($cartList))->setShippingMethod($this->cart->shipping_method_code); $data = [ 'current' => [ @@ -136,8 +137,10 @@ class CheckoutService 'addresses' => AddressResource::collection($addresses), 'shipping_methods' => $shipments, 'payment_methods' => $payments, - 'carts' => $carts + 'carts' => $carts, + 'totals' => $totalService->getTotals(), ]; + return $data; } } diff --git a/beike/Shop/Services/TotalService.php b/beike/Shop/Services/TotalService.php index d282916d..9ddcd193 100644 --- a/beike/Shop/Services/TotalService.php +++ b/beike/Shop/Services/TotalService.php @@ -19,32 +19,48 @@ class TotalService 'subtotal', 'tax', 'shipping', - 'total' + 'order_total' ]; public array $carts; public array $totals; + public float $amount = 0; + public string $shippingMethod = ''; public function __construct($carts) { $this->carts = $carts; } + + /** + * 设置配送方式 + */ + public function setShippingMethod($methodCode): TotalService + { + $this->shippingMethod = $methodCode; + return $this; + } + + /** * @return array */ public function getTotals(): array { - $totals = []; foreach (self::TOTAL_CODES as $code) { $serviceName = Str::studly($code) . 'Service'; $service = "\Beike\\Shop\\Services\\TotalServices\\{$serviceName}"; if (!class_exists($service) || !method_exists($service, 'getTotal')) { continue; } - $this->totals[] = $service::getTotal($this); + $totalData = $service::getTotal($this); + if ($totalData) { + $this->amount += $totalData['amount']; + $this->totals[] = $totalData; + } } - return $totals; + return $this->totals; } } diff --git a/beike/Shop/Services/TotalServices/OrderTotalService.php b/beike/Shop/Services/TotalServices/OrderTotalService.php new file mode 100644 index 00000000..0454dced --- /dev/null +++ b/beike/Shop/Services/TotalServices/OrderTotalService.php @@ -0,0 +1,28 @@ + + * @created 2022-07-27 17:49:15 + * @modified 2022-07-27 17:49:15 + */ + +namespace Beike\Shop\Services\TotalServices; + +use Beike\Shop\Services\TotalService; + +class OrderTotalService +{ + public static function getTotal(TotalService $totalService) + { + $amount = $totalService->amount; + return [ + 'code' => 'order_total', + 'title' => '应付总金额', + 'amount' => $amount, + 'amount_format' => currency_format($amount) + ]; + } +} diff --git a/beike/Shop/Services/TotalServices/ShippingService.php b/beike/Shop/Services/TotalServices/ShippingService.php index 6945b240..51bbdf37 100644 --- a/beike/Shop/Services/TotalServices/ShippingService.php +++ b/beike/Shop/Services/TotalServices/ShippingService.php @@ -12,10 +12,22 @@ namespace Beike\Shop\Services\TotalServices; +use Beike\Shop\Services\TotalService; + class ShippingService { - public static function getTotal() + public static function getTotal(TotalService $totalService) { - + $shippingMethod = $totalService->shippingMethod; + if (empty($shippingMethod)) { + return null; + } + $amount = 5; + return [ + 'code' => 'shipping', + 'title' => '运费', + 'amount' => $amount, + 'amount_format' => currency_format($amount) + ]; } } diff --git a/beike/Shop/Services/TotalServices/SubtotalService.php b/beike/Shop/Services/TotalServices/SubtotalService.php index c3d400dc..ef079d5b 100644 --- a/beike/Shop/Services/TotalServices/SubtotalService.php +++ b/beike/Shop/Services/TotalServices/SubtotalService.php @@ -12,14 +12,19 @@ namespace Beike\Shop\Services\TotalServices; +use Beike\Shop\Services\TotalService; + class SubtotalService { - public static function getTotal($totalService) + public static function getTotal(TotalService $totalService) { $carts = $totalService->carts; + $amount = collect($carts)->sum('subtotal'); return [ 'code' => 'sub_total', - 'value' => collect($carts)->sum('total') + 'title' => '商品总额', + 'amount' => $amount, + 'amount_format' => currency_format($amount) ]; } } diff --git a/beike/Shop/Services/TotalServices/TaxService.php b/beike/Shop/Services/TotalServices/TaxService.php index b8e1e9f6..1dd97409 100644 --- a/beike/Shop/Services/TotalServices/TaxService.php +++ b/beike/Shop/Services/TotalServices/TaxService.php @@ -12,10 +12,18 @@ namespace Beike\Shop\Services\TotalServices; +use Beike\Shop\Services\TotalService; + class TaxService { - public static function getTotal() + public static function getTotal(TotalService $totalService) { - + $amount = $totalService->amount * 0.02; + return [ + 'code' => 'shipping', + 'title' => '运费', + 'amount' => $amount, + 'amount_format' => currency_format($amount) + ]; } }