Cart.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\goods\Cart as CartModel;
  4. class Cart extends BaseApi
  5. {
  6. /**
  7. * 添加信息
  8. */
  9. public function add()
  10. {
  11. $token = $this->checkToken();
  12. if ($token['code'] < 0) return $this->response($token);
  13. $sku_id = isset($this->params['sku_id']) ? $this->params['sku_id'] : 0;
  14. $num = isset($this->params['num']) ? $this->params['num'] : 0;
  15. if (empty($sku_id)) {
  16. return $this->response($this->error('', 'REQUEST_SKU_ID'));
  17. }
  18. if (empty($num)) {
  19. return $this->response($this->error('', 'REQUEST_NUM'));
  20. }
  21. $cart = new CartModel();
  22. $data = [
  23. 'site_id' => $this->params['site_id'],
  24. 'member_id' => $token['data']['member_id'],
  25. 'sku_id' => $sku_id,
  26. 'num' => $num,
  27. 'create_time'=>time()
  28. ];
  29. $res = $cart->addCart($data);
  30. return $this->response($res);
  31. }
  32. /**
  33. * 编辑信息
  34. */
  35. public function edit()
  36. {
  37. $token = $this->checkToken();
  38. if ($token['code'] < 0) return $this->response($token);
  39. $cart_id = isset($this->params['cart_id']) ? $this->params['cart_id'] : 0;
  40. $num = isset($this->params['num']) ? $this->params['num'] : 0;
  41. if (empty($cart_id)) {
  42. return $this->response($this->error('', 'REQUEST_CART_ID'));
  43. }
  44. if (empty($num)) {
  45. return $this->response($this->error('', 'REQUEST_NUM'));
  46. }
  47. $cart = new CartModel();
  48. $data = [
  49. 'cart_id' => $cart_id,
  50. 'member_id' => $token['data']['member_id'],
  51. 'num' => $num
  52. ];
  53. $res = $cart->editCart($data);
  54. return $this->response($res);
  55. }
  56. /**
  57. * 删除信息
  58. */
  59. public function delete()
  60. {
  61. $token = $this->checkToken();
  62. if ($token['code'] < 0) return $this->response($token);
  63. $cart_id = isset($this->params['cart_id']) ? $this->params['cart_id'] : 0;
  64. if (empty($cart_id)) {
  65. return $this->response($this->error('', 'REQUEST_CART_ID'));
  66. }
  67. $cart = new CartModel();
  68. $data = [
  69. 'cart_id' =>$cart_id,
  70. 'member_id' => $token['data']['member_id']
  71. ];
  72. $res = $cart->deleteCart($data);
  73. return $this->response($res);
  74. }
  75. /**
  76. * 清空购物车
  77. */
  78. public function clear()
  79. {
  80. $token = $this->checkToken();
  81. if ($token['code'] < 0) return $this->response($token);
  82. $cart = new CartModel();
  83. $data = [
  84. 'member_id' => $token['data']['member_id']
  85. ];
  86. $res = $cart->clearCart($data);
  87. return $this->response($res);
  88. }
  89. /**
  90. * 列表信息
  91. */
  92. public function lists()
  93. {
  94. $token = $this->checkToken();
  95. if ($token['code'] < 0) return $this->response($token);
  96. $cart = new CartModel();
  97. $goods_model = new \app\model\goods\Goods();
  98. $list = $cart->getCart($token['data']['member_id']);
  99. foreach ($list['data'] as &$v){
  100. $gods_price =$goods_model->getGoodsInfo([['goods_id','=',$v['goods_id']]],'pay_num1,pay_num2,pay_num3,price1,price2,price3');
  101. if($v['num']>=$gods_price['data']['pay_num1']){
  102. $v['price'] =$gods_price['data']['price1'];
  103. }
  104. if($v['num']>=$gods_price['data']['pay_num2']){
  105. $v['price'] =$gods_price['data']['price2'];
  106. }
  107. if($v['num']>=$gods_price['data']['pay_num3']){
  108. $v['price'] =$gods_price['data']['price3'];
  109. }
  110. $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
  111. }
  112. return $this->response($list);
  113. }
  114. /**
  115. * 获取购物车数量
  116. * @return string
  117. */
  118. public function count()
  119. {
  120. $token = $this->checkToken();
  121. if ($token['code'] < 0) return $this->response($token);
  122. $cart = new CartModel();
  123. $list = $cart->getCartCount($token['data']['member_id']);
  124. return $this->response($list);
  125. }
  126. }