request->post('goods_id'); //单规格使用 $goods_sku_id = $this->request->post('goods_sku_id'); //多规格使用 $nums = $this->request->post('nums/d', 1); // 增加验证器 $validate = new CartValidate(); $validate->scene('add')->check([ 'goods_id' => $goods_id, 'goods_sku_id' => $goods_sku_id, 'nums' => $nums, ]); if ($validate->getError()) { $this->error($validate->getError()); } $goods = Goods::where('id', $goods_id) ->where('status', GoodsEnum::STATUS_ON_SALE) ->find(); if (empty($goods)) { $this->error('商品不存在'); } if ($goods['spec_type'] && !$goods_sku_id) { $this->error("请选择规格"); } if (empty($goods_id) && empty($goods_sku_id)) { $this->error('参数错误'); } if ($nums <= 0) { $this->error('数量必须大于0'); } if (!empty($goods_sku_id)) { $row = Sku::with([ 'goods' => function ($query) { $query->where('status', GoodsEnum::STATUS_ON_SALE); } ])->where('id', $goods_sku_id)->find(); if (empty($row) || empty($row->goods)) { $this->error('商品已下架'); } if ($row->stocks < $nums) { $this->error('库存数量不足' . $nums . '件'); } } else { if ($goods->stocks < $nums) { $this->error('库存数量不足' . $nums . '件'); } } //去添加购物车 $cart_id = CartService::push($goods_id, $goods_sku_id, $nums, $this->auth->id); $this->success('添加成功', $cart_id); } //删除购物车 public function del() { $id = $this->request->post('id'); if (!$id) { $this->error('参数错误'); } $status = Carts::where('user_id', $this->auth->id)->where('id', 'IN', $id)->delete(); if ($status) { $this->success('删除成功'); } $this->error('删除失败'); } //购物车商品数量+- public function set_nums() { $id = $this->request->post('id'); if (!$id) { $this->error('参数错误'); } $nums = $this->request->post('nums/d'); // 使用验证器验证数量 $validate = new CartValidate(); $validate->scene('set_nums')->check([ 'nums' => $nums, ]); $row = Carts::with(['Goods', 'Sku'])->where('id', $id)->where('user_id', $this->auth->id)->find(); if (!$row) { $this->error('未找到记录'); } if ($row->goods_sku_id) { if (!$row->sku) { $this->error('商品不存在'); } if ($row->sku->stocks < $nums) { $this->error('库存不足'); } } else { if (!$row->goods) { $this->error('商品不存在'); } if ($row->goods->stocks < $nums) { $this->error('库存不足'); } } $row->nums = $nums; $row->save(); $this->success('操作成功'); } //购物车列表 public function index() { $ids = $this->request->param('ids'); $list = CartService::getGoodsList($ids, $this->auth->id); //没有商品过滤数据 foreach ($list as $key => $item) { if (empty($item->goods)) { $item->delete(); unset($list[$key]); continue; } $item['subtotal'] = bcmul($item['nums'], ($item->sku->price ?? $item->goods->price), 2); $item->goods->visible(explode(',', 'id,title,price,image,marketprice')); } $this->success('', $list); } //获取购物车的数量 public function cart_nums() { $total = Carts::where('user_id', $this->auth->id)->count(); $this->success('', $total); } /** * 选中或取消商品 * */ public function checked() { $cartIds = $this->request->post('cartIds/a'); $isChecked = $this->request->post('isChecked'); $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); $this->success('操作成功'); } }