WIN-2308041133\Administrator hai 7 horas
pai
achega
dff0d313e6
Modificáronse 2 ficheiros con 10 adicións e 7 borrados
  1. 4 3
      app/api/controller/Cart.php
  2. 6 4
      app/model/api/StoreCart.php

+ 4 - 3
app/api/controller/Cart.php

@@ -25,6 +25,7 @@ class Cart extends BaseController
     /**
      * 添加购物车
      * @param Request $request
+     * @return \think\response\Json
      */
     public function add(Request $request)
     {
@@ -57,10 +58,10 @@ class Cart extends BaseController
             'create_time' => time(),
         ];
 
-        $result = (new StoreCart())->addToCart($data);
+        $cartId = (new StoreCart())->addToCart($data);
 
-        if ($result) {
-            return app('json')->success('添加成功');
+        if ($cartId) {
+            return app('json')->success('添加成功', ['id' => $cartId]);
         } else {
             return app('json')->fail('添加失败');
         }

+ 6 - 4
app/model/api/StoreCart.php

@@ -31,21 +31,23 @@ class StoreCart extends Model
     /**
      * 添加购物车
      * @param array $data
-     * @return bool
+     * @return int|false 成功返回购物车ID,失败返回false
      */
     public function addToCart($data)
     {
         // 检查是否已存在
         $exist = $this->where('uid', $data['uid'])
             ->where('product_id', $data['product_id'])
-            ->where('cart_num', $data['cart_num'])
+            ->where('product_attr_unique', $data['product_attr_unique'] ?? '')
             ->find();
         if ($exist) {
             // 更新数量
-            return $this->where('id', $exist['id'])->inc('cart_num', $data['cart_num'])->update();
+            $result = $this->where('id', $exist['id'])->inc('cart_num', $data['cart_num'])->update();
+            return $result ? $exist['id'] : false;
         } else {
             // 新增
-            return $this->insert($data);
+            $result = $this->insert($data);
+            return $result ? $this->getLastInsID() : false;
         }
     }