| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace addons\CloudStock\api\modules\v1\controllers;
- use addons\CloudStock\common\models\CloudStockAgentCart;
- use addons\CloudStock\common\services\CloudStockAgentCartService;
- use addons\CloudStock\common\services\CloudStockAgentGoodsService;
- use addons\CloudStock\common\services\CloudStockAgentOrderService;
- use addons\CloudStock\common\services\CloudStockLevelService;
- use common\enums\StatusEnum;
- use Exception;
- use Yii;
- class CsAgentCartController extends BaseController
- {
- public $modelClass = '';
- protected $authOptional = [];
- /**
- * @return mixed|void|null
- */
- public function actionSetCart()
- {
- $post = $this->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()]);
- }
- }
|