Cart.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\Carts;
  4. use app\common\model\Sku;
  5. use app\common\model\Goods;
  6. use app\common\Service\CartService;
  7. use app\api\validate\Cart as CartValidate;
  8. use app\common\Enum\GoodsEnum;
  9. /**
  10. * 购物车接口
  11. */
  12. class Cart extends Base
  13. {
  14. protected $noNeedLogin = [];
  15. //添加购物车
  16. public function add()
  17. {
  18. $goods_id = $this->request->post('goods_id'); //单规格使用
  19. $goods_sku_id = $this->request->post('goods_sku_id'); //多规格使用
  20. $nums = $this->request->post('nums/d', 1);
  21. // 增加验证器
  22. $validate = new CartValidate();
  23. $validate->scene('add')->check([
  24. 'goods_id' => $goods_id,
  25. 'goods_sku_id' => $goods_sku_id,
  26. 'nums' => $nums,
  27. ]);
  28. if ($validate->getError()) {
  29. $this->error($validate->getError());
  30. }
  31. $goods = Goods::where('id', $goods_id)
  32. ->where('status', GoodsEnum::STATUS_ON_SALE)
  33. ->find();
  34. if (empty($goods)) {
  35. $this->error('商品不存在');
  36. }
  37. if ($goods['spec_type'] && !$goods_sku_id) {
  38. $this->error("请选择规格");
  39. }
  40. if (empty($goods_id) && empty($goods_sku_id)) {
  41. $this->error('参数错误');
  42. }
  43. if ($nums <= 0) {
  44. $this->error('数量必须大于0');
  45. }
  46. if (!empty($goods_sku_id)) {
  47. $row = Sku::with([
  48. 'goods' => function ($query) {
  49. $query->where('status', GoodsEnum::STATUS_ON_SALE);
  50. }
  51. ])->where('id', $goods_sku_id)->find();
  52. if (empty($row) || empty($row->goods)) {
  53. $this->error('商品已下架');
  54. }
  55. if ($row->stocks < $nums) {
  56. $this->error('库存数量不足' . $nums . '件');
  57. }
  58. } else {
  59. if ($goods->stocks < $nums) {
  60. $this->error('库存数量不足' . $nums . '件');
  61. }
  62. }
  63. //去添加购物车
  64. $cart_id = CartService::push($goods_id, $goods_sku_id, $nums, $this->auth->id);
  65. $this->success('添加成功', $cart_id);
  66. }
  67. //删除购物车
  68. public function del()
  69. {
  70. $nId = $this->request->post('id/d');
  71. $userId = $this->auth->id;
  72. // 验证器
  73. $validate = new CartValidate();
  74. $validate->scene('del')->check([
  75. 'id' => $nId,
  76. ]);
  77. if ($validate->getError()) {
  78. $this->error($validate->getError());
  79. }
  80. $arrIds = [$nId];
  81. $status = CartService::deleteCart($arrIds, $userId);
  82. if ($status) {
  83. $this->success('删除成功');
  84. }
  85. $this->error('删除失败');
  86. }
  87. //购物车商品数量+-
  88. public function set_nums()
  89. {
  90. $id = $this->request->post('id');
  91. if (!$id) {
  92. $this->error('参数错误');
  93. }
  94. $nums = $this->request->post('nums/d');
  95. // 使用验证器验证数量
  96. $validate = new CartValidate();
  97. $validate->scene('set_nums')->check([
  98. 'nums' => $nums,
  99. ]);
  100. $row = Carts::with(['Goods', 'Sku'])->where('id', $id)->where('user_id', $this->auth->id)->find();
  101. if (!$row) {
  102. $this->error('未找到记录');
  103. }
  104. if ($row->goods_sku_id) {
  105. if (!$row->sku) {
  106. $this->error('商品不存在');
  107. }
  108. if ($row->sku->stocks < $nums) {
  109. $this->error('库存不足');
  110. }
  111. } else {
  112. if (!$row->goods) {
  113. $this->error('商品不存在');
  114. }
  115. if ($row->goods->stocks < $nums) {
  116. $this->error('库存不足');
  117. }
  118. }
  119. $row->nums = $nums;
  120. $row->save();
  121. $this->success('操作成功');
  122. }
  123. //购物车列表
  124. public function index()
  125. {
  126. $ids = $this->request->param('ids');
  127. $list = CartService::getGoodsList($ids, $this->auth->id);
  128. //没有商品过滤数据
  129. foreach ($list as $key => $item) {
  130. if (empty($item->goods)) {
  131. $item->delete();
  132. unset($list[$key]);
  133. continue;
  134. }
  135. // 如果sku 的 图片为空
  136. if (empty($item->sku->image)) {
  137. $item->sku->image = $item->goods->image;
  138. }
  139. $item['subtotal'] = bcmul($item['nums'], ($item->sku->price ?? $item->goods->price), 2);
  140. $item->goods->visible(explode(',', 'id,title,price,image,marketprice'));
  141. }
  142. $this->success('', $list);
  143. }
  144. //获取购物车的数量
  145. public function cart_nums()
  146. {
  147. $total = Carts::where('user_id', $this->auth->id)->count();
  148. $this->success('', $total);
  149. }
  150. /**
  151. * 选中或取消商品
  152. *
  153. */
  154. public function checked()
  155. {
  156. $cartIds = $this->request->post('cartIds/a');
  157. $isChecked = $this->request->post('isChecked');
  158. $userId = $this->auth->id;
  159. // 验证器
  160. $validate = new CartValidate();
  161. $validate->scene('checked')->check([
  162. 'cartIds' => $cartIds,
  163. 'isChecked' => $isChecked,
  164. ]);
  165. if ($validate->getError()) {
  166. $this->error($validate->getError());
  167. }
  168. CartService::updateChecked(
  169. $userId,
  170. $cartIds,
  171. $isChecked);
  172. $this->success('操作成功');
  173. }
  174. }