重构配送方式

This commit is contained in:
Edward Yang 2022-12-01 17:08:08 +08:00
parent 126755cf89
commit a6322880d0
5 changed files with 40 additions and 18 deletions

View File

@ -48,6 +48,7 @@ class Manager
$status = $plugin->getStatus(); $status = $plugin->getStatus();
$plugin->setDirname($dirname); $plugin->setDirname($dirname);
$plugin->setName(Arr::get($package, 'name')); $plugin->setName(Arr::get($package, 'name'));
$plugin->setDescription(Arr::get($package, 'description'));
$plugin->setInstalled(true); $plugin->setInstalled(true);
$plugin->setEnabled($status); $plugin->setEnabled($status);
$plugin->setVersion(Arr::get($package, 'version')); $plugin->setVersion(Arr::get($package, 'version'));

View File

@ -22,6 +22,7 @@ class Plugin implements Arrayable, \ArrayAccess
{ {
protected $path; protected $path;
protected $name; protected $name;
protected $description;
protected $packageInfo; protected $packageInfo;
protected $dirName; protected $dirName;
protected $installed; protected $installed;
@ -64,6 +65,13 @@ class Plugin implements Arrayable, \ArrayAccess
return $this; return $this;
} }
public function setDescription(string $description): Plugin
{
$this->description = $description;
return $this;
}
public function setInstalled(bool $installed): Plugin public function setInstalled(bool $installed): Plugin
{ {
$this->installed = $installed; $this->installed = $installed;
@ -127,6 +135,16 @@ class Plugin implements Arrayable, \ArrayAccess
} }
public function getName(): string
{
return $this->name;
}
public function getDescription(): string
{
return $this->description;
}
public function getDirname(): string public function getDirname(): string
{ {
return $this->dirName; return $this->dirName;

View File

@ -13,17 +13,18 @@ namespace Beike\Services;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Beike\Repositories\PluginRepo; use Beike\Repositories\PluginRepo;
use Beike\Shop\Services\CheckoutService;
class ShippingMethodService class ShippingMethodService
{ {
/** /**
* 获取配送方式, 二维数组, 一个配送插件对应多个配送方式 * 获取配送方式, 二维数组, 一个配送插件对应多个配送方式
* *
* @param $currentCart * @param CheckoutService $checkout
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getShippingMethods($currentCart): array public static function getShippingMethods(CheckoutService $checkout): array
{ {
$shippingPlugins = PluginRepo::getShippingMethods(); $shippingPlugins = PluginRepo::getShippingMethods();
@ -37,7 +38,7 @@ class ShippingMethodService
if (!method_exists($className, 'getQuotes')) { if (!method_exists($className, 'getQuotes')) {
throw new \Exception("请在插件 {$className} 实现方法: public function getQuotes(\$currentCart)"); throw new \Exception("请在插件 {$className} 实现方法: public function getQuotes(\$currentCart)");
} }
$quotes = (new $className)->getQuotes($currentCart, $shippingPlugin); $quotes = (new $className)->getQuotes($checkout, $plugin);
if ($quotes) { if ($quotes) {
$shippingMethods[] = [ $shippingMethods[] = [
'code' => $pluginCode, 'code' => $pluginCode,

View File

@ -27,9 +27,9 @@ use Beike\Shop\Http\Resources\Checkout\PaymentMethodItem;
class CheckoutService class CheckoutService
{ {
private $customer; public $customer;
private $cart; public $cart;
private $selectedProducts; public $selectedProducts;
/** /**
@ -167,13 +167,13 @@ class CheckoutService
$customer = $this->customer; $customer = $this->customer;
$currentCart = $this->cart; $currentCart = $this->cart;
$addresses = AddressRepo::listByCustomer($customer);
$shipments = ShippingMethodService::getShippingMethods($currentCart);
$payments = PaymentMethodItem::collection(PluginRepo::getPaymentMethods())->jsonSerialize();
$cartList = CartService::list($customer, true); $cartList = CartService::list($customer, true);
$carts = CartService::reloadData($cartList); $carts = CartService::reloadData($cartList);
$addresses = AddressRepo::listByCustomer($customer);
$shipments = ShippingMethodService::getShippingMethods($this);
$payments = PaymentMethodItem::collection(PluginRepo::getPaymentMethods())->jsonSerialize();
$totalService = (new TotalService($currentCart, $cartList)); $totalService = (new TotalService($currentCart, $cartList));
$data = [ $data = [

View File

@ -11,26 +11,28 @@
namespace Plugin\FlatShipping; namespace Plugin\FlatShipping;
use Beike\Plugin\Plugin;
use Beike\Shop\Services\CheckoutService;
class Bootstrap class Bootstrap
{ {
/** /**
* 获取固定运费方式 * 获取固定运费方式
* *
* @param $currentCart * @param CheckoutService $checkout
* @param $shippingPlugin * @param Plugin $plugin
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public function getQuotes($currentCart, $shippingPlugin): array public function getQuotes(CheckoutService $checkout, Plugin $plugin): array
{ {
$code = 'flat_shipping'; $code = $plugin->code;
$pluginSetting = $shippingPlugin->plugin;
$quotes[] = [ $quotes[] = [
'type' => 'shipping', 'type' => 'shipping',
'code' => "{$code}.0", 'code' => "{$code}.0",
'name' => $pluginSetting->name, 'name' => $plugin->getName(),
'description' => $pluginSetting->description, 'description' => $plugin->getDescription(),
'icon' => plugin_resize($code, $pluginSetting->icon), 'icon' => plugin_resize($code, $plugin->icon),
]; ];
return $quotes; return $quotes;
} }