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.

124 lines
3.5 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 create($goodsId, $count, $skuId)
  34. {
  35. if (empty($goodsId) || empty($count)) {
  36. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  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->addGoods($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(Helper::REQUEST_BAD_PARAMS);
  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. * @param $goodsId
  76. * @param $skuId
  77. * @param $count
  78. * @return Cart;
  79. * 创建新购物车
  80. * @throws ServerErrorHttpException
  81. * @throws NotFoundHttpException
  82. */
  83. private function addGoods($goodsId, $skuId, $count)
  84. {
  85. $goods = Goods::findOne($goodsId);
  86. if (!$goods) {
  87. throw new NotFoundHttpException('商品未找到');
  88. }
  89. $cart = new Cart();
  90. $cart->goods_id = $goods->id;
  91. $cart->user_id = Yii::$app->user->getId();
  92. $cart->goods_count = $count;
  93. $cart->sku_id = $skuId ?: 0;
  94. $cart->goods_img = $goods->image;
  95. $cart->goods_name = $goods->name;
  96. $cart->goods_price = Helper::countPrice($goodsId, $skuId);
  97. if (!$cart->save()) {
  98. throw new ServerErrorHttpException('服务器创建购物车失败');
  99. }
  100. return $cart;
  101. }
  102. /**
  103. * @return ActiveRecord|null|Cart
  104. * 获取购物车实例
  105. */
  106. private function findCart()
  107. {
  108. $id = Yii::$app->request->getQueryParam('id');
  109. return Cart::find()
  110. ->where(['id' => $id])
  111. ->andWhere(['user_id' => Yii::$app->user->getId()])
  112. ->one();
  113. }
  114. }