Procházet zdrojové kódy

Wesmiler 三勾多商户商城提交

wesmiler před 3 roky
rodič
revize
e3f110f3ac

+ 1 - 1
app/api/controller/product/Product.php

@@ -51,7 +51,7 @@ class Product extends Controller
         $model = new ProductModel;
         $list = $model->getList($param, $this->getUser(false));
         $cartModel = new CartModel($this->getUser());
-        $carts = $cartModel->getList([]);
+        $carts = $cartModel->getCartProductList();
         return $this->renderSuccess('', compact('list','carts'));
     }
 

+ 27 - 0
app/api/model/order/Cart.php

@@ -124,6 +124,33 @@ class Cart
         return $supplierData;
     }
 
+    /**
+     * 获取购物车中所有商品列表
+     * @return array|mixed
+     */
+    public function getCartProductList(){
+        $carts = Cache::get('cart_products_' . $this->user_id)? : [];
+        if($carts){
+            return $carts;
+        }
+
+        $cartList = $this->getList([]);
+        $carts = [];
+        if($cartList){
+            foreach ($cartList as $item){
+                if($item['productList']){
+                    foreach ($item['productList'] as $v){
+                        $carts[$v['product_id']] = $v;
+                    }
+                }
+            }
+        }
+        if($carts){
+            Cache::set('cart_products_' . $this->user_id, $carts, rand(10, 30));
+        }
+        return $carts;
+    }
+
     private function getProductBySupplier($supplierId, $productList){
         $result = [];
         foreach ($productList as $product){