From a1b1f6ba15197e218053e57c6d09a9d3c865cd6a Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Fri, 3 Mar 2023 16:52:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BA=93=E5=AD=98=E9=97=AE?= =?UTF-8?q?=E9=A2=98:=20=E5=BD=93=E8=B4=AD=E7=89=A9=E8=BD=A6=E4=B8=AD?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=8A=A0=E4=B8=8A=E8=AF=A6=E6=83=85=E9=A1=B5?= =?UTF-8?q?=E8=B4=AD=E4=B9=B0=E5=95=86=E5=93=81=E5=A4=A7=E4=BA=8E=E5=BA=93?= =?UTF-8?q?=E5=AD=98=E6=97=B6=EF=BC=8C=E5=8F=AF=E4=B8=8B=E5=8D=95=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Plugin/Asset.php | 23 ++++++------- .../Shop/Http/Controllers/CartController.php | 32 +++++++++++-------- .../Http/Controllers/PluginController.php | 4 ++- beike/Shop/Services/CartService.php | 13 ++++++++ 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/beike/Plugin/Asset.php b/beike/Plugin/Asset.php index 0279f3d3..ef32adc9 100644 --- a/beike/Plugin/Asset.php +++ b/beike/Plugin/Asset.php @@ -18,19 +18,19 @@ class Asset { private $pluginPath; - const CONTENT_TYPES = [ - 'js' => 'application/javascript', - 'css' => 'text/css', - 'jpg' => 'image/jpeg', + public const CONTENT_TYPES = [ + 'js' => 'application/javascript', + 'css' => 'text/css', + 'jpg' => 'image/jpeg', 'apng' => 'image/apng', 'avif' => 'image/avif', - 'gif' => 'image/gif', + 'gif' => 'image/gif', 'jpeg' => 'image/jpeg', - 'png' => 'image/png', - 'svg' => 'image/svg+xml', + 'png' => 'image/png', + 'svg' => 'image/svg+xml', 'webp' => 'image/webp', 'webm' => 'video/webm', - 'ogg' => 'video/ogg', + 'ogg' => 'video/ogg', ]; public function __construct($pluginCode) @@ -38,7 +38,7 @@ class Asset if (empty($pluginCode)) { throw new \Exception('Empty plugin code!'); } - $folderName = Str::studly($pluginCode); + $folderName = Str::studly($pluginCode); $this->pluginPath = base_path('plugins/' . $folderName); } @@ -47,7 +47,6 @@ class Asset return new self($pluginCode); } - /** * Get content and type * @@ -59,11 +58,13 @@ class Asset $filePath = $this->pluginPath . '/Static/' . $file; if (is_file($filePath)) { $extension = File::extension($filePath); + return [ - 'type' => self::CONTENT_TYPES[$extension] ?? '', + 'type' => self::CONTENT_TYPES[$extension] ?? '', 'content' => file_get_contents($filePath), ]; } + return []; } } diff --git a/beike/Shop/Http/Controllers/CartController.php b/beike/Shop/Http/Controllers/CartController.php index ea43158a..3fe5d061 100644 --- a/beike/Shop/Http/Controllers/CartController.php +++ b/beike/Shop/Http/Controllers/CartController.php @@ -54,23 +54,27 @@ class CartController extends Controller */ public function store(CartRequest $request) { - $skuId = $request->sku_id; - $quantity = $request->quantity ?? 1; - $buyNow = (bool) $request->buy_now ?? false; - $customer = current_customer(); + try { + $skuId = $request->sku_id; + $quantity = $request->quantity ?? 1; + $buyNow = (bool) $request->buy_now ?? false; + $customer = current_customer(); - $sku = ProductSku::query() - ->whereRelation('product', 'active', '=', true) - ->findOrFail($skuId); + $sku = ProductSku::query() + ->whereRelation('product', 'active', '=', true) + ->findOrFail($skuId); - $cart = CartService::add($sku, $quantity, $customer); - if ($buyNow) { - CartService::select($customer, [$cart->id]); + $cart = CartService::add($sku, $quantity, $customer); + if ($buyNow) { + CartService::select($customer, [$cart->id]); + } + + $cart = hook_filter('cart.store.data', $cart); + + return json_success(trans('shop/carts.added_to_cart'), $cart); + } catch (\Exception $e) { + return json_fail($e->getMessage()); } - - $cart = hook_filter('cart.store.data', $cart); - - return json_success(trans('shop/carts.added_to_cart'), $cart); } /** diff --git a/beike/Shop/Http/Controllers/PluginController.php b/beike/Shop/Http/Controllers/PluginController.php index d884800e..64cdcd9d 100644 --- a/beike/Shop/Http/Controllers/PluginController.php +++ b/beike/Shop/Http/Controllers/PluginController.php @@ -21,13 +21,15 @@ class PluginController extends Controller $contents = Asset::getInstance($code)->getContent($path); $content = $contents['content'] ?? ''; - $type = $contents['type'] ?? ''; + $type = $contents['type'] ?? ''; if ($content && $type) { $response = Response::make($content); $response->header('Content-Type', $type); + return $response; } + return ''; } } diff --git a/beike/Shop/Services/CartService.php b/beike/Shop/Services/CartService.php index 2f663d15..dfa6aea4 100644 --- a/beike/Shop/Services/CartService.php +++ b/beike/Shop/Services/CartService.php @@ -40,6 +40,13 @@ class CartService $item->delete(); } + $cartQuantity = $item->quantity; + $skuQuantity = $item->sku->quantity; + if ($cartQuantity > $skuQuantity) { + $item->quantity = $skuQuantity; + $item->save(); + } + return $description && $product; }); @@ -82,6 +89,12 @@ class CartService ]); } + $cartQuantity = $cart->quantity; + $skuQuantity = $cart->sku->quantity; + if ($cartQuantity > $skuQuantity) { + throw new \Exception(trans('cart.stock_out')); + } + return $cart; }