123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace app\api\controller;
- use app\common\model\StoreGoods;
- use app\common\model\StoreGoodsItem;
- use app\common\model\UserCollect;
- use app\common\service\ExpressService;
- use think\Db;
- use app\common\model\ShoppingTrolley;
- use app\common\model\DeliveryAddress;
- use think\Exception;
- /**
- * @title 购物车
- * @controller Trolley
- * @group base
- */
- class Trolley extends Base
- {
- protected $need_login = [
- 'getTrolleyList',
- 'addTrolley',
- 'changeTrolleyNum',
- 'changeTrolleySpec',
- 'cancelTrolleyList',
- 'getTrolleyByGoodsId',
- 'trolleyToCollect',
- ];
- public function initialize()
- {
- parent::initialize();
- parent::setUid();
- }
- /**
- * @title 获取购物车商品数量
- * @desc 获取购物车商品数量
- * @author qc
- * @url /api/Trolley/getTrolleyNum
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int default:-- desc:商品id(商品id不传查全部的)
- * @return name:total_num type:int default:-- desc:购物车商品数量
- */
- public function getTrolleyNum()
- {
- $where = [];
- $where['user_id'] = $this->user_id;
- if($goods_id = input('get.goods_id',0)) $where['goods_id'] = $goods_id;
- $total_num = ShoppingTrolley::where($where)->sum('num');
- $this->success('ok',['total_num'=>numTransform($total_num)]);
- }
- /**
- * @title 获取购物车列表
- * @desc 获取购物车列表
- * @author qc
- * @url /api/Trolley/getTrolleyList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:id type:int default:-- desc:购物车记录id
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:goods_status type:int default:-- desc:商品状态(1上架,0下架)
- * @return name:cover type:string default:-- desc:商品封面
- * @return name:spec_cover type:string default:-- desc:规格图片
- * @return name:deleted_status type:string default:-- desc:商品删除状态(1删除,0未删除)
- * @return name:num type:int default:-- desc:数量
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:spec_id type:int default:-- desc:规格id
- * @return name:goods_spec type:int default:-- desc:规格
- * @return name:goods_no type:int default:-- desc:货号
- * @return name:stock type:int default:-- desc:剩余规格库存
- * @return name:original_price type:float default:-- desc:规格原价(下划线价格)
- * @return name:sell_price type:float default:-- desc:规格售价
- */
- public function getTrolleyList()
- {
- $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';
- $list = ShoppingTrolley::field($join_field)
- ->alias('t')
- ->where(['t.user_id'=>$this->user_id])
- ->leftJoin('StoreGoodsItem i','i.id = t.spec_id')
- ->leftJoin('StoreGoods g','t.goods_id = g.id')
- ->order('t.id desc')
- ->select()->toArray();
- foreach ($list as &$v) {
- $v['specs'] = json_decode($v['specs'],true);
- $v['item_list'] = StoreGoodsItem::getGoodsSpec($v['goods_id']);
- }
- $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();
- $this->success('ok',['list'=>$list,'total_count'=>$total_num]);
- }
- /**
- * @title 商品添加购物车(按规格加入)
- * @desc 商品某个规格加入多少
- * @author qc
- * @url /api/Trolley/addTrolley
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:0 desc:商品id
- * @param name:spec_id type:int require:1 default:0 desc:规格id
- * @param name:num type:int default:1 desc:新增数量
- */
- public function addTrolley()
- {
- $goods_id = input('post.goods_id');
- $spec_id = input('post.spec_id');
- $num = input('post.num',1);
- if(!$goods_id || !$spec_id) $this->error('请选择商品');
- if($num <=0 ) $this->error('添加数量有误');
- Db::startTrans();
- try {
- // 商品详情
- $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
- return $query->where('id',$spec_id)->where('is_deleted',0);
- }])->where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
- if(!$goods_info) throw new Exception('商品已下架');
- $goods_info = $goods_info->toArray();
- if(empty($goods_info['item_list'])) throw new Exception('该规格已下架');
- if($goods_info['item_list'][0]['stock'] < $num) throw new Exception('库存不足');
- $trolley_id = ShoppingTrolley::checkTrolley($this->user_id,$goods_id,$spec_id);
- // 添加到购物车
- if($trolley_id) {
- ShoppingTrolley::where('id',$trolley_id)->setInc('num',$num);
- }else{
- $trolley_info = [
- 'user_id' => $this->user_id,
- 'goods_id' => $goods_id,
- 'spec_id' => $spec_id,
- 'goods_no' => $goods_info['item_list'][0]['goods_no'],
- 'num' => $num,
- ];
- ShoppingTrolley::create($trolley_info);
- }
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->is_commit ? $this->success('添加成功'):$this->error($this->ret_msg);
- }
- /**
- * @title 修改购物车商品数量
- * @desc 修改购物车商品数量
- * @author qc
- * @url /api/Trolley/changeTrolleyNum
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:trolley_id type:int require:1 default:0 desc:购物车记录id
- * @param name:num type:int require:1 default:1 desc:变更数量
- * @param name:type type:int require:1 default:1 desc:变更类型(1增加2扣减)
- */
- public function changeTrolleyNum()
- {
- $trolley_id = input('post.trolley_id');
- $num = input('post.num',1);
- $type = input('post.type',1);
- if(!in_array($type,[1,2])) $this->error('变更可类型错误');
- Db::startTrans();
- try {
- $trolley_info = ShoppingTrolley::get($trolley_id);
- if(!$trolley_info) $this->exception('记录不存在!');
- $goods_info = StoreGoods::getGoodsSpec($trolley_info->goods_id,$trolley_info->spec_id);
- if($type == 1){
- if(!$goods_info) $this->exception('商品已下架');
- if(empty($goods_info['item_list'])) $this->exception('该规格已下架');
- if($goods_info['item_list'][0]['stock'] < $num)$this->exception('库存不足');
- ShoppingTrolley::where('id',$trolley_id)->setInc('num',$num);
- }else{
- if($trolley_info->num < $num) $this->exception('扣减数量有误');
- if($trolley_info->num > $num){
- ShoppingTrolley::where('id',$trolley_id)->setDec('num',$num);//扣减数量
- }else{
- ShoppingTrolley::where('id',$trolley_id)->delete();//删除记录
- }
- }
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->is_commit ? $this->success('操作成功') : $this->error($this->ret_msg);
- }
- /**
- * @title 修改购物车商品规格
- * @desc 修改购物车规格
- * @author qc
- * @url /api/Trolley/changeTrolleySpec
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:trolley_id type:int require:1 default:0 desc:购物车记录id
- * @param name:goods_id type:int require:1 default:1 desc:商品id
- * @param name:spec_id type:int require:1 default:0 desc:规格id
- * @param name:num type:int require:1 default:1 desc:添加数量
- */
- public function changeTrolleySpec()
- {
- $change_id = input('post.trolley_id');
- $goods_id = input('post.goods_id');
- $spec_id = input('post.spec_id');
- $num = input('post.num',1);
- if(!$goods_id || !$spec_id) $this->error('请选择商品');
- if($num <=0 ) $this->error('添加数量有误');
- Db::startTrans();
- try {
- // 商品详情
- $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
- return $query->where('id',$spec_id)->where('is_deleted',0);
- }])->where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
- if(!$goods_info) throw new Exception('商品已下架');
- $goods_info = $goods_info->toArray();
- if(empty($goods_info['item_list'])) throw new Exception('该规格已下架');
- if($goods_info['item_list'][0]['stock'] < $num) throw new Exception('库存不足');
- $trolley_id = ShoppingTrolley::checkTrolley($this->user_id,$goods_id,$spec_id);
- // 添加到购物车
- if($trolley_id && $trolley_id != $change_id) {
- ShoppingTrolley::where('id',$trolley_id)->setInc('num',$num);
- ShoppingTrolley::where('id',$change_id)->delete();// 删除购物车
- }else{
- $trolley_info = [
- 'user_id' => $this->user_id,
- 'goods_id' => $goods_id,
- 'spec_id' => $spec_id,
- 'goods_no' => $goods_info['item_list'][0]['goods_no'],
- 'num' => $num,
- ];
- ShoppingTrolley::where('id',$change_id)->update($trolley_info);
- }
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->is_commit ? $this->success('添加成功'):$this->error($this->ret_msg);
- }
- /**
- * @title 删除购物车记录
- * @desc 删除购物车记录
- * @author qc
- * @url /api/Trolley/cancelTrolleyList
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:ids type:string require:1 default:0 desc:购物车记录id(多条用逗号隔开)
- */
- public function cancelTrolleyList()
- {
- $ids = input('post.ids');
- if(!$ids) $this->error('请选择要删除的数据');
- $trolley_list = ShoppingTrolley::where('id','in',$ids)->select();
- ShoppingTrolley::where('id','in',$ids)->delete();
- $this->success('删除成功');
- }
- /**
- * @title 某商品是否添加到购物
- * @desc 获取某商品添加到购物车规格及数量
- * @author qc
- * @url /api/Trolley/getTrolleyByGoodsId
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int default:1 desc:商品id
- * @return name:spec_id type:int default:-- desc:规格id
- * @return name:num type:int default:-- desc:数量
- */
- public function getTrolleyByGoodsId()
- {
- $goods_id = input('get.goods_id');
- $list = ShoppingTrolley::field('id,goods_id,spec_id,goods_no,num')
- ->where(['user_id'=>$this->user_id,'goods_id'=>$goods_id])
- ->order('id desc')
- ->select();
- $this->success('ok',['list'=>$list?$list->toArray() : null ]);
- }
- /**
- * @title 购物车商品添加收藏
- * @desc 购物车商品添加收藏
- * @author qc
- * @url /api/Trolley/trolleyToCollect
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:ids type:int default:1 desc:购物车id(多个逗号隔开)
- */
- public function trolleyToCollect()
- {
- $list = ShoppingTrolley::field('id,goods_id,spec_id,goods_no,num')->where(['user_id'=>$this->user_id])->where('id','in',input('ids'))->select()->toArray();
- foreach ($list as $v){
- $is_collect = UserCollect::checkCollectByType($this->user_id,6,$v['goods_id'], 0);
- if(!$is_collect) UserCollect::plateCollect($this->user_id,6,$v['goods_id']);
- }
- $this->success('添加成功');
- }
- }
|