diff --git a/beike/Shop/Http/Controllers/ProductController.php b/beike/Shop/Http/Controllers/ProductController.php index 3944112e..d4004cc8 100644 --- a/beike/Shop/Http/Controllers/ProductController.php +++ b/beike/Shop/Http/Controllers/ProductController.php @@ -2,10 +2,19 @@ namespace Beike\Shop\Http\Controllers; +use Beike\Models\Cart; +use Beike\Models\CartProduct; use Beike\Models\Product; +use Beike\Models\ProductSku; +use Beike\Repositories\AddressRepo; +use Beike\Repositories\CartRepo; +use Beike\Repositories\PluginRepo; use Beike\Repositories\ProductRepo; +use Beike\Services\ShippingMethodService; use Beike\Shop\Http\Resources\ProductDetail; use Beike\Shop\Http\Resources\ProductSimple; +use Beike\Shop\Services\CartService; +use Beike\Shop\Services\CheckoutService; use Illuminate\Http\Request; class ProductController extends Controller @@ -22,6 +31,7 @@ class ProductController extends Controller $product = ProductRepo::getProductDetail($product); $data = [ + 'product_id' => $product->id, 'product' => (new ProductDetail($product))->jsonSerialize(), 'relations' => ProductRepo::getProductsByIds($relationIds)->jsonSerialize(), ]; @@ -56,4 +66,76 @@ class ProductController extends Controller return view('search', $data); } + + + /** + * Common: 计算当前商品订单金额信息 + * Author: wu-hui + * Time: 2023/08/18 10:37 + * @param Request $request + * @return array|\Illuminate\Http\JsonResponse + * @throws \Exception + */ + public function computeOrderMoney(Request $request){ + // 参数获取 + $list = json_decode($request->list,TRUE) ?? ''; + if(!is_array($list)) return json_fail(trans('shop/products.buy_sku_error')); + // 生成模拟数据 + $diyData = []; + foreach($list as $item){ + $diyData[] = [ + 'id' => 0, + 'selected' => 1, + 'product_id' => $item['product_id'], + 'product_sku_id' => $item['product_sku_id'], + 'quantity' => $item['quantity'], + 'product' => Product::where('id',40)->first()->toArray(), + 'sku' => ProductSku::where('id',641)->first()->toArray(), + ]; + } + $cartItems = collect($diyData)->mapInto(CartProduct::class); + $cartList = CartService::cartListHandle($cartItems); + // 价格计算 + $customer = current_customer(); + $customerId = $customer->id ?? 0; + $sessionId = session()->getId(); + $defaultAddress = AddressRepo::listByCustomer($customer)->first(); + $defaultAddressId = $defaultAddress->id ?? 0; + $shippingMethod = PluginRepo::getShippingMethods()->first(); + $paymentMethod = PluginRepo::getPaymentMethods()->first(); + $shippingMethodCode = $shippingMethod->code ?? ''; + $cart = collect([[ + 'customer_id' => $customerId, + 'session_id' => $sessionId, + 'shipping_address_id' => $defaultAddressId, + 'shipping_method_code' => $shippingMethodCode ? $shippingMethodCode . '.0' : '', + 'payment_address_id' => $defaultAddressId, + 'payment_method_code' => $paymentMethod->code ?? '', + // "id" => 28, + // "customer_id" => 0, + // "session_id" => "Chwm3kGAY5YX4CC58yTKWREvSoplb9gFXl2UNnsb", + // "shipping_address_id" => 0, + // "guest_shipping_address" => null, + // "shipping_method_code" => "", + // "payment_address_id" => 0, + // "guest_payment_address" => null, + // "payment_method_code" => "paypal", + // "extra" => null, + // "created_at" => "2023-08-18 01:35:12", + // "updated_at" => "2023-08-18 01:35:12", + // "shipping_address" => null, + // "payment_address" => null, + ]])->mapInto(Cart::class); + $cart = $cart[0]; + // 计算 + $checkoutService = new CheckoutService(); + $totalClass = hook_filter('service.checkout.total_service','Beike\Shop\Services\TotalService'); + $checkoutService->totalService = (new $totalClass($cart,$cartList)); + $checkoutData = $checkoutService->checkoutData(); + $totals = $checkoutData['totals']; + + return json_success(trans('common.success'),$totals); + } + + } diff --git a/beike/Shop/Routes/shop.php b/beike/Shop/Routes/shop.php index d9418ca0..d3c20b98 100644 --- a/beike/Shop/Routes/shop.php +++ b/beike/Shop/Routes/shop.php @@ -65,6 +65,7 @@ Route::prefix('/') Route::get('products/search', [ProductController::class, 'search'])->name('products.search'); Route::get('products/{product}', [ProductController::class, 'show'])->name('products.show'); + Route::post('products/computeOrderMoney', [ProductController::class, 'computeOrderMoney'])->name('products.computeOrderMoney'); Route::get('register', [RegisterController::class, 'index'])->name('register.index'); Route::post('register', [RegisterController::class, 'store'])->name('register.store'); diff --git a/beike/Shop/Services/CartService.php b/beike/Shop/Services/CartService.php index c3dd03d0..0ba7f8ad 100644 --- a/beike/Shop/Services/CartService.php +++ b/beike/Shop/Services/CartService.php @@ -11,7 +11,10 @@ namespace Beike\Shop\Services; +use Beike\Models\Cart; use Beike\Models\CartProduct; +use Beike\Models\Product; +use Beike\Models\ProductSku; use Beike\Repositories\CartRepo; use Beike\Shop\Http\Resources\CartDetail; use Exception; @@ -27,18 +30,22 @@ class CartService * @param bool $selected * @return array */ - public static function list($customer, bool $selected = false): array - { - if (self::$cartList !== null) { - return self::$cartList; - } - + public static function list($customer, bool $selected = false): array{ + if (self::$cartList !== null) return self::$cartList; $cartBuilder = CartRepo::allCartProductsBuilder($customer->id ?? 0); - if ($selected) { - $cartBuilder->where('selected', true); - } + if ($selected) $cartBuilder->where('selected', true); $cartItems = $cartBuilder->get(); + return self::cartListHandle($cartItems); + } + /** + * Common: 处理指定的购物车商品 + * Author: wu-hui + * Time: 2023/08/17 17:28 + * @param $cartItems + * @return mixed + */ + public static function cartListHandle($cartItems){ $cartItems = $cartItems->filter(function ($item) { $description = $item->sku->product->description ?? ''; $product = $item->product ?? null; @@ -57,7 +64,6 @@ class CartService return $description && $product; }); - $productQuantitySumList = []; foreach($cartItems as $item) { $productId = $item->product_id; @@ -206,4 +212,5 @@ class CartService return hook_filter('service.cart.data', $data); } + } diff --git a/themes/default/product/product.blade.php b/themes/default/product/product.blade.php index 6adf7414..72b48520 100644 --- a/themes/default/product/product.blade.php +++ b/themes/default/product/product.blade.php @@ -308,7 +308,7 @@ {{--价格实时计算--}}