request->post(); $rules = [ [['goods_id', 'num'], 'required'], [['goods_id', 'num'], 'integer'], ]; $res = Yii::$app->services->validate->validate($post, $rules); if ($res === false) { return $this->error(Yii::$app->services->validate->getErrorMessage()); } $baseParams = ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'stock_agent' => $this->stock_agent]; $goodsService = new CloudStockAgentGoodsService($baseParams); $cartService = new CloudStockAgentCartService($baseParams); $orderService = new CloudStockAgentOrderService($baseParams); $levelService = new CloudStockLevelService($baseParams); if (!$goodsService->goodsExist($post['goods_id'])) { $this->error('商品不存在'); return; } $currentCart = $cartService->getCartList([$post['goods_id']]); $currentNum = array_column($currentCart, 'num', 'goods_id')[$post['goods_id']] ?? 0; // 如果是增加购物车才验证,如果是减购物车则不理 if ($post['num'] > $currentNum) { $cartList = [ ['num' => $post['num'], 'goods_id' => $post['goods_id']] ]; // 验证限购(不能超过代理商品的总库存 不能超过后台限制的配置) if (!$orderService->checkLimit($cartService, $levelService, $goodsService, $cartList, false)) { $this->error($orderService->getError()); return; } } $post = array_merge($post, $baseParams); if (!CloudStockAgentCart::setCart($post)) { $this->error('加入购物车异常'); return; } $this->success(); } /** * 返回加购的数量 * * @return void */ public function actionCartInfo() { $baseParams = ['user_id' => $this->user_id, 'mall_id' => $this->mall_id]; $cartService = new CloudStockAgentCartService($baseParams); $this->success('success', ['cart_count' => $cartService->getCurrentCartCount(), 'cart_list' => $cartService->getCartList()]); } }