'require|checkCart', 'cart_ids' => 'require|array', 'goods_num' => 'require|integer|gt:0', 'item_id' => 'require|checkGoods', 'selected' => 'require|in:0,1', ]; protected $message = [ 'item_id' => '请选择商品', 'goods_num.require' => '商品数量不能为0', 'goods_num.gt' => '商品数量需大于0', 'goods_num.integer' => '商品数量需为整数', 'cart_id.require' => '参数错误', 'selected.require' => '参数错误', 'selected.in' => '参数错误', ]; protected $scene = [ 'del' => ['cart_id'], 'delIds' => ['cart_ids'], 'editSku' => [ 'cart_id', 'item_id', 'goods_num' ], 'add' => ['item_id', 'goods_num'], 'selected' => ['cart_id', 'selected'], 'change' => ['cart_id', 'goods_num'], ]; /** * @notes 验证购物车是否存在 * @param $value * @param $rule * @param $data * @return bool|string * @author 段誉 * @date 2021/7/16 18:32 */ protected function checkCart($value, $rule, $data) { $cart = (new Cart())->getCartById($value, $data['user_id']); request()->_api_validate_cart_detail = $cart; if ($cart->isEmpty()) { return '购物车不存在'; } return true; } /** * @notes 验证商品 * @param $value * @param $rule * @param $data * @return bool|string * @author 段誉 * @date 2021/7/16 18:31 */ protected function checkGoods($value, $rule, $data) { $goods = (new Goods())->alias('g') ->field(['g.status', 'g.delete_time', 'gi.stock', 'g.id as goods_id']) ->join('goods_item gi', 'gi.goods_id = g.id') ->where(['gi.id' => $value]) ->find(); if (empty($goods) || $goods['delete_time'] > 0) { return '找不到商品'; } if ($this->currentScene == 'editSku') { $cart = request()->_api_validate_cart_detail; if ($cart['goods_id'] != $goods['goods_id']) { return '找不到商品'; } } if ($goods['status'] == 0) { return '商品不能购买'; } if (CommonPresellLogic::checkGoodsHas($goods['id'])) { return '预售活动商品不能加入购物车'; } $cart = (new Cart())->getCartByItem($data['item_id'], $data['user_id']); $cartNum = $data['goods_num'] + $cart['goods_num'] ?? 0; if ($goods['stock'] < $cartNum) { return '商品库存不足'; } return true; } }