Cart.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  7. * 购物车接口
  8. */
  9. class Cart extends Base
  10. {
  11. protected $noNeedLogin = [];
  12. //添加购物车
  13. public function add()
  14. {
  15. $goods_id = $this->request->post('goods_id'); //单规格使用
  16. $goods_sku_id = $this->request->post('goods_sku_id'); //多规格使用
  17. $nums = $this->request->post('nums/d', 1);
  18. // 增加验证器
  19. $validate = new \app\api\validate\Cart();
  20. $validate->scene('add')->check([
  21. 'goods_id' => $goods_id,
  22. 'goods_sku_id' => $goods_sku_id,
  23. 'nums' => $nums,
  24. ]);
  25. if ($validate->getError()) {
  26. $this->error($validate->getError());
  27. }
  28. $goods = Goods::where('id', $goods_id)->where('status', 'normal')->find();
  29. if (empty($goods)) {
  30. $this->error('商品不存在');
  31. }
  32. if ($goods['spectype'] && !$goods_sku_id) {
  33. $this->error("请选择规格");
  34. }
  35. if (empty($goods_id) && empty($goods_sku_id)) {
  36. $this->error('参数错误');
  37. }
  38. if ($nums <= 0) {
  39. $this->error('数量必须大于0');
  40. }
  41. if (!empty($goods_sku_id)) {
  42. $row = Sku::with([
  43. 'goods' => function ($query) {
  44. $query->where('status', 'normal');
  45. }
  46. ])->where('id', $goods_sku_id)->find();
  47. if (empty($row) || empty($row->goods)) {
  48. $this->error('商品已下架');
  49. }
  50. if ($row->stocks < $nums) {
  51. $this->error('库存数量不足' . $nums . '件');
  52. }
  53. } else {
  54. if ($goods->stocks < $nums) {
  55. $this->error('库存数量不足' . $nums . '件');
  56. }
  57. }
  58. //去添加购物车
  59. $cart_id = Carts::push($goods_id, $goods_sku_id, $nums, $this->auth->id);
  60. $this->success('添加成功', $cart_id);
  61. }
  62. //删除购物车
  63. public function del()
  64. {
  65. $id = $this->request->post('id');
  66. if (!$id) {
  67. $this->error('参数错误');
  68. }
  69. $status = Carts::where('user_id', $this->auth->id)->where('id', 'IN', $id)->delete();
  70. if ($status) {
  71. $this->success('删除成功');
  72. }
  73. $this->error('删除失败');
  74. }
  75. //购物车商品数量+-
  76. public function set_nums()
  77. {
  78. $id = $this->request->post('id');
  79. if (!$id) {
  80. $this->error('参数错误');
  81. }
  82. $nums = $this->request->post('nums/d');
  83. // 使用验证器验证数量
  84. $validate = new \addons\shop\validate\Cart();
  85. $validate->scene('set_nums')->check([
  86. 'nums' => $nums,
  87. ]);
  88. $row = Carts::with(['Goods', 'Sku'])->where('id', $id)->where('user_id', $this->auth->id)->find();
  89. if (!$row) {
  90. $this->error('未找到记录');
  91. }
  92. if ($row->goods_sku_id) {
  93. if (!$row->sku) {
  94. $this->error('商品不存在');
  95. }
  96. if ($row->sku->stocks < $nums) {
  97. $this->error('库存不足');
  98. }
  99. } else {
  100. if (!$row->goods) {
  101. $this->error('商品不存在');
  102. }
  103. if ($row->goods->stocks < $nums) {
  104. $this->error('库存不足');
  105. }
  106. }
  107. $row->nums = $nums;
  108. $row->save();
  109. $this->success('操作成功');
  110. }
  111. //购物车列表
  112. public function index()
  113. {
  114. $ids = $this->request->param('ids');
  115. $list = Carts::getGoodsList($ids, $this->auth->id);
  116. //没有商品过滤数据
  117. foreach ($list as $key => $item) {
  118. if (empty($item->goods)) {
  119. $item->delete();
  120. unset($list[$key]);
  121. continue;
  122. }
  123. $item['subtotal'] = bcmul($item['nums'], ($item->sku->price ?? $item->goods->price), 2);
  124. $item->goods->visible(explode(',', 'id,title,price,image,marketprice'));
  125. }
  126. $this->success('', $list);
  127. }
  128. //获取购物车的数量
  129. public function cart_nums()
  130. {
  131. $total = Carts::where('user_id', $this->auth->id)->count();
  132. $this->success('', $total);
  133. }
  134. }