CartController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\index\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Cart;
  5. use app\model\Member;
  6. use app\model\Goods;
  7. class CartController extends Base
  8. {
  9. public function list()
  10. {
  11. $cart = new Cart;
  12. $data = $cart->getlist();
  13. return $this->json(['data' => $data]);
  14. }
  15. public function total()
  16. {
  17. $cart = new Cart;
  18. $total = $cart->count_cart_total();
  19. $data['total'] = $total;
  20. return $this->json(['data' => $data]);
  21. }
  22. public function del()
  23. {
  24. $id = input('post.id', '', 'intval');
  25. $ids = input('post.ids', '', 'serach_in');
  26. if (!empty($id)) {
  27. $result = Cart::where('id', $id)->delete();
  28. } elseif (!empty($ids)) {
  29. $inids = explode(',', $ids);
  30. Cart::where(['id' => $inids])->delete();
  31. }
  32. if ($result) {
  33. $message = '删除成功';
  34. } else {
  35. $message = '删除失败';
  36. }
  37. return $this->json(['message' => $message, 'data' => $data]);
  38. }
  39. public function add()
  40. {
  41. $cart = new Cart;
  42. $param['goods_id'] = input('post.goodsId', '', 'serach_in');
  43. $param['sku'] = input('post.sku');
  44. $param['quantity'] = input('post.quantity', '', 'serach_in');
  45. if (empty($param['type'])) {
  46. $param['type'] = 'money';
  47. }
  48. $Membermob = new Member;
  49. $memberinfo = $Membermob->getUserByWechat();
  50. $param['uid'] = $memberinfo['id'];
  51. $param['weid'] = weid();
  52. //加入购物车
  53. if ($cart->add($param)) {
  54. //计算购物车商品数量
  55. $total = $cart->count_cart_total();
  56. } else {
  57. $errno = 1;
  58. $message = '更新失败';
  59. }
  60. $data['total'] = $total;
  61. return $this->json(['message' => $message, 'errno' => $errno, 'data' => $data]);
  62. }
  63. public function check()
  64. {
  65. $goodsmob = new Goods;
  66. $id = input('post.goodsId', '', 'serach_in');
  67. $sku = input('post.sku');
  68. $number = input('post.number', '', 'serach_in');
  69. $goodsPrice = $goodsmob->cartGoods(['id' => $id, 'sku' => $sku, 'quantity' => $number]);
  70. $data['price'] = $goodsPrice['price'];
  71. $data['total'] = $goodsPrice['total'];
  72. $data['points'] = $goodsPrice['total_return_points'];
  73. $data['stores'] = $goodsPrice['stores'];
  74. $data['sku'] = $goodsPrice['sku'];
  75. return $this->json(['data' => $data]);
  76. }
  77. public function quantity()
  78. {
  79. $errno = 0;
  80. $goodsmob = new Goods;
  81. $id = input('post.id', '', 'intval');
  82. $uptype = input('post.uptype', '', 'serach_in');
  83. $quantity = input('post.quantity', '', 'serach_in');
  84. if ($uptype == 'plus') {
  85. $uptype = 'jia';
  86. }
  87. if ($uptype == 'reduce') {
  88. $uptype = 'jian';
  89. }
  90. if (!empty($id)) {
  91. $cart = Cart::find($id);
  92. if ($uptype == 'jia') {
  93. $sku = $cart->sku;
  94. $goodsid = $cart->goods_id;
  95. $goods = $goodsmob->cartGoods(['id' => $goodsid, 'sku' => $sku]);
  96. if (intval($quantity) < 1) {
  97. $quantity = intval($cart->quantity) + 1;
  98. }
  99. if (intval($quantity) > intval($goods['stores'])) {
  100. $errno = 1;
  101. $message = '商品库不足';
  102. return $this->json(['message' => $message, 'errno' => $errno, 'data' => $data]);
  103. }
  104. } elseif ($uptype == 'jian') {
  105. if (intval($quantity) < 1) {
  106. $quantity = intval($cart->quantity) - 1;
  107. }
  108. if (intval(jian) < 1) {
  109. //数量小于1删除商品
  110. Cart::where('id', $id)->delete();
  111. }
  112. }
  113. $result = Cart::update(['quantity' => $quantity, 'id' => $id]);
  114. }
  115. return $this->json(['message' => $message, 'errno' => $errno, 'data' => $data]);
  116. }
  117. }