Trolley.php 13 KB

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