CartController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace addons\Purchase\api\modules\v1\controllers;
  3. use addons\Purchase\common\models\PurchaseCart;
  4. use addons\Purchase\common\models\PurchaseManager;
  5. use api\controllers\OnAuthController;
  6. use common\enums\StatusEnum;
  7. use common\models\goods\Goods;
  8. use common\models\goods\GoodsAttr;
  9. /**
  10. * Class CartController
  11. * @package api\modules\goods\controllers
  12. */
  13. class CartController extends OnAuthController
  14. {
  15. public $modelClass = '';
  16. /**
  17. * 不用进行登录验证的方法
  18. * 例如: ['index', 'update', 'create', 'view', 'delete']
  19. * 默认全部需要验证
  20. *
  21. * @var array
  22. */
  23. protected $authOptional = ['add'];
  24. /**
  25. * 添加购物车
  26. * @return mixed|void
  27. */
  28. public function actionAdd()
  29. {
  30. try {
  31. $params = \Yii::$app->request->post();
  32. $res = \Yii::$app->services->validate->validate($params, [
  33. [['goods_id', 'num'], 'required'],
  34. [['attr_id'], 'safe'],
  35. ]);
  36. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  37. $goods = Goods::findOne(['id' => $params['goods_id'], 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]);
  38. if (empty($goods)) return $this->error("商品不存在");
  39. // 检查用户申请是否通过
  40. if (PurchaseManager::check($this->user_id) == false) return $this->error("请申请资格");
  41. // 没有规格id则查找默认规格【寻找价格最低的规格】
  42. $where = ['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED];
  43. !empty($params['attr_id']) && $where['id'] = $params['attr_id'];
  44. $goods_attr = GoodsAttr::find()->where($where)->orderBy('price asc')->one();
  45. if ($goods_attr->goods_id != $params['goods_id']) return $this->error("goods_id与attr_id不配对");
  46. if (empty($goods->is_attr)) {
  47. if (empty($goods->stock)) return $this->error('库存不足');
  48. } else {
  49. //多规格
  50. if (empty($goods_attr)) return $this->error("所选的规格不存在");
  51. if (empty($goods_attr->stock)) return $this->error("所选的规格库存不足");
  52. }
  53. $params['mall_id'] = $this->mall_id;
  54. $params['user_id'] = $this->user_id;
  55. PurchaseCart::addCart($params);
  56. return $this->success('加入购物车成功', []);
  57. } catch (\Exception $e) {
  58. return $this->error($e->getMessage(), []);
  59. }
  60. }
  61. /**
  62. * 删除购物车
  63. * @return mixed|void
  64. */
  65. public function actionDel()
  66. {
  67. $params = \Yii::$app->request->post();
  68. $res = \Yii::$app->services->validate->validate($params, [
  69. [['list'], 'required'],
  70. ]);
  71. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  72. $params['mall_id'] = $this->mall_id;
  73. $params['user_id'] = $this->user_id;
  74. PurchaseCart::delCart($params);
  75. return $this->success('操作成功', []);
  76. }
  77. /**
  78. * 购物车列表
  79. * @return mixed|void
  80. */
  81. public function actionIndex()
  82. {
  83. $list = PurchaseCart::getCartList($this->user_id);
  84. $data = [];
  85. if (!empty($list)) {
  86. $attr_id_arr = [];
  87. $num_arr = [];
  88. foreach ($list as $v) {
  89. $row = json_decode($v, true);
  90. if ($row) {
  91. $attr_id_arr[] = $row['attr_id'];
  92. $num_arr[$row['attr_id']] = $row['num'];
  93. }
  94. }
  95. $params['where'] = [['id' => $attr_id_arr]];
  96. $params['with'] = ['goods' => function ($query) {
  97. $query->select('id,goods_name,is_on_sale,cover_pic,is_attr')->with(['extra']);
  98. }];
  99. $params['select'] = 'id,sign_id,goods_id,pic_url,price,name';
  100. $list = GoodsAttr::lists($params);
  101. $rows = [];
  102. if (!empty($list)) {
  103. foreach ($list as $v) {
  104. $rows[] = [
  105. 'goods_id' => $v['goods_id'],
  106. 'num' => $num_arr[$v['id']],
  107. 'attr_id' => $v['id'],
  108. 'price' => $v['price'],
  109. 'cover_pic' => $v['goods']['cover_pic']??'',
  110. 'attr' => $v['name'],
  111. 'goods_name' => $v['goods']['goods_name'],
  112. 'is_on_sale' => $v['goods']['is_on_sale'],
  113. ];
  114. }
  115. }
  116. $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name');
  117. $mall_logo = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
  118. $mch_arr = [
  119. ['id' => 0, 'mch_pic' => $mall_logo, 'mch_name' => $mall_name, 'list' => $rows]
  120. ];
  121. }
  122. return $this->success('操作成功', $mch_arr);
  123. }
  124. /**
  125. * @notes:修改购物车
  126. * @return mixed|null
  127. * @author: lun
  128. * @Time: 2022/10/24 14:56
  129. */
  130. public function actionUpdateCart()
  131. {
  132. try {
  133. $params = \Yii::$app->request->post();
  134. $res = \Yii::$app->services->validate->validate($params, [
  135. [['goods_id', 'num'], 'required'],
  136. [['attr_id'], 'safe'],
  137. ]);
  138. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  139. $goods = Goods::findOne(['id' => $params['goods_id'], 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]);
  140. if (empty($goods)) return $this->error("商品不存在");
  141. // 没有规格id则查找默认规格【寻找价格最低的规格】
  142. $where = ['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED];
  143. !empty($params['attr_id']) && $where['id'] = $params['attr_id'];
  144. $goods_attr = GoodsAttr::find()->where($where)->orderBy('price asc')->one();
  145. if ($goods_attr->goods_id != $params['goods_id']) return $this->error("goods_id与attr_id不配对");
  146. if (empty($goods->is_attr)) {
  147. if (empty($goods->stock)) return $this->error('库存不足');
  148. } else {
  149. //多规格
  150. if (empty($goods_attr)) return $this->error("所选的规格不存在");
  151. if (empty($goods_attr->stock)) return $this->error("所选的规格库存不足");
  152. }
  153. $params['mall_id'] = $this->mall_id;
  154. $params['user_id'] = $this->user_id;
  155. PurchaseCart::updateCart($params);
  156. return $this->success('更新购物车成功', []);
  157. } catch (\Exception $e) {
  158. return $this->error($e->getMessage(), []);
  159. }
  160. }
  161. }