123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- namespace app\api\controller;
- use app\common\model\StoreGoods;
- use app\common\model\StoreGoodsItem;
- 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 $no_login = ['delComment'];// 不需要登录的
- public function initialize()
- {
- parent::initialize();
- $path = explode('/',$this->request->path());
- if(!in_array(end($path),$this->no_login)) 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:id type:int default:-- 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: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: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: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.goods_no,i.is_deleted,i.original_price,i.sell_price,i.weight,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();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @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);
- }
- // 扣减库存
- 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
- * @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);
- 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() : null ]);
- }
- /**
- * @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:cover type:string default:-- desc:封面
- * @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.original_price type:float default:-- desc:规格原价
- * @return name:item_list.sell_price type:float default:-- desc:规格售价
- * @return name:item_list.goods_spec type:string default:-- desc:规格
- * @return name:total_money type:float default:-- desc:商品总金额【不含运费】
- * @return name:freight type:float default:-- desc:运费
- * @return name:freight_code type:int default:1 desc:运费提示编号【1所选地址支持发货,0所选地址不支持发货】
- * @return name:freight_msg type:string default:-- desc:运费提示信息【freight_code=1忽略】
- */
- 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');
- $freight = 0;
- $list = [];
- $total_money = 0;
- $goods_list = StoreGoods::where('id','in',$goods_ids)->select()->toArray();
- $address_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
- foreach ($goods_list as $goods_info){
- $trolley_data = ['goods_id'=>$goods_info['id'],'goods_name'=>$goods_info['name'],'goods_status'=>$goods_info['status'],'cover'=>$goods_info['cover'],'deleted_status'=>$goods_info['is_deleted']];
- $trolley_data['item_list'] = ShoppingTrolley::field('t.*,i.original_price,i.sell_price,i.goods_spec')
- ->alias('t')
- ->where('t.id','in',$ids)
- ->where('t.goods_id',$goods_info['id'])
- ->leftJoin('StoreGoodsItem i','i.id = t.spec_id')
- ->select()->toArray();
- foreach ($trolley_data['item_list'] as $iv){
- $total_money = bcadd(bcmul($iv['num'],$iv['sell_price'],2),$total_money,2);
- }
- $list[] = $trolley_data;
- $fre_num = array_sum(array_column($trolley_data['item_list'],'num'));
- if($address_info) {
- $goods_freight = ExpressService::getGoodsExpressPrice($goods_info,$address_info->id,$fre_num);
- if($goods_freight['code'] !=200){
- $this->is_commit = false;
- $this->ret_msg = $goods_freight['msg'];
- }
- $freight += $goods_freight['freight']; // 商品运费
- }
- }
- $this->success('ok',[
- 'list'=>$list,
- 'total_money'=>$total_money,
- 'address_info'=>$address_info ? $address_info->toArray():null,
- 'freight' =>$freight,
- 'freight_msg' => $this->ret_msg,
- 'freight_code' => $this->is_commit ? 1 :0
- ]);
- }
- }
|