CartItemController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\member;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use yii\helpers\Json;
  6. use common\helpers\ResultHelper;
  7. use api\controllers\UserAuthController;
  8. use addons\TinyShop\common\forms\CartItemForm;
  9. use addons\TinyShop\common\models\member\CartItem;
  10. use addons\TinyShop\common\interfaces\CartItemInterface;
  11. /**
  12. * 购物车
  13. *
  14. * Class CartItemController
  15. * @package addons\TinyShop\api\modules\v1\controllers\member
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class CartItemController extends UserAuthController
  19. {
  20. /**
  21. * @var CartItem
  22. */
  23. public $modelClass = CartItem::class;
  24. /**
  25. * @var CartItemInterface
  26. */
  27. public $service;
  28. /**
  29. * @var int
  30. */
  31. public $member_id;
  32. /**
  33. * @param $action
  34. * @return bool
  35. * @throws \yii\base\InvalidConfigException
  36. * @throws \yii\web\BadRequestHttpException
  37. * @throws \yii\web\ForbiddenHttpException
  38. * @throws \yii\web\NotFoundHttpException
  39. */
  40. public function beforeAction($action)
  41. {
  42. parent::beforeAction($action);
  43. // 已登录
  44. $this->member_id = Yii::$app->user->identity->member_id;
  45. return true;
  46. }
  47. /**
  48. * 购物车
  49. *
  50. * @return mixed|ActiveDataProvider
  51. */
  52. public function actionIndex()
  53. {
  54. list($carts, $loseEfficacy) = Yii::$app->tinyShopService->memberCartItem->all($this->member_id);
  55. return [
  56. 'carts' => array_merge($carts),
  57. 'lose_efficacy' => $loseEfficacy,
  58. ];
  59. }
  60. /**
  61. * 创建
  62. *
  63. * @return CartItem|array|mixed|\yii\db\ActiveRecord|null
  64. * @throws \yii\web\UnprocessableEntityHttpException
  65. */
  66. public function actionCreate()
  67. {
  68. $model = new CartItemForm();
  69. $model->attributes = Yii::$app->request->post();
  70. $model->member_id = $this->member_id;
  71. if (!$model->validate()) {
  72. return ResultHelper::json(422, $this->getError($model));
  73. }
  74. return Yii::$app->tinyShopService->memberCartItem->create($model);
  75. }
  76. /**
  77. * 修改购物车数量
  78. *
  79. * @return CartItem|array|mixed|\yii\db\ActiveRecord|null
  80. * @throws \yii\web\UnprocessableEntityHttpException
  81. */
  82. public function actionUpdateNumber()
  83. {
  84. $model = new CartItemForm();
  85. $model->attributes = Yii::$app->request->post();
  86. $model->member_id = $this->member_id;
  87. if (!$model->validate()) {
  88. return ResultHelper::json(422, $this->getError($model));
  89. }
  90. return Yii::$app->tinyShopService->memberCartItem->updateNumber($model);
  91. }
  92. /**
  93. * 修改购物车 sku
  94. *
  95. * @param $product_id
  96. * @param $sku_id
  97. * @param $member_id
  98. */
  99. public function actionUpdateSku()
  100. {
  101. $model = new CartItemForm();
  102. $model->attributes = Yii::$app->request->post();
  103. $model->member_id = $this->member_id;
  104. if (!$model->validate()) {
  105. return ResultHelper::json(422, $this->getError($model));
  106. }
  107. // 事务
  108. $transaction = Yii::$app->db->beginTransaction();
  109. try {
  110. $data = Yii::$app->tinyShopService->memberCartItem->updateSku($model);
  111. $transaction->commit();
  112. return $data;
  113. } catch (\Exception $e) {
  114. $transaction->rollBack();
  115. return ResultHelper::json(422, $e->getMessage());
  116. }
  117. }
  118. /**
  119. * 删除一组
  120. *
  121. * @param $product_id
  122. * @param $sku_id
  123. * @param $member_id
  124. */
  125. public function actionDeleteIds()
  126. {
  127. $ids = Yii::$app->request->post('ids', '');
  128. try {
  129. $ids = Json::decode($ids);
  130. } catch (\Exception $e) {
  131. return ResultHelper::json(422, '请提交正确的 json 格式');
  132. }
  133. if (empty($ids)) {
  134. return ResultHelper::json(422, '请选择要删除的购物车商品');
  135. }
  136. return Yii::$app->tinyShopService->memberCartItem->deleteIds($ids, $this->member_id);
  137. }
  138. /**
  139. * @return int|string
  140. */
  141. public function actionCount()
  142. {
  143. return Yii::$app->tinyShopService->memberCartItem->findCountByMemberId($this->member_id);
  144. }
  145. /**
  146. * 清空购物车
  147. *
  148. * @param $member_id
  149. */
  150. public function actionClear()
  151. {
  152. // 清空失效的购物车商品
  153. $lose_status = Yii::$app->request->post('lose_status');
  154. return Yii::$app->tinyShopService->memberCartItem->clear($this->member_id, $lose_status);
  155. }
  156. }
粤ICP备19079148号