123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace app\api\controller;
- use app\common\model\StoreGoods;
- use app\common\model\StoreGoodsItem;
- use app\common\model\User;
- 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
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @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'=>$total_num]);
- }
- /**
- * @title 获取购物车列表
- * @desc 获取购物车列表
- * @author qc
- * @url /api/Trolley/getTrolleyList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:goods_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:deleted_status type:string default:-- desc:删除状态(1删除,0未删除)
- * @return name:item_list type:array default:-- desc:添加该商品的规格列表
- * @return name:item_list.id type:int default:-- desc:购物车记录id
- * @return name:item_list.num type:int default:-- desc:数量
- * @return name:item_list.goods_id type:int default:-- desc:商品id
- * @return name:item_list.spec_id type:int default:-- desc:规格id
- * @return name:item_list.goods_no type:int default:-- desc:货号
- * @return name:item_list.spec_title type:string default:-- desc:规格名称
- * @return name:item_list.cover type:string default:-- desc:规格封面
- * @return name:item_list.original_price type:float default:-- desc:规格原价(下划线价格)
- * @return name:item_list.sell_price type:float default:-- desc:规格售价
- * @return name:total_num type:int default:-- desc:购物车商品数量
- */
- public function getTrolleyList()
- {
- $trolley_goods = ShoppingTrolley::field('goods_id')
- ->where('user_id',$this->user_id)->group('goods_id')
- ->select();
- if(!$trolley_goods) $this->success('',['list'=>[]]);
- $goods_ids = array_column($trolley_goods->toArray(),'goods_id');
- $list = [];
- $total_num = 0;
- $goods_list = StoreGoods::where('id','in',$goods_ids)->select()->toArray();
- foreach ($goods_list as $goods_info){
- $trolley_data = ['goods_id'=>$goods_info['id'],'cover'=>$goods_info['cover'],'goods_name'=>$goods_info['name'],'goods_status'=>$goods_info['status'],'deleted_status'=>$goods_info['is_deleted'],'is_combo'=>$goods_info['is_combo'],'full_money'=>0,'sub_money'=>0];
- $join_field = 't.id,t.num,t.spec_id,t.goods_id,i.goods_spec,i.cover,i.goods_no,i.spec_title,i.stock,i.is_deleted,i.original_price,i.sell_price,i.weight';
- $trolley_data['item_list'] = ShoppingTrolley::field($join_field)->alias('t')
- ->where('t.goods_id',$goods_info['id'])
- ->leftJoin('StoreGoodsItem i','i.id = t.spec_id')
- ->select()->toArray();
- $total_num += array_sum(array_column($trolley_data,'num'));
- $list[] = $trolley_data;
- }
- $this->success('ok',['list'=>$list,'total_num'=>$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['is_presell']) 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);
- }
- // 扣减库存
- StoreGoods::where('id',$goods_id)->setDec('stock',$num);
- StoreGoodsItem::where('id',$spec_id)->setDec('stock',$num);
- 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
- * @return name:num type:int require:1 default:1 desc:变更数量
- * @return name:type type:intrequire: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);
- StoreGoods::where('id',$trolley_info->goods_id)->setDec('stock',$num);// 扣减商品库存
- StoreGoodsItem::where('id',$trolley_info->spec_id)->setDec('stock',$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();//删除记录
- }
- StoreGoods::where('id',$trolley_info->goods_id)->setInc('stock',$num);//增加商品库存
- StoreGoodsItem::where('id',$trolley_info->spec_id)->setInc('stock',$num);//增加规格库存
- }
- 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/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();
- Db::startTrans();
- foreach ($trolley_list as $trolley_info) {
- StoreGoods::where('id',$trolley_info->goods_id)->setInc('stock',$trolley_info->num);//增加商品库存
- StoreGoodsItem::where('id',$trolley_info->spec_id)->setInc('stock',$trolley_info->num);//增加规格库存
- }
- ShoppingTrolley::where('id','in',$ids)->delete();
- Db::commit();
- $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() : [] ]);
- }
- /**
- * @title 购物车订单确认
- * @desc 购物车订单确认
- * @author qc
- * @url /api/Trolley/confirmTrolleyList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:ids type:int default:1 desc:购物车记录id(多条用逗号隔开)
- * @return name:goods_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:deleted_status type:string default:-- desc:删除状态(1删除,0未删除)
- * @return name:item_list type:array default:-- desc:添加该商品的规格列表
- * @return name:item_list.spec_id type:int default:-- desc:规格id
- * @return name:item_list.goods_no type:int default:-- desc:货号
- * @return name:item_list.goods_spec type:array default:-- desc:规格详情
- * @return name:item_list.goods_spec.spec_title type:string default:-- desc:规格名称
- * @return name:item_list.goods_spec.cover type:string default:-- desc:规格封面
- * @return name:item_list.goods_spec.original_price type:float default:-- desc:规格原价
- * @return name:item_list.goods_spec.sell_price type:float default:-- desc:规格售价
- * @return name:wallet type:float default:-- desc:金额
- * @return name:address_info type:array default:-- desc:默认收货地址
- * @return name:wallet type:array default:-- desc:钱包
- * @return name:walle.integral type:int default:-- desc:积分
- * @return name:walle.money type:int default:-- desc:佣金余额
- */
- public function confirmTrolleyList()
- {
- $ids = input('get.ids');
- $trolley_goods = ShoppingTrolley::field('goods_id')
- ->where('user_id',$this->user_id)
- ->where('id','in',$ids)
- ->group('goods_id')
- ->select();
- if(!$trolley_goods) $this->error('请选择商品');
- $goods_ids = array_column($trolley_goods->toArray(),'goods_id');
- $list = [];
- $total_money = 0;
- $goods_list = StoreGoods::where('id','in',$goods_ids)->select()->toArray();
- foreach ($goods_list as $goods_info){
- $trolley_data = ['goods_id'=>$goods_info['id'],'goods_name'=>$goods_info['name'],'goods_status'=>$goods_info['status'],'deleted_status'=>$goods_info['is_deleted'],'is_combo'=>$goods_info['is_combo']];
- $trolley_data['item_list'] = ShoppingTrolley::with(['goodsSpec'])->where('id','in',$ids)->where('goods_id',$goods_info['id'])->select()->toArray();
- foreach ($trolley_data['item_list'] as $iv){
- $total_money = bcadd(bcmul($iv['num'],$iv['goods_spec']['sell_price'],2),$total_money,2);
- }
- $list[] = $trolley_data;
- }
- $address_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
- $user_info = User::with('wallet')->field('id,level_id')->where('id',$this->user_id)->find()->toArray();
- $this->success('ok',[
- 'list'=>$list,
- 'total_money'=>$total_money,
- 'address_info'=>$address_info ? $address_info->toArray():'',
- 'wallet'=>$user_info['wallet'],
- ]);
- }
- }
|