|
|
<?php
namespace api\logic;
use backend\modules\goods\models\ars\Goods; use backend\modules\shop\models\ars\Cart; use Throwable; use Yii; use yii\base\BaseObject; use yii\db\ActiveRecord; use yii\web\BadRequestHttpException; use yii\web\NotFoundHttpException; use yii\web\ServerErrorHttpException;
/** * @author iron * @email weiriron@gmail.com * Class CartLogic * @package api\logic */ class CartLogic extends BaseObject { public $viewAction = 'view';
const TYPE_SUB = -1; const TYPE_ADD = 1; const TYPE_EDIT = 2;
/** * @throws BadRequestHttpException * @throws NotFoundHttpException * @throws ServerErrorHttpException * @throws Throwable * @throws yii\db\StaleObjectException * 删除购物车 */ public function delete() { $ids = Yii::$app->request->getBodyParam('ids'); if (empty($ids) || !is_array($ids)) { throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } Helper::delCarts($ids); Yii::$app->getResponse()->setStatusCode(204); }
/** * @return array|Cart|null * @throws BadRequestHttpException * @throws NotFoundHttpException * @throws ServerErrorHttpException * 添加商品到购物车 */ public function create() { $goodsId = Yii::$app->request->getBodyParam('goodsId'); $count = Yii::$app->request->getBodyParam('count'); $skuId = Yii::$app->request->getBodyParam('skuId'); if (empty($goodsId) || empty($count)) { throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } //TODO 判断限购
Helper::checkStock($goodsId, $count, $skuId); $cart = Cart::find()->where(['goods_id' => $goodsId]) ->andWhere(['user_id' => Yii::$app->user->getId()]) ->one(); if ($cart) { if (!$cart->updateCounters(['goods_count' => $count])) { throw new ServerErrorHttpException('服务器添加购物车商品失败'); } } else { $cart = $this->addGoods($goodsId, $skuId, $count); } Helper::createdResponse($cart, $this->viewAction); return $cart;
}
/** * @return bool * @throws BadRequestHttpException * @throws NotFoundHttpException * @throws ServerErrorHttpException */ public function update() { $type = Yii::$app->request->getBodyParam('type'); $count = Yii::$app->request->getBodyParam('count'); if (empty($type) || !in_array($type, [self::TYPE_ADD, self::TYPE_SUB, self::TYPE_EDIT])) { throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } $cart = $this->findCart(); if (!$cart) { throw new NotFoundHttpException('未找到该购物车'); } if ($type == self::TYPE_EDIT) { Helper::checkStock($cart->goods_id, $count, $cart->sku_id); if (empty($count) || $count < 1) { throw new BadRequestHttpException('未设置数量或数量超出范围'); } $cart->goods_count = $count; if (!$cart->save()) { throw new ServerErrorHttpException('服务器编辑购物车数量失败'); } } else { if ($cart->goods_count == 1 && $type == self::TYPE_SUB) { throw new BadRequestHttpException('商品需至少1件'); } if ($type == self::TYPE_ADD) { Helper::checkStock($cart->goods_id, $cart->goods_count + 1, $cart->sku_id); } if (!$cart->updateCounters(['goods_count' => $type])) { throw new ServerErrorHttpException('服务器增删购物车失败'); } } return true; }
/*----------------------------------- 又一条华丽的分割线 --------------------------------------*/ /** * @param $goodsId * @param $skuId * @param $count * @return Cart; * @throws ServerErrorHttpException * @throws NotFoundHttpException * 创建新购物车 */ private function addGoods($goodsId, $skuId, $count) { $goods = Goods::findOne($goodsId); if (!$goods) { throw new NotFoundHttpException('商品未找到'); } $cart = new Cart(); $cart->goods_id = $goods->id; $cart->user_id = Yii::$app->user->getId(); $cart->goods_count = $count; $cart->sku_id = $skuId ?: 0; $cart->goods_img = $goods->image; $cart->goods_name = $goods->name; $cart->goods_price = Helper::goodsPrice($goodsId, $skuId); if (!$cart->save()) { throw new ServerErrorHttpException('服务器创建购物车失败'); } return $cart; }
/** * @return ActiveRecord|null|Cart * 获取购物车实例 */ private function findCart() { $id = Yii::$app->request->getQueryParam('id'); return Cart::find() ->where(['id' => $id]) ->andWhere(['user_id' => Yii::$app->user->getId()]) ->one(); }
}
|