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.

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