diff --git a/beike/Services/ShippingMethodService.php b/beike/Services/ShippingMethodService.php new file mode 100644 index 00000000..e57fccf2 --- /dev/null +++ b/beike/Services/ShippingMethodService.php @@ -0,0 +1,46 @@ + + * @created 2022-12-01 10:54:46 + * @modified 2022-12-01 10:54:46 + */ + +namespace Beike\Services; + +use Illuminate\Support\Str; +use Beike\Repositories\PluginRepo; + +class ShippingMethodService +{ + /** + * 获取配送方式, 二维数组, 一个配送插件对应多个配送方式 + * + * @param $currentCart + * @return array + * @throws \Exception + */ + public static function getShippingMethods($currentCart): array + { + $shippingPlugins = PluginRepo::getShippingMethods(); + + $shippingMethods = []; + foreach ($shippingPlugins as $shippingPlugin) { + $pluginCode = $shippingPlugin->code; + $pluginName = Str::studly($pluginCode); + $className = "Plugin\\{$pluginName}\\Bootstrap"; + + if (!method_exists($className, 'getQuotes')) { + throw new \Exception("请在插件 {$className} 实现方法: public function getQuotes(\$currentCart)"); + } + $quotes = (new $className)->getQuotes($currentCart, $shippingPlugin); + if ($quotes) { + $shippingMethods[$pluginCode] = $quotes; + } + } + return $shippingMethods; + } +} diff --git a/beike/Shop/Services/CheckoutService.php b/beike/Shop/Services/CheckoutService.php index 7c0eea72..71db93ca 100644 --- a/beike/Shop/Services/CheckoutService.php +++ b/beike/Shop/Services/CheckoutService.php @@ -21,9 +21,9 @@ use Beike\Repositories\PluginRepo; use Beike\Repositories\AddressRepo; use Beike\Repositories\CountryRepo; use Beike\Services\StateMachineService; +use Beike\Services\ShippingMethodService; use Beike\Shop\Http\Resources\Account\AddressResource; use Beike\Shop\Http\Resources\Checkout\PaymentMethodItem; -use Beike\Shop\Http\Resources\Checkout\ShippingMethodItem; class CheckoutService { @@ -159,6 +159,7 @@ class CheckoutService * 获取结账页数据 * * @return array + * @throws \Exception */ public function checkoutData(): array { @@ -166,7 +167,7 @@ class CheckoutService $currentCart = $this->cart; $addresses = AddressRepo::listByCustomer($customer); - $shipments = ShippingMethodItem::collection(PluginRepo::getShippingMethods())->jsonSerialize(); + $shipments = ShippingMethodService::getShippingMethods($currentCart); $payments = PaymentMethodItem::collection(PluginRepo::getPaymentMethods())->jsonSerialize(); $cartList = CartService::list($customer, true); diff --git a/plugins/FlatShipping/Bootstrap.php b/plugins/FlatShipping/Bootstrap.php index cd53e15c..fe243745 100644 --- a/plugins/FlatShipping/Bootstrap.php +++ b/plugins/FlatShipping/Bootstrap.php @@ -11,12 +11,31 @@ namespace Plugin\FlatShipping; +use Beike\Shop\Http\Resources\Checkout\ShippingMethodItem; + class Bootstrap { /** - * @return float + * 获取固定运费方式 + * + * @param $currentCart + * @param $shippingPlugin + * @return array */ - public function getShippingFee($totalService) + public function getQuotes($currentCart, $shippingPlugin): array + { + $quotes['flat_shipping.0'] = (new ShippingMethodItem($shippingPlugin))->jsonSerialize(); + return $quotes; + } + + + /** + * 计算固定运费 + * + * @param $totalService + * @return float|int + */ + public function getShippingFee($totalService): float|int { $amount = $totalService->amount; $shippingType = plugin_setting('flat_shipping.type', 'fixed');