Cart.php 5.3 KB

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