Cart.php 4.1 KB

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