Trolley.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\StoreGoods;
  4. use app\common\model\StoreGoodsItem;
  5. use app\common\service\ExpressService;
  6. use think\Db;
  7. use app\common\model\ShoppingTrolley;
  8. use app\common\model\DeliveryAddress;
  9. use think\Exception;
  10. /**
  11. * @title 购物车
  12. * @controller Trolley
  13. * @group base
  14. */
  15. class Trolley extends Base
  16. {
  17. protected $need_login = [
  18. 'getTrolleyList',
  19. 'addTrolley',
  20. 'changeTrolleyNum',
  21. 'cancelTrolleyList',
  22. 'getTrolleyByGoodsId',
  23. 'confirmTrolleyList',
  24. ];
  25. public function initialize()
  26. {
  27. parent::initialize();
  28. parent::setUid();
  29. }
  30. /**
  31. * @title 获取购物车商品数量
  32. * @desc 获取购物车商品数量
  33. * @author qc
  34. * @url /api/Trolley/getTrolleyNum
  35. * @method GET
  36. * @header name:Authorization require:1 desc:Token
  37. * @param name:goods_id type:int default:-- desc:商品id(商品id不传查全部的)
  38. * @return name:total_num type:int default:-- desc:购物车商品数量
  39. */
  40. public function getTrolleyNum()
  41. {
  42. $where = [];
  43. $where['user_id'] = $this->user_id;
  44. if($goods_id = input('get.goods_id',0)) $where['goods_id'] = $goods_id;
  45. $total_num = ShoppingTrolley::where($where)->sum('num');
  46. $this->success('ok',['total_num'=>numTransform($total_num)]);
  47. }
  48. /**
  49. * @title 获取购物车列表
  50. * @desc 获取购物车列表
  51. * @author qc
  52. * @url /api/Trolley/getTrolleyList
  53. * @method GET
  54. * @header name:Authorization require:1 desc:Token
  55. * @return name:id type:int default:-- desc:购物车记录id
  56. * @return name:goods_name type:string default:-- desc:商品名称
  57. * @return name:goods_status type:int default:-- desc:商品状态(1上架,0下架)
  58. * @return name:cover type:string default:-- desc:商品封面
  59. * @return name:deleted_status type:string default:-- desc:商品删除状态(1删除,0未删除)
  60. * @return name:num type:int default:-- desc:数量
  61. * @return name:goods_id type:int default:-- desc:商品id
  62. * @return name:spec_id type:int default:-- desc:规格id
  63. * @return name:goods_spec type:int default:-- desc:规格
  64. * @return name:goods_no type:int default:-- desc:货号
  65. * @return name:stock type:int default:-- desc:剩余规格库存
  66. * @return name:original_price type:float default:-- desc:规格原价(下划线价格)
  67. * @return name:sell_price type:float default:-- desc:规格售价
  68. */
  69. public function getTrolleyList()
  70. {
  71. $join_field = 't.id,t.num,t.spec_id,t.goods_id,i.goods_spec,g.cover,i.goods_no,i.is_deleted,i.original_price,i.sell_price,i.weight,i.stock,g.name goods_name,g.status goods_status,g.is_deleted deleted_status';
  72. $list = ShoppingTrolley::field($join_field)
  73. ->alias('t')
  74. ->where(['t.user_id'=>$this->user_id])
  75. ->leftJoin('StoreGoodsItem i','i.id = t.spec_id')
  76. ->leftJoin('StoreGoods g','t.goods_id = g.id')
  77. ->order('t.id desc')
  78. ->select()->toArray();
  79. /*foreach ($list as &$v) {
  80. $v['goods_spec'] = str_replace(['::',';;'],[':',';'],$v['goods_spec']);
  81. }*/
  82. $total_num = ShoppingTrolley::where(['t.user_id'=>$this->user_id])->leftJoin('StoreGoodsItem i','i.id = t.spec_id')->leftJoin('StoreGoods g','t.goods_id = g.id')->count();
  83. $this->success('ok',['list'=>$list,'total_count'=>$total_num]);
  84. }
  85. /**
  86. * @title 商品添加购物车(按规格加入)
  87. * @desc 商品某个规格加入多少
  88. * @author qc
  89. * @url /api/Trolley/addTrolley
  90. * @method POST
  91. * @header name:Authorization require:1 desc:Token
  92. * @param name:goods_id type:int require:1 default:0 desc:商品id
  93. * @param name:spec_id type:int require:1 default:0 desc:规格id
  94. * @param name:num type:int default:1 desc:新增数量
  95. */
  96. public function addTrolley()
  97. {
  98. $goods_id = input('post.goods_id');
  99. $spec_id = input('post.spec_id');
  100. $num = input('post.num',1);
  101. if(!$goods_id || !$spec_id) $this->error('请选择商品');
  102. if($num <=0 ) $this->error('添加数量有误');
  103. Db::startTrans();
  104. try {
  105. // 商品详情
  106. $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
  107. return $query->where('id',$spec_id)->where('is_deleted',0);
  108. }])->where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
  109. if(!$goods_info) throw new Exception('商品已下架');
  110. $goods_info = $goods_info->toArray();
  111. if(empty($goods_info['item_list'])) throw new Exception('该规格已下架');
  112. if($goods_info['item_list'][0]['stock'] < $num) throw new Exception('库存不足');
  113. $trolley_id = ShoppingTrolley::checkTrolley($this->user_id,$goods_id,$spec_id);
  114. // 添加到购物车
  115. if($trolley_id) {
  116. ShoppingTrolley::where('id',$trolley_id)->setInc('num',$num);
  117. }else{
  118. $trolley_info = [
  119. 'user_id' => $this->user_id,
  120. 'goods_id' => $goods_id,
  121. 'spec_id' => $spec_id,
  122. 'goods_no' => $goods_info['item_list'][0]['goods_no'],
  123. 'num' => $num,
  124. ];
  125. ShoppingTrolley::create($trolley_info);
  126. }
  127. Db::commit();
  128. }catch (\Exception $e){
  129. $this->is_commit = false;
  130. $this->ret_msg = $e->getMessage();
  131. Db::rollback();
  132. }
  133. $this->is_commit ? $this->success('添加成功'):$this->error($this->ret_msg);
  134. }
  135. /**
  136. * @title 修改购物车商品数量
  137. * @desc 修改购物车商品数量
  138. * @author qc
  139. * @url /api/Trolley/changeTrolleyNum
  140. * @method POST
  141. * @header name:Authorization require:1 desc:Token
  142. * @param name:trolley_id type:int require:1 default:0 desc:购物车记录id
  143. * @param name:num type:int require:1 default:1 desc:变更数量
  144. * @param name:type type:int require:1 default:1 desc:变更类型(1增加2扣减)
  145. */
  146. public function changeTrolleyNum()
  147. {
  148. $trolley_id = input('post.trolley_id');
  149. $num = input('post.num',1);
  150. $type = input('post.type',1);
  151. if(!in_array($type,[1,2])) $this->error('变更可类型错误');
  152. Db::startTrans();
  153. try {
  154. $trolley_info = ShoppingTrolley::get($trolley_id);
  155. if(!$trolley_info) $this->exception('记录不存在!');
  156. $goods_info = StoreGoods::getGoodsSpec($trolley_info->goods_id,$trolley_info->spec_id);
  157. if($type == 1){
  158. if(!$goods_info) $this->exception('商品已下架');
  159. if(empty($goods_info['item_list'])) $this->exception('该规格已下架');
  160. if($goods_info['item_list'][0]['stock'] < $num)$this->exception('库存不足');
  161. ShoppingTrolley::where('id',$trolley_id)->setInc('num',$num);
  162. }else{
  163. if($trolley_info->num < $num) $this->exception('扣减数量有误');
  164. if($trolley_info->num > $num){
  165. ShoppingTrolley::where('id',$trolley_id)->setDec('num',$num);//扣减数量
  166. }else{
  167. ShoppingTrolley::where('id',$trolley_id)->delete();//删除记录
  168. }
  169. }
  170. Db::commit();
  171. }catch (\Exception $e){
  172. $this->ret_msg = $e->getMessage();
  173. $this->is_commit = false;
  174. Db::rollback();
  175. }
  176. $this->is_commit ? $this->success('操作成功') : $this->error($this->ret_msg);
  177. }
  178. /**
  179. * @title 修改购物车商品规格
  180. * @desc 修改购物车规格
  181. * @author qc
  182. * @url /api/Trolley/changeTrolleySpec
  183. * @method POST
  184. * @header name:Authorization require:1 desc:Token
  185. * @param name:trolley_id type:int require:1 default:0 desc:购物车记录id
  186. * @param name:goods_id type:int require:1 default:1 desc:商品id
  187. * @param name:spec_id type:int require:1 default:0 desc:规格id
  188. * @param name:num type:int require:1 default:1 desc:添加数量
  189. */
  190. public function changeTrolleySpec()
  191. {
  192. $change_id = input('post.trolley_id');
  193. $goods_id = input('post.goods_id');
  194. $spec_id = input('post.spec_id');
  195. $num = input('post.num',1);
  196. if(!$goods_id || !$spec_id) $this->error('请选择商品');
  197. if($num <=0 ) $this->error('添加数量有误');
  198. Db::startTrans();
  199. try {
  200. // 商品详情
  201. $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
  202. return $query->where('id',$spec_id)->where('is_deleted',0);
  203. }])->where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
  204. if(!$goods_info) throw new Exception('商品已下架');
  205. $goods_info = $goods_info->toArray();
  206. if(empty($goods_info['item_list'])) throw new Exception('该规格已下架');
  207. if($goods_info['item_list'][0]['stock'] < $num) throw new Exception('库存不足');
  208. $trolley_id = ShoppingTrolley::checkTrolley($this->user_id,$goods_id,$spec_id);
  209. // 添加到购物车
  210. if($trolley_id != $change_id) {
  211. ShoppingTrolley::where('id',$trolley_id)->setInc('num',$num);
  212. ShoppingTrolley::where('id',$change_id)->delete();// 删除购物车
  213. }else{
  214. $trolley_info = [
  215. 'user_id' => $this->user_id,
  216. 'goods_id' => $goods_id,
  217. 'spec_id' => $spec_id,
  218. 'goods_no' => $goods_info['item_list'][0]['goods_no'],
  219. 'num' => $num,
  220. ];
  221. ShoppingTrolley::where('id',$change_id)->update($trolley_info);
  222. }
  223. Db::commit();
  224. }catch (\Exception $e){
  225. $this->is_commit = false;
  226. $this->ret_msg = $e->getMessage();
  227. Db::rollback();
  228. }
  229. $this->is_commit ? $this->success('添加成功'):$this->error($this->ret_msg);
  230. }
  231. /**
  232. * @title 删除购物车记录
  233. * @desc 删除购物车记录
  234. * @author qc
  235. * @url /api/Trolley/cancelTrolleyList
  236. * @method POST
  237. * @header name:Authorization require:1 desc:Token
  238. * @param name:ids type:string require:1 default:0 desc:购物车记录id(多条用逗号隔开)
  239. */
  240. public function cancelTrolleyList()
  241. {
  242. $ids = input('post.ids');
  243. if(!$ids) $this->error('请选择要删除的数据');
  244. $trolley_list = ShoppingTrolley::where('id','in',$ids)->select();
  245. ShoppingTrolley::where('id','in',$ids)->delete();
  246. $this->success('删除成功');
  247. }
  248. /**
  249. * @title 某商品是否添加到购物
  250. * @desc 获取某商品添加到购物车规格及数量
  251. * @author qc
  252. * @url /api/Trolley/getTrolleyByGoodsId
  253. * @method GET
  254. * @header name:Authorization require:1 desc:Token
  255. * @param name:goods_id type:int default:1 desc:商品id
  256. * @return name:spec_id type:int default:-- desc:规格id
  257. * @return name:num type:int default:-- desc:数量
  258. */
  259. public function getTrolleyByGoodsId()
  260. {
  261. $goods_id = input('get.goods_id');
  262. $list = ShoppingTrolley::field('id,goods_id,spec_id,goods_no,num')
  263. ->where(['user_id'=>$this->user_id,'goods_id'=>$goods_id])
  264. ->order('id desc')
  265. ->select();
  266. $this->success('ok',['list'=>$list?$list->toArray() : null ]);
  267. }
  268. }