Browse Source

fix:购物车增加是否选中

super-yimizi 1 month ago
parent
commit
65272af6a0

+ 25 - 0
application/api/controller/Cart.php

@@ -149,4 +149,29 @@ class Cart extends Base
         $total = Carts::where('user_id', $this->auth->id)->count();
         $total = Carts::where('user_id', $this->auth->id)->count();
         $this->success('', $total);
         $this->success('', $total);
     }
     }
+
+     /**
+     * 选中或取消商品
+     * 
+     */
+    public function checked()
+    {
+        $cartIds  =  $this->request->post('cartIds/a');
+        $isChecked = $this->request->post('isChecked/d');
+        $userId = $this->auth->id;
+        // 验证器
+        $validate = new CartValidate();
+        $validate->scene('checked')->check([
+            'cartIds' => $cartIds,
+            'isChecked' => $isChecked,
+        ]);
+        if ($validate->getError()) {
+            $this->error($validate->getError());
+        }
+        CartService::updateChecked(
+            $userId,
+            $cartIds,
+            $isChecked == 1);
+        $this->success('操作成功');
+    }
 }
 }

+ 8 - 0
application/api/validate/Cart.php

@@ -13,6 +13,8 @@ class Cart extends Validate
         'goods_id'     => 'require|integer|gt:0',
         'goods_id'     => 'require|integer|gt:0',
         'goods_sku_id' => 'integer|egt:0',
         'goods_sku_id' => 'integer|egt:0',
         'nums'         => 'require|integer|gt:0',
         'nums'         => 'require|integer|gt:0',
+        'cartIds'      => 'require|array|min:1',
+        'isChecked'    => 'require|in:0,1',
     ];
     ];
 
 
     /**
     /**
@@ -27,6 +29,11 @@ class Cart extends Validate
         'nums.require'      => '商品数量不能为空',
         'nums.require'      => '商品数量不能为空',
         'nums.integer'      => '商品数量必须是整数',
         'nums.integer'      => '商品数量必须是整数',
         'nums.gt'           => '商品数量必须大于0',
         'nums.gt'           => '商品数量必须大于0',
+        'cartIds.require'   => '购物车ID不能为空',
+        'cartIds.array'     => '购物车ID必须为数组',
+        'cartIds.min'       => '至少选择一个购物车项',
+        'isChecked.require' => '选中状态不能为空',
+        'isChecked.in'      => '选中状态只能是0或1',
     ];
     ];
 
 
     /**
     /**
@@ -35,5 +42,6 @@ class Cart extends Validate
     protected $scene = [
     protected $scene = [
         'add'       => ['goods_id', 'goods_sku_id', 'nums'],
         'add'       => ['goods_id', 'goods_sku_id', 'nums'],
         'set_nums'  => ['nums'],
         'set_nums'  => ['nums'],
+        'checked'   => ['cartIds', 'isChecked'],
     ];
     ];
 } 
 } 

+ 7 - 0
application/common/Service/CartService.php

@@ -136,4 +136,11 @@ class CartService
         }
         }
     }
     }
 
 
+    public static function updateChecked($userId, $cartIds=[], $isChecked)
+    {
+        return Carts::where('user_id', $userId)
+            ->whereIn('id', $cartIds)
+            ->update(['checked' => $isChecked]);
+    }
+
 }
 }

+ 7 - 0
application/common/service/CartService.php

@@ -136,4 +136,11 @@ class CartService
         }
         }
     }
     }
 
 
+    public static function updateChecked($userId, $cartIds=[], $isChecked)
+    {
+        return Carts::where('user_id', $userId)
+            ->whereIn('id', $cartIds)
+            ->update(['checked' => $isChecked]);
+    }
+
 }
 }