Buycar.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\BuycarModel;
  4. use app\api\model\Commoditycolor;
  5. use app\common\controller\Api;
  6. /**
  7. * 购物车
  8. */
  9. class Buycar extends APi
  10. {
  11. protected $noNeedLogin = '*';
  12. protected $noNeedRight = '*';
  13. /**
  14. * 购物车显示
  15. * @param string $user_id 用户id
  16. */
  17. public function buyCarIndex()
  18. {
  19. $parames = $this->request->post();
  20. $rules = [
  21. 'user_id' => 'require|number'
  22. ];
  23. $msg = [
  24. 'user_id.require' => '网络错误',
  25. 'user_id.number' => '网络错误',
  26. ];
  27. $validata = $this->validate($parames, $rules, $msg);
  28. if (is_string($validata)) {
  29. return $this->result($validata, [], 100);
  30. }
  31. $where = array(
  32. 'user_id' => $parames['user_id']
  33. );
  34. $data = BuycarModel::with('commodity')
  35. ->where($where)
  36. ->select();
  37. // $data = BuycarModel::where('user_id',$parames['user_id'])
  38. // ->alias('b')
  39. // ->join('commodity c','b.c_id=c.c_id')
  40. // ->select();
  41. if ($data) {
  42. return $this->result('', $data, 200);
  43. } else {
  44. return $this->result('暂无数据', [], 100);
  45. }
  46. }
  47. /**
  48. * 购物车添加
  49. * @param string $user_id 用户id
  50. * @param string $c_id 商品c_id
  51. * @param string $p_id 规格id
  52. * @param string $p_name 规格名
  53. * @param string $colorid 颜色id
  54. * @param string $name 颜色名
  55. * @param string $number 数量
  56. * @param string $color 颜色的图片
  57. */
  58. public function buyCarAdd()
  59. {
  60. $params = $this->request->post();
  61. $rules = [
  62. 'user_id' => 'require|number',
  63. 'c_id' => 'require|number',
  64. 'p_id' => 'require|number',
  65. 'colorid' => 'require|number',
  66. 'p_name' => 'require',
  67. 'name' => 'require',
  68. 'number' => 'require|number',
  69. 'color' => 'require',
  70. ];
  71. $msg = [
  72. 'user_id.require' => '网络错误',
  73. 'user_id.number' => '网络错误',
  74. 'c_id.require' => '网络错误',
  75. 'c_id.number' => '网络错误',
  76. 'p_id.require' => '网络错误',
  77. 'p_id.number' => '网络错误',
  78. 'colorid.number' => '网络错误',
  79. 'colorid.require' => '网络错误',
  80. 'p_name.require' => '网络错误',
  81. 'name.require' => '网络错误',
  82. 'number.require' => '网络错误',
  83. 'number.number' => '网络错误',
  84. 'color.require' => '网络错误',
  85. ];
  86. $validata = $this->validate($params, $rules, $msg);
  87. if (is_string($validata)) {
  88. return $this->result($validata, [], 100);
  89. }
  90. // 查出颜色信息
  91. $color = Commoditycolor::where('colorid',$params['colorid'])->find();
  92. $list = $params;
  93. unset($list['number']);
  94. unset($list['token']);
  95. $issetcar = BuycarModel::where($list)->find(); //判断购物车是否已存在此商品
  96. $params['money'] = $color['money'];
  97. $params['kucun'] = $color['kucun'];
  98. if ($issetcar) {
  99. return $this->result('购物车已经存在此商品了','',100);
  100. // //存在的话把数量加上
  101. // $addnumber = BuycarModel::where('k_id',$issetcar['k_id'])->setInc('number',$params['number']);
  102. // //存在的话把金额加上
  103. // $addmoney = BuycarModel::where('k_id',$issetcar['k_id'])->setInc('money',$color['money']);
  104. // if ($addnumber&&$addmoney) {
  105. // return $this->result('添加成功', '', 200);
  106. // } else {
  107. // return $this->result('添加失败', '', 100);
  108. // }
  109. }
  110. $model = new BuycarModel();
  111. $addCar = $model->allowField(true)->save($params);
  112. if ($addCar) {
  113. return $this->result('添加成功', '', 200);
  114. } else {
  115. return $this->result('添加失败', '', 100);
  116. }
  117. }
  118. /**
  119. * 购物车批量删除
  120. * @param string $list 购物车id(数组)
  121. */
  122. public function carDel () {
  123. $params = $this->request->post();
  124. $rules = [
  125. 'list' => 'require'
  126. ];
  127. $msg = [
  128. 'list.require' => '网络错误'
  129. ];
  130. $validata = $this->validate($params,$rules,$msg);
  131. if (is_string($validata)) {
  132. return $this->result($validata,[],100);
  133. }
  134. foreach ($params['list'] as $v) {
  135. $del = BuycarModel::where('k_id',$v)->delete();
  136. }
  137. return $this->result('删除成功',[],200);
  138. }
  139. }