Cart.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $id = $this->request->post('id');
  71. if (!$id) {
  72. $this->error('参数错误');
  73. }
  74. $status = Carts::where('user_id', $this->auth->id)->where('id', 'IN', $id)->delete();
  75. if ($status) {
  76. $this->success('删除成功');
  77. }
  78. $this->error('删除失败');
  79. }
  80. //购物车商品数量+-
  81. public function set_nums()
  82. {
  83. $id = $this->request->post('id');
  84. if (!$id) {
  85. $this->error('参数错误');
  86. }
  87. $nums = $this->request->post('nums/d');
  88. // 使用验证器验证数量
  89. $validate = new CartValidate();
  90. $validate->scene('set_nums')->check([
  91. 'nums' => $nums,
  92. ]);
  93. $row = Carts::with(['Goods', 'Sku'])->where('id', $id)->where('user_id', $this->auth->id)->find();
  94. if (!$row) {
  95. $this->error('未找到记录');
  96. }
  97. if ($row->goods_sku_id) {
  98. if (!$row->sku) {
  99. $this->error('商品不存在');
  100. }
  101. if ($row->sku->stocks < $nums) {
  102. $this->error('库存不足');
  103. }
  104. } else {
  105. if (!$row->goods) {
  106. $this->error('商品不存在');
  107. }
  108. if ($row->goods->stocks < $nums) {
  109. $this->error('库存不足');
  110. }
  111. }
  112. $row->nums = $nums;
  113. $row->save();
  114. $this->success('操作成功');
  115. }
  116. //购物车列表
  117. public function index()
  118. {
  119. $ids = $this->request->param('ids');
  120. $list = CartService::getGoodsList($ids, $this->auth->id);
  121. //没有商品过滤数据
  122. foreach ($list as $key => $item) {
  123. if (empty($item->goods)) {
  124. $item->delete();
  125. unset($list[$key]);
  126. continue;
  127. }
  128. $item['subtotal'] = bcmul($item['nums'], ($item->sku->price ?? $item->goods->price), 2);
  129. $item->goods->visible(explode(',', 'id,title,price,image,marketprice'));
  130. }
  131. $this->success('', $list);
  132. }
  133. //获取购物车的数量
  134. public function cart_nums()
  135. {
  136. $total = Carts::where('user_id', $this->auth->id)->count();
  137. $this->success('', $total);
  138. }
  139. /**
  140. * 选中或取消商品
  141. *
  142. */
  143. public function checked()
  144. {
  145. $cartIds = $this->request->post('cartIds/a');
  146. $isChecked = $this->request->post('isChecked/d');
  147. $userId = $this->auth->id;
  148. // 验证器
  149. $validate = new CartValidate();
  150. $validate->scene('checked')->check([
  151. 'cartIds' => $cartIds,
  152. 'isChecked' => $isChecked,
  153. ]);
  154. if ($validate->getError()) {
  155. $this->error($validate->getError());
  156. }
  157. CartService::updateChecked(
  158. $userId,
  159. $cartIds,
  160. $isChecked == 1);
  161. $this->success('操作成功');
  162. }
  163. }