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]; $cloudStockFillCartService = new CloudStockFillCartService($baseParams); $currentNum = $cloudStockFillCartService->getCartList([$post['goods_id']])[0]['num'] ?? 0; // 加数量才需要验证 if ($post['num'] >= $currentNum) { // 加入购物车前验证 $cloudStockFillOrderService = new CloudStockFillOrderService($baseParams); try { $cloudStockFillOrderService->createBeforeCheck($post['goods_id'], $post['num']); }catch (\Exception $e) { $this->error($e->getMessage()); return; } } $post = array_merge($post, $baseParams); if (!CloudStockFillCart::setCart($post)) { $this->error('加入购物车异常'); return; } $this->success(); } /** * 返回加购的数量 * * @return void */ public function actionCartInfo() { $baseParams = ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'stock_agent' => $this->stock_agent]; $cartService = new CloudStockFillCartService($baseParams); $this->success('success', $cartService->getCartInfo()); } }