Cart.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace addons\shop\controller;
  3. use addons\shop\model\Carts;
  4. use addons\shop\model\Goods;
  5. use addons\shop\model\Sku;
  6. use think\Config;
  7. /**
  8. * 购物车控制器
  9. * Class Cart
  10. * @package addons\shop\controller
  11. */
  12. class Cart extends Base
  13. {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. /**
  21. * 购物车首页
  22. */
  23. public function index()
  24. {
  25. // 判断是否跳转移动端
  26. $this->checkredirect('cart');
  27. $ids = $this->request->param('ids');
  28. $sceneval = $this->request->param('sceneval/d', 1);
  29. $list = Carts::getGoodsList($ids, $this->auth->id, $sceneval);
  30. //没有商品过滤数据
  31. foreach ($list as $key => $item) {
  32. if ((empty($item->goods) && empty($item->sku)) || empty($item->goods)) {
  33. $item->delete();
  34. unset($list[$key]);
  35. continue;
  36. }
  37. $item['subtotal'] = bcmul($item['nums'], ($item->sku->price ?? $item->goods->price), 2);
  38. }
  39. $cartList = $list;
  40. $this->view->assign("cartList", $cartList);
  41. Config::set('shop.title', "购物车");
  42. return $this->view->fetch('/cart');
  43. }
  44. //添加购物车
  45. public function add()
  46. {
  47. $goods_id = $this->request->post('goods_id/d'); //单规格使用
  48. $goods_sku_id = $this->request->post('goods_sku_id/d'); //多规格使用
  49. $nums = $this->request->post('nums/d', 1);
  50. $sceneval = $this->request->post('sceneval/d', 1);
  51. if (empty($goods_id) && empty($goods_sku_id)) {
  52. $this->error('参数错误');
  53. }
  54. if ($nums <= 0) {
  55. $this->error('数量必须大于0');
  56. }
  57. $goods = Goods::get($goods_id);
  58. if (!$goods || $goods['status'] != 'normal') {
  59. $this->error('未找到指定商品');
  60. }
  61. if ($goods['spec_type']) {
  62. if (!$goods_sku_id) {
  63. $this->error('请选择商品规格');
  64. }
  65. $row = Sku::with([
  66. 'goods' => function ($query) {
  67. $query->where('status', 'normal');
  68. }
  69. ])->where('id', $goods_sku_id)->find();
  70. if (empty($row) || empty($row->goods)) {
  71. $this->error('商品已下架');
  72. }
  73. if ($row->stocks < $nums) {
  74. $this->error('库存数量不足' . $nums . '件');
  75. }
  76. } else {
  77. $row = Goods::where('id', $goods_id)->where('status', 'normal')->find();
  78. if (empty($row)) {
  79. $this->error('商品已下架');
  80. }
  81. if ($row->stocks < $nums) {
  82. $this->error('库存数量不足' . $nums . '件');
  83. }
  84. }
  85. //去添加购物车
  86. $cart_id = Carts::push($goods_id, $goods_sku_id, $nums, $this->auth->id, $sceneval);
  87. $cartnums = Carts::where('user_id', $this->auth->id)->where('sceneval', 1)->count();
  88. $this->success('添加成功', '', ['cartnums' => $cartnums, 'cart_id' => $cart_id]);
  89. }
  90. //删除购物车
  91. public function del()
  92. {
  93. $id = $this->request->post('id/a');
  94. if (!$id) {
  95. $this->error('参数错误');
  96. }
  97. $status = Carts::where('user_id', $this->auth->id)->where('id', 'IN', $id)->delete();
  98. if ($status) {
  99. $this->success('删除成功');
  100. }
  101. $this->error('删除失败');
  102. }
  103. //购物车商品数量+-
  104. public function set_nums()
  105. {
  106. $id = $this->request->post('id');
  107. if (!$id) {
  108. $this->error('参数错误');
  109. }
  110. $nums = $this->request->post('nums/d');
  111. if ($nums <= 0) {
  112. $this->error('数量必须大于0');
  113. }
  114. $row = Carts::with(['Goods', 'Sku'])->where('id', $id)->where('user_id', $this->auth->id)->find();
  115. if (!$row) {
  116. $this->error('未找到记录');
  117. }
  118. if ($row->goods_sku_id) {
  119. if (!$row->sku) {
  120. $this->error('商品不存在');
  121. }
  122. if ($row->sku->stocks < $nums) {
  123. $this->error('库存不足');
  124. }
  125. } else {
  126. if (!$row->goods) {
  127. $this->error('商品不存在');
  128. }
  129. if ($row->goods->stocks < $nums) {
  130. $this->error('库存不足');
  131. }
  132. }
  133. $row->nums = $nums;
  134. $row->save();
  135. $this->success('操作成功');
  136. }
  137. }