You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.0 KiB

  1. <?php
  2. namespace api\logic;
  3. use backend\modules\goods\models\ars\Goods;
  4. use backend\modules\shop\models\ars\Cart;
  5. use Throwable;
  6. use Yii;
  7. use yii\base\BaseObject;
  8. use yii\db\ActiveRecord;
  9. use yii\web\BadRequestHttpException;
  10. use yii\web\NotFoundHttpException;
  11. use yii\web\ServerErrorHttpException;
  12. /**
  13. * @author iron
  14. * @email weiriron@gmail.com
  15. * Class CartLogic
  16. * @package api\logic
  17. */
  18. class CartLogic extends BaseObject
  19. {
  20. public $viewAction = 'view';
  21. const TYPE_SUB = -1;
  22. const TYPE_ADD = 1;
  23. const TYPE_EDIT = 2;
  24. /**
  25. * @throws BadRequestHttpException
  26. * @throws NotFoundHttpException
  27. * @throws ServerErrorHttpException
  28. * @throws Throwable
  29. * @throws yii\db\StaleObjectException
  30. * 删除购物车
  31. */
  32. public function delete()
  33. {
  34. $ids = Yii::$app->request->getBodyParam('ids');
  35. if (empty($ids) || !is_array($ids)) {
  36. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  37. }
  38. Helper::delCarts($ids);
  39. Yii::$app->getResponse()->setStatusCode(204);
  40. }
  41. /**
  42. * @return array|Cart|null
  43. * @throws BadRequestHttpException
  44. * @throws NotFoundHttpException
  45. * @throws ServerErrorHttpException
  46. * 添加商品到购物车
  47. */
  48. public function create()
  49. {
  50. $goodsId = Yii::$app->request->getBodyParam('goodsId');
  51. $count = Yii::$app->request->getBodyParam('count');
  52. $skuId = Yii::$app->request->getBodyParam('skuId');
  53. if (empty($goodsId) || empty($count)) {
  54. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  55. }
  56. //TODO 判断限购
  57. Helper::checkStock($goodsId, $count, $skuId);
  58. $cart = Cart::find()->where(['goods_id' => $goodsId])
  59. ->andWhere(['user_id' => Yii::$app->user->getId()])
  60. ->one();
  61. if ($cart) {
  62. if (!$cart->updateCounters(['goods_count' => $count])) {
  63. throw new ServerErrorHttpException('服务器添加购物车商品失败');
  64. }
  65. } else {
  66. $cart = $this->addGoods($goodsId, $skuId, $count);
  67. }
  68. Helper::createdResponse($cart, $this->viewAction);
  69. return $cart;
  70. }
  71. /**
  72. * @return bool
  73. * @throws BadRequestHttpException
  74. * @throws NotFoundHttpException
  75. * @throws ServerErrorHttpException
  76. */
  77. public function update()
  78. {
  79. $type = Yii::$app->request->getBodyParam('type');
  80. $count = Yii::$app->request->getBodyParam('count');
  81. if (empty($type) || !in_array($type, [self::TYPE_ADD, self::TYPE_SUB, self::TYPE_EDIT])) {
  82. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  83. }
  84. $cart = $this->findCart();
  85. if (!$cart) {
  86. throw new NotFoundHttpException('未找到该购物车');
  87. }
  88. if ($type == self::TYPE_EDIT) {
  89. Helper::checkStock($cart->goods_id, $count, $cart->sku_id);
  90. if (empty($count) || $count < 1) {
  91. throw new BadRequestHttpException('未设置数量或数量超出范围');
  92. }
  93. $cart->goods_count = $count;
  94. if (!$cart->save()) {
  95. throw new ServerErrorHttpException('服务器编辑购物车数量失败');
  96. }
  97. } else {
  98. if ($cart->goods_count == 1 && $type == self::TYPE_SUB) {
  99. throw new BadRequestHttpException('商品需至少1件');
  100. }
  101. if ($type == self::TYPE_ADD) {
  102. Helper::checkStock($cart->goods_id, $cart->goods_count + 1, $cart->sku_id);
  103. }
  104. if (!$cart->updateCounters(['goods_count' => $type])) {
  105. throw new ServerErrorHttpException('服务器增删购物车失败');
  106. }
  107. }
  108. return true;
  109. }
  110. /*----------------------------------- 又一条华丽的分割线 --------------------------------------*/
  111. /**
  112. * @param $goodsId
  113. * @param $skuId
  114. * @param $count
  115. * @return Cart;
  116. * @throws ServerErrorHttpException
  117. * @throws NotFoundHttpException
  118. * 创建新购物车
  119. */
  120. private function addGoods($goodsId, $skuId, $count)
  121. {
  122. $goods = Goods::findOne($goodsId);
  123. if (!$goods) {
  124. throw new NotFoundHttpException('商品未找到');
  125. }
  126. $cart = new Cart();
  127. $cart->goods_id = $goods->id;
  128. $cart->user_id = Yii::$app->user->getId();
  129. $cart->goods_count = $count;
  130. $cart->sku_id = $skuId ?: 0;
  131. $cart->goods_img = $goods->image;
  132. $cart->goods_name = $goods->name;
  133. $cart->goods_price = Helper::goodsPrice($goodsId, $skuId);
  134. if (!$cart->save()) {
  135. throw new ServerErrorHttpException('服务器创建购物车失败');
  136. }
  137. return $cart;
  138. }
  139. /**
  140. * @return ActiveRecord|null|Cart
  141. * 获取购物车实例
  142. */
  143. private function findCart()
  144. {
  145. $id = Yii::$app->request->getQueryParam('id');
  146. return Cart::find()
  147. ->where(['id' => $id])
  148. ->andWhere(['user_id' => Yii::$app->user->getId()])
  149. ->one();
  150. }
  151. }