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.

143 lines
4.0 KiB

  1. <?php
  2. namespace api\logic;
  3. use antgoods\goods\models\ars\Goods;
  4. use common\models\ars\Cart;
  5. use yii\base\Component;
  6. use Yii;
  7. use yii\db\ActiveRecord;
  8. use yii\helpers\Url;
  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 Component
  19. {
  20. public $viewAction = 'view';
  21. const TYPE_ADD = 1;
  22. const TYPE_SUB = -1;
  23. /**
  24. * @param $goodsId
  25. * @param $count
  26. * @param $skuId
  27. * @return array|\yii\db\ActiveRecord|null
  28. * @throws BadRequestHttpException
  29. * @throws NotFoundHttpException
  30. * @throws ServerErrorHttpException
  31. * 添加商品到购物车
  32. */
  33. public function addGoods($goodsId, $count, $skuId)
  34. {
  35. if (empty($goodsId) || empty($count)) {
  36. throw new BadRequestHttpException('无效参数');
  37. }
  38. //TODO 判断限购
  39. //判断库存
  40. Helper::checkStock($goodsId, $count, $skuId);
  41. $cart = Cart::find()->where(['goods_id' => $goodsId])
  42. ->andWhere(['user_id' => Yii::$app->user->getId()])
  43. ->one();
  44. if ($cart) {
  45. if (!$cart->updateCounters(['goods_count' => $count])) {
  46. throw new ServerErrorHttpException('服务器添加购物车商品失败');
  47. }
  48. } else {
  49. $cart = $this->create($goodsId, $skuId, $count);
  50. }
  51. Helper::createdResponse($cart, $this->viewAction);
  52. return $cart;
  53. }
  54. /**
  55. * @param int $type
  56. * @return bool
  57. * @throws BadRequestHttpException
  58. * @throws NotFoundHttpException
  59. */
  60. public function addOrSubGoods($type)
  61. {
  62. if (empty($type) || ($type != self::TYPE_SUB && $type != self::TYPE_ADD)) {
  63. throw new BadRequestHttpException('无效参数');
  64. }
  65. $cart = $this->findCart();
  66. if (!$cart) {
  67. throw new NotFoundHttpException('未找到该购物车');
  68. }
  69. if ($cart->goods_count == 1 && $type == self::TYPE_SUB) {
  70. throw new BadRequestHttpException('购物商品需至少1件');
  71. }
  72. return $cart->updateCounters(['goods_count' => $type]);
  73. }
  74. /**
  75. * @throws NotFoundHttpException
  76. * @throws ServerErrorHttpException
  77. * @throws \Throwable
  78. * @throws \yii\db\StaleObjectException
  79. * 删除购物车
  80. */
  81. public function delete()
  82. {
  83. $cart = $this->findCart();
  84. if (!$cart) {
  85. throw new NotFoundHttpException('未找到该购物车');
  86. }
  87. if ($cart->delete()) {
  88. Yii::$app->getResponse()->setStatusCode(204);
  89. } else {
  90. throw new ServerErrorHttpException('服务器无法删除购物车');
  91. }
  92. }
  93. /**
  94. * @param $goodsId
  95. * @param $skuId
  96. * @param $count
  97. * @return Cart;
  98. * 创建新购物车
  99. * @throws ServerErrorHttpException
  100. * @throws NotFoundHttpException
  101. */
  102. private function create($goodsId, $skuId, $count)
  103. {
  104. $goods = Goods::findOne($goodsId);
  105. if (!$goods) {
  106. throw new NotFoundHttpException('商品未找到');
  107. }
  108. $cart = new Cart();
  109. $cart->goods_id = $goods->id;
  110. $cart->user_id = Yii::$app->user->getId();
  111. $cart->goods_count = $count;
  112. $cart->sku_id = $skuId ?: 0;
  113. $cart->goods_img = $goods->image;
  114. $cart->goods_name = $goods->name;
  115. $cart->goods_price = Helper::countPrice($goodsId, $skuId);
  116. if (!$cart->save()) {
  117. throw new ServerErrorHttpException('服务器创建购物车失败');
  118. }
  119. return $cart;
  120. }
  121. /**
  122. * @return ActiveRecord|null|Cart
  123. * 获取购物车实例
  124. */
  125. private function findCart()
  126. {
  127. $id = Yii::$app->request->getQueryParam('id');
  128. return Cart::find()
  129. ->where(['id' => $id])
  130. ->andWhere(['user_id' => Yii::$app->user->getId()])
  131. ->one();
  132. }
  133. }