123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335 |
- <?php
- namespace app\api\controller;
- use app\common\model\CouponConfig;
- use app\common\model\DiscountActivity;
- use app\common\model\GroupActivity;
- use app\common\model\GroupGoods;
- use app\common\model\GroupGoodsItem;
- use app\common\model\GroupLeader;
- use app\common\model\OrderBill;
- use app\common\model\OrderComment;
- use app\common\model\ReductionActivity;
- use app\common\model\SeckillActivity;
- use app\common\model\SeckillGoods;
- use app\common\model\SeckillGoodsItem;
- use app\common\model\ShoppingTrolley;
- use app\common\model\DeliveryAddress;
- use app\common\model\StoreGoods;
- use app\common\model\StoreGoodsItem;
- use app\common\model\GoodsOrder;
- use app\common\model\GoodsOrderItem;
- use app\common\model\StoreOrderRefund;
- use app\common\model\User;
- use app\common\model\UserCouponList;
- use app\common\model\UserMessage;
- use app\common\model\UserWallet;
- use app\common\service\Activity;
- use app\common\service\ExpressService;
- use app\common\service\OrderCallback;
- use EasyWeChat\Factory;
- use library\tools\Data;
- use think\cache\driver\Redis;
- use think\Db;
- use think\Exception;
- use function EasyWeChat\Kernel\data_get;
- /**
- * @title 订单
- * @controller Order
- * @group base
- */
- class Order extends Base
- {
- public $int_rate = 100;
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 订单确认页面(立即购买)
- * @desc 订单确认页面
- * @author qc
- * @url /api/Order/confirmOrderImmediately
- * @method GET
- * @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:数量
- * @return name:name type:string default:-- desc:商品名称
- * @return name:cover type:string default:-- desc:商品封面图
- * @return name:desc type:string default:-- desc:商品简述(副标题)
- * @return name:freight_type type:int default:-- desc:运费类型0固定金额,1运费模板
- * @return name:freight_id type:int default:-- desc:模板id(freight_type=1)
- * @return name:postage type:float default:-- desc:运费(freight_type=0)
- * @return name:share_money type:float default:-- desc:分享返利金额
- * @return name:total_price type:float default:-- desc:订单金额
- * @return name:total_group type:float default:-- desc:拼团订单金额
- * @return name:total_seckill type:float default:-- desc:秒杀订单金额
- * @return name:total_discount type:float default:-- desc:折扣订单金额
- * @return name:item_list type:array default:-- desc:商品多规格列表
- * @return name:item_list.spec_info type:array default:-- desc:规格型号设置;
- * @return name:item_list.goods_no type:string default:-- desc:规格货号
- * @return name:item_list.goods_title:string default:-- desc:规格标题
- * @return name:item_list.cover type:string default:-- desc:规格封面
- * @return name:item_list.stock 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.seckill_price type:float default:-- desc:秒杀价(没有秒杀活动=售价)
- * @return name:item_list.group_price type:float default:-- desc:拼团价(没有拼团活动=售价)
- * @return name:item_list.discount_price type:float default:-- desc:折扣价(没有折扣活动=售价)
- * @return name:discount type:array default:-- desc:折扣活动详情(为空:商品没有正在进行的折扣活动)
- * @return name:seckill type:array default:-- desc:秒杀活动详情(为空:商品没有正在进行的秒杀活动)
- * @return name:seckill.start_time type:string default:-- desc:开始时间
- * @return name:seckill.end_time type:string default:-- desc:结束时间
- * @return name:group type:array default:-- desc:拼团活动
- * @return name:group.start_time type:string default:-- desc:开始时间
- * @return name:group.end_time type:string default:-- desc:结束时间
- * @return name:group.full_num type:int default:-- desc:成团人数
- * @return name:reduction type:array default:-- desc:满减活动
- * @return name:reduction.id type:int default:-- desc:满减活动id
- * @return name:reduction.title type:string default:-- desc:满减标题
- * @return name:reduction.start_time type:string default:-- desc:满减开始时间
- * @return name:reduction.end_time type:string default:-- desc:满减结束时间
- * @return name:reduction.act_set type:array default:-- desc:满减设置
- * @return name:reduction.act_set.full type:float default:-- desc:满多少
- * @return name:reduction.act_set.reduction type:float default:-- desc:减多少
- * @return name:goods_discount type:array default:-- desc:商品优惠
- * @return name:goods_discount.order_cash type:float default:-- desc:积分最高抵扣订单金额
- * @return name:goods_discount.coupon_list type:array default:-- desc:优惠券列表(单品可以只让用一张)
- * @return name:coupon_list.id type:int default:-- desc:优惠券列表id
- * @return name:coupon_list.low_amount type:float default:-- desc:订单最低金额
- * @return name:coupon_list.amount type:float default:-- desc:可抵扣金额
- * @return name:add_info type:array default:-- desc:默认收货地址(设置才有)
- * @return name:add_info.pro_id type:int default:-- desc:省id
- * @return name:add_info.city_id type:int default:-- desc:市id
- * @return name:add_info.county_id type:int default:-- desc:县区id
- * @return name:add_info.pro_name type:string default:-- desc:省名
- * @return name:add_info.city_name type:string default:-- desc:市名
- * @return name:add_info.county_name type:string default:-- desc:县区名
- * @return name:add_info.street_name type:string default:-- desc:街道名称
- * @return name:add_info.detail type:string default:-- desc:详细地址
- * @return name:add_info.phone type:string default:-- desc:联系电话
- * @return name:add_info.name type:string default:-- desc:联系人
- *
- */
- public function confirmOrderImmediately()
- {
- $user_id = $this->user_id;
- $goods_id = input('get.goods_id');
- $spec_id = input('get.spec_id');
- $num = input('get.num',1);
- $act_type = input('get.act_type',0);
- $act_id = input('get.act_id',0);
- $field = 'id,name,cover,desc,low_price,status,is_deleted,level_set,freight_type,postage';
- $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
- return $query->where('id',$spec_id);
- }])->field($field)->where('id',$goods_id)->find()->toArray();
- $act_data = Activity::goodsActivityList($goods_id);
- $goods_info['item_list'] = $goods_info['item_list'][0];
- // 秒杀价格
- $goods_info['item_list']['seckill_price'] = $goods_info['item_list']['sell_price'];
- if(!empty($act_data['seckill']['goods'])){
- foreach ($act_data['seckill']['goods']['item'] as $sv){
- if($sv['spec_id'] == $spec_id){
- $goods_info['item_list']['seckill_price'] = $sv['seckill_price'];
- break;
- }
- }
- }
- // 拼团价格
- $goods_info['item_list']['group_price'] = $goods_info['item_list']['sell_price'];
- if(!empty($act_data['group']['goods'])){
- foreach ($act_data['group']['goods']['item'] as $gv){
- if($gv['spec_id'] == $spec_id){
- $goods_info['item_list']['group_price'] = $gv['group_price'];
- break;
- }
- }
- }
- // 折扣活动
- $goods_info['item_list']['discount_price'] = $goods_info['item_list']['sell_price'];
- if(!empty($act_data['discount']['act'])){
- $goods_info['item_list']['discount_price'] = bcmul($goods_info['item_list']['sell_price'],$act_data['discount']['act']['discount']/100,2);
- }
- $level_set = $goods_info['level_set'] ? json_decode($goods_info['level_set'],true):[];
- // 商品优惠信息
- $goods_discount = [
- 'order_cash' => 0,
- 'express_cash' => 0,
- 'coupon_list'=> [],
- ];
- $user_info = User::field('u.id,u.level_id,w.integral,money')->alias('u')
- ->leftJoin('UserWallet w','u.id = w.user_id')
- ->where('u.id',$this->user_id)->find()->toArray();
- if($user_info['level_id'] > 0 && !empty($level_set))
- {
- $goods_discount['order_cash'] = isset($level_set[$user_info['level_id']]) ? $level_set[$user_info['level_id']]['integral_cash']:0;
- // $goods_discount['express_cash'] = isset($level_set[$user_info['level_id']]) ? $level_set[$user_info['level_id']]['integral_freight']:0;
- }
- // 优惠券 全场限时 || 全场不限时 || 商品券限时 || 商品券不限时
- $goods_discount['coupon_list'] = UserCouponList::where(function ($query)use($goods_id,$user_id){
- return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',0],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
- })->whereOr(function ($query)use($goods_id,$user_id){
- return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',0],['type','=',0]]);
- })->whereOr(function ($query)use($goods_id,$user_id){
- return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_id]]);
- })->select()->toArray();
- $goods_info['total_price']= bcmul($goods_info['item_list']['sell_price'],$num,2);
- $goods_info['total_seckill']= bcmul($goods_info['item_list']['seckill_price'],$num,2);
- $goods_info['total_group']= bcmul($goods_info['item_list']['group_price'],$num,2);
- $goods_info['total_discount']= bcmul($goods_info['item_list']['discount_price'],$num,2);
- $add_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
- $this->success('ok',['goods_info'=>$goods_info,
- 'seckill'=>$act_data['seckill']['act'],
- 'group'=>$act_data['group']['act'],
- 'discount'=>$act_data['discount']['act'],
- 'goods_discount'=>$goods_discount,
- 'user_info'=>$user_info,
- 'reduction'=>ReductionActivity::getAct(),
- 'add_info' =>$add_info ? $add_info->toArray():'',
- ]);
- }
- /**
- * @title 生成订单(立即购买)
- * @desc 立即购买场景
- * @author qc
- * @url /api/Order/createOrderImmediately
- * @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:数量
- * @param name:act_type type:int default:0 desc:活动类型(0正常购买,1限时秒杀(抢购),2拼团,3折扣)
- * @param name:act_id type:int default:0 desc:活动id(活动类型act_type>0必传)
- * @param name:leader_id type:int default:0 desc:参与团的id(为0是新开团)
- * @param name:share_user type:int default:0 desc:分享人(谁分享给你的)
- * @param name:integral_order type:int default:0 desc:积分(抵扣订单的)
- * @param name:coupon_id type:int default:0 desc:优惠券id
- * @param name:add_id type:int default:1 desc:收货地址id
- * @param name:pay_city type:int default:0 desc:本次登录的城市
- * @return name:order_id type:int default:1 desc:订单id
- */
- public function createOrderImmediately()
- {
- $goods_id = input('post.goods_id');
- $spec_id = input('post.spec_id');
- $num = input('post.num');
- $add_id = input('post.add_id');
- $act_type = input('post.act_type',0);
- $act_id = input('post.act_id',0);
- $share_user = input('post.share_user',0);
- $integral_order = input('post.integral_order',0);
- $integral_express = input('post.integral_express',0);
- $coupon_id = input('post.coupon_id',0);// 优惠券id
- $leader_id = input('post.leader_id',0);// 参与拼团的id
- if(!$goods_id || !$spec_id) $this->error('请选择商品');
- if($num <=0 ) $this->error('数量有误');
- if(!$add_id ) $this->error('请选择收货地址');
- Db::startTrans();
- try {
- $add_info = DeliveryAddress::find($add_id)->toArray();
- $order_insert = [
- 'user_id' => $this->user_id,
- 'order_no' => get_order_sn(),
- 'goods_num' => $num,
- 'pro_name' => $add_info['pro_name'],
- 'city_name' => $add_info['city_name'],
- 'county_name' => $add_info['county_name'],
- 'street_name' => $add_info['street_name'],
- 'user_name' => $add_info['name'],
- 'add_detail' => $add_info['detail'],
- 'phone' => $add_info['phone'],
- 'act_type' => $act_type,
- 'act_id' => $act_id,
- 'share_user' => $share_user,
- 'leader_id' => $leader_id,
- 'cl_ids' => $coupon_id,
- 'express_cash' => 0,
- 'coupon_cash' => 0,
- ];
- $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]['depot_item_info']['stock'] < $num) throw new Exception('库存不足');
- $order_insert['original_price'] = bcmul($num,$goods_info['item_list'][0]['original_price'],2);// 原价
- $true_cost = $goods_info['item_list'][0]['sell_price'];//实际售价
- $spec_info = $goods_info['item_list'][0];
- // 秒杀购买
- if($act_type == 1){
- if(!$act_id) $this->exception('参数错误');
- $act_info = SeckillActivity::where('id',$act_id)->find()->toArray();
- if($act_info['status'] != 1 || $act_info['is_deleted'] == 1 || strtotime($act_info['end_time'])<time()) $this->exception('活动已结束');
- if(strtotime($act_info['start_time']) > time()) $this->exception('活动尚未开始请耐心等待');
- // 商品详情
- $seckill_goods_id = SeckillGoods::where(['goods_id'=>$goods_id,'act_id'=>$act_id])->value('id');
- if(!$seckill_goods_id) $this->exception('商品信息错误');
- $item = SeckillGoodsItem::where(['act_id'=>$act_id,'goods_id'=>$goods_id,'spec_id'=>$spec_id])->find()->toArray();
- if($item['status'] != 1) $this->exception('商品已下架');
- // redis 验证库存
- // $redis_prefix = 'SECKILL_'.$act_id.'_'.$item['id'];
- // $redis = new Redis();
- //$redis_stock = $redis->get($redis_prefix.'_stock',0);
- // if($redis_stock <= 0 || $redis_stock < $num || $item['stock'] < $num) $this->exception('库存不足');
- //$redis->dec($redis_prefix.'_stock',$num);// 扣减redis库存
- $true_cost = $item['seckill_price'];
- }
- //拼团购买
- if($act_type == 2){
- if(!$act_id) $this->exception('参数错误');
- $act_info = GroupActivity::where('id',$act_id)->find()->toArray();
- if($act_info['status'] != 1 || $act_info['is_deleted'] == 1 || strtotime($act_info['end_time'])<time()) $this->exception('活动已结束');
- if(strtotime($act_info['start_time']) > time()) $this->exception('活动尚未开始请耐心等待');
- $group_goods_id = GroupGoods::where(['goods_id'=>$goods_id,'act_id'=>$act_id])->value('id');
- if(!$group_goods_id) $this->exception('商品信息错误');
- $item = GroupGoodsItem::where(['act_id'=>$act_id,'goods_id'=>$goods_id,'spec_id'=>$spec_id])->find()->toArray();
- if($item['status'] != 1) $this->exception('商品已下架');
- // redis 验证库存
- /* $redis_prefix = 'GROUP_'.$act_id.'_';
- $redis = new Redis();
- $redis_stock = $redis->get($redis_prefix.$item['id'].'_stock',0);
- if($redis_stock <= 0 || $redis_stock < $num || $item['stock'] < $num) $this->exception('库存不足');
- */
- if($leader_id){
- // if($redis->get($redis_prefix.$leader_id,0)) $this->exception('该团已有团员参与');
- // $redis->set($redis_prefix.$leader_id,1);
- GroupLeader::where(['id'=>$leader_id])->update(['status'=>3]);
- }
- // $redis->dec($redis_prefix.'_stock',$num);// 扣减redis库存
- $true_cost = $item['group_price'];
- }
- // 折扣购买
- if($act_type == 3){
- if(!$act_id) $this->exception('参数错误');
- $act_info = DiscountActivity::where('id',$act_id)->find()->toArray();
- if($act_info['status'] != 1 || $act_info['is_deleted'] == 1 || strtotime($act_info['end_time'])<time()) $this->exception('活动已结束');
- if(strtotime($act_info['start_time']) > time()) $this->exception('活动尚未开始请耐心等待');
- $true_cost = bcmul($goods_info['item_list'][0]['sell_price'] , $act_info['discount']/100,2);
- }
- $price_goods = bcmul($num,$true_cost,2); // 需要支付商品价格
- // 积分抵扣金额
- $total_integral = $integral_order + $integral_express;// 积分使用数量
- if($total_integral > 0){
- $wallet_integral = UserWallet::where(['user_id'=>$this->user_id])->value('integral');
- if($total_integral > $wallet_integral) $this->exception('积分不足');
- }
- $integral_cash = bcdiv($total_integral,$this->int_rate,2);// 积分抵扣订单金额(全部)
- if($integral_express >0) $order_insert['express_cash'] = bcdiv($integral_express,$this->int_rate,2);// 积分抵扣运费金额
- // 优惠券
- if($coupon_id){
- $coupon_info = UserCouponList::where(function ($query)use($coupon_id){
- return $query->where([['id','=',$coupon_id],['status','=',0],['coupon_type','=',0],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
- })->whereOr(function ($query)use($coupon_id){
- return $query->where([['id','=',$coupon_id],['status','=',0],['coupon_type','=',0],['type','=',0]]);
- })->whereOr(function ($query)use($goods_id,$coupon_id){
- return $query->where([['id','=',$coupon_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_id],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
- })->whereOr(function ($query)use($goods_id,$coupon_id){
- return $query->where([['id','=',$coupon_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_id],['type','=',0]]);
- })->find();
- if(!$coupon_info) $this->exception('优惠券过期');
- if($coupon_info->low_amount < $price_goods) $this->exception('优惠券使用限制');
- UserCouponList::where('id',$coupon_id)->update(['status'=>1]);
- $order_insert['cl_ids'] = $coupon_id;
- $order_insert['coupon_cash'] = $coupon_info->amount; // 优惠券抵扣金额
- }
- // 获取运费信息
- $exp_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$num);
- if($exp_data['code'] != 200 ) throw new Exception($exp_data['msg']);
- $price_express = $exp_data['freight']; // 商品运费
- if($price_express < $order_insert['express_cash'] ) $this->exception('运费抵扣金额有误');
- $redu_money = ReductionActivity::getReduction($price_goods);// 商品满减
- $total_discount = $integral_cash + $order_insert['coupon_cash'] + $redu_money;
- //订单金额 = 商品金额 + 运费 - (积分抵扣 + 优惠券 + 满减)
- $order_money = bcsub($price_goods+$price_express,$total_discount ,2);
- if($order_money < 0) $this->exception('订单金额有误');
- $order_insert['price_total'] = $order_money;
- $order_insert['price_goods'] = $price_goods;
- $order_insert['price_express'] = $price_express;
- $order_insert['integral'] = $total_integral;
- $order_insert['total_discount'] = $total_discount ;
- $order_insert['redu_money'] = $redu_money;
- $order_info = GoodsOrder::create($order_insert);
- // 扣除积分
- if($total_integral) UserWallet::userIntegralChange($this->user_id,$total_integral,'订单抵扣',1,-1,$order_info->id);
- $order_item = [
- 'user_id' => $this->user_id,
- 'order_id' => $order_info->id,
- 'goods_id' => $goods_id,
- 'goods_no' => $spec_info['goods_no'],
- 'goods_spec' => $spec_info['goods_spec'],
- 'goods_name' => $goods_info['name'],
- 'spec_title' => $spec_info['spec_title'],
- 'spec_id' => $spec_info['id'],
- 'is_combo' => $goods_info['is_combo'],
- 'cover' => $goods_info['cover'],
- 'original_price' => $spec_info['original_price'],
- 'sell_price' => $true_cost,
- 'pay_price' => $order_money,
- 'num' => $num,
- 'depot_id' => $goods_info['depot_id'],
- 'belong' => StoreGoods::getGoodsBelong($goods_id),
- ];
- GoodsOrderItem::create($order_item);
- StoreGoods::where('id',$goods_id)->setDec('stock',$num);
- StoreGoodsItem::where('id',$spec_id)->setDec('stock',$num);
- /* switch ($act_type){
- case 0: // 商城购买
- StoreGoods::where('id',$goods_id)->setDec('stock',$num);
- StoreGoodsItem::where('id',$spec_id)->setDec('stock',$num);
- break;
- case 1: // 秒杀购买
- SeckillGoodsItem::where('id',$item['id'])->setDec('stock',$num);
- break;
- case 2:// 拼团购买
- GroupGoodsItem::where('id',$item['id'])->setDec('stock',$num);
- break;
- }*/
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->is_commit ? $this->success('添加成功',['order_id'=>$order_info->id]):$this->error($this->ret_msg);
- }
- /**
- * @title 订单支付
- * @desc 订单支付
- * @author qc
- * @url /api/Order/payOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @param name:pay_type type:int require:1 default:0 desc:支付方式(1微信小程序 ,2佣金余额 ,3移动支付(支付宝),4移动支付(微信),5pc(微信扫码),6pc(支付宝扫码))
- * @return name:config type:array default:0 desc:小程序支付配置
- * @return name:config.appId type:string default:-- desc:公众账号ID(小程序支付)
- * @return name:config.signType type:string default:-- desc:签名类型(小程序支付)
- * @return name:config.paySign type:string default:-- desc:签名(小程序支付)
- * @return name:config.nonceStr type:string default:-- desc:随机字符串(小程序支付&&APP支付)
- * @return name:config.timestamp type:string default:-- desc:时间戳小程序支付&&APP支付)
- * @return name:config.partnerid type:string default:-- desc:商户号(App支付)
- * @return name:config.prepayid type:string default:-- desc:唯一支付号(App支付)
- * @return name:config.package type:string default:-- desc:package(App支付)
- * @return name:config.sign type:string default:-- desc:签名(App支付)
- * @return name:code_url type:string default:-- desc:付款码url
- */
- public function payOrder()
- {
- $order_id = input('post.order_id');
- $pay_type = input('post.pay_type');
- $order_info = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if($order_info['status'] != 0) $this->error('订单状态错误');
- if($order_info['cancel_state'] != 0 || $order_info['is_deleted'] != 0) $this->error('订单异常');
- if($order_info['price_total'] <= 0) $this->error('订单金额错误');
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();// 支付单号
- if(!$order_info['pay_no']) GoodsOrder::where('id',$order_id)->update(['pay_no'=>$pay_no]);
- $user_info = User::with('wallet')->where('id',$this->user_id)->find()->toArray();
- $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>''];
- Db::startTrans();
- try {
- switch ($pay_type){
- case 1://微信小程序
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
- $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $ret_data['config'] = $pay_config['config'];
- break;
- case 2://佣金余额
- if($user_info['wallet']['money'] < $order_info['price_total'])$this->exception('余额不足');
- // 扣除余额
- UserWallet::userMoneyChange($this->user_id,$order_info['price_total'],'订单支付',1,-1,$order_id);
- // 支付完成后回调
- $back_res = OrderCallback::goodsOrderCallBack($order_info,$pay_type);
- if(!$back_res['ret_val']) $this->exception($back_res['msg']);
- $ret_data['pay_status'] = 1;
- break;
- case 3://移动支付(支付宝)
- break;
- case 4: //4移动支付(微信)
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
- $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'APP');
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $ret_data['config'] = $pay_config['config'];
- break;
- case 5: //pc(微信扫码)
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
- $app = Factory::payment(config('app.wx_pay'));
- $result = $app->order->unify([
- 'body' => '微信扫码支付',
- 'out_trade_no' => $pay_no,
- 'total_fee' => $order_info['price_total'] * 100,
- 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
- 'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
- ]);
- if(!isset($result['code_url'])) $this->exception($result['err_code_des']?$result['err_code_des']:'获取付款码失败');
- $ret_data['code_url'] = $result['code_url'];
- break;
- case 6: //pc(支付宝扫码)
- break;
- }
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->transReturn($ret_data);
- }
- /**
- * @title 订单确认页面(购物车)
- * @desc 订单确认页面
- * @author qc
- * @url /api/Order/confirmOrderByTrolley
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:ids type:int require:1 default:0 desc:购物车记录id
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:cover type:string default:-- desc:商品封面
- * @return name:goods_status type:int default:-- desc:商品状态(1上架,0下架)
- * @return name:total_price type:float default:-- desc:该商品价格(这个商品下面所有规格商品)
- * @return name:integral_cash type:float default:-- desc:该商品积分可抵扣金额
- * @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:string default:-- desc:货号
- * @return name:item_list.spec_title type:string default:-- desc:规格名称
- * @return name:item_list.sell_price type:flaot default:-- desc:售价
- * @return name:item_list.original_trolley type:flaot default:-- desc:规格总原价(下划线用[原价*数量])
- * @return name:item_list.sell_trolley type:flaot default:-- desc:规格总价(售价*数量)
- * @return name:item_list.original_price type:float default:-- desc:规格原价(下划线价格)
- * @return name:item_list.sell_price type:float default:-- desc:规格售价
- * @return name:coupon_list type:array default:-- desc:满减券
- * @return name:coupon_list.title type:string default:-- desc:满减券标题
- * @return name:coupon_list.low_amount type:float default:-- desc:最低消费金额
- * @return name:coupon_list.amount type:float default:-- desc:抵扣金额
- * @return name:order_money type:float default:-- desc:订单商品金额(不含运费)
- * @return name:total_integral_money type:float default:-- desc:积分可抵扣金额(订单最高)
- * @return name:reduction type:array default:-- desc:满减活动
- * @return name:reduction.id type:int default:-- desc:满减活动id
- * @return name:reduction.title type:string default:-- desc:满减标题
- * @return name:reduction.start_time type:string default:-- desc:满减开始时间
- * @return name:reduction.end_time type:string default:-- desc:满减结束时间
- * @return name:reduction.act_set type:array default:-- desc:满减设置
- * @return name:reduction.act_set.full type:float default:-- desc:满多少
- * @return name:reduction.act_set.reduction type:float default:-- desc:减多少
- * @return name:redu_money type:float default:-- desc:满减金额
- * @return name:add_info type:array default:-- desc:默认收货地址(设置才有)
- * @return name:add_info.pro_id type:int default:-- desc:省id
- * @return name:add_info.city_id type:int default:-- desc:市id
- * @return name:add_info.county_id type:int default:-- desc:县区id
- * @return name:add_info.pro_name type:string default:-- desc:省名
- * @return name:add_info.city_name type:string default:-- desc:市名
- * @return name:add_info.county_name type:string default:-- desc:县区名
- * @return name:add_info.street_name type:string default:-- desc:街道名称
- * @return name:add_info.detail type:string default:-- desc:详细地址
- * @return name:add_info.phone type:string default:-- desc:联系电话
- * @return name:add_info.name type:string default:-- desc:联系人
- */
- public function confirmOrderByTrolley()
- {
- $ids = input('get.ids');
- $user_id = $this->user_id;
- // 购物车数据分组
- $trolley_goods = ShoppingTrolley::field('goods_id')->where('user_id',$this->user_id)->where('id','in',$ids)->group('goods_id')->select();
- $goods_ids = array_column($trolley_goods->toArray(),'goods_id');
- // 用户等级&&积分
- $user_info = User::field('u.id,u.level_id,w.integral,money')->alias('u')
- ->leftJoin('UserWallet w','u.id = w.user_id')
- ->where('u.id',$this->user_id)->find()->toArray();
- $list = [];
- $field = 'id,name,cover,status,is_deleted,is_combo,level_set';
- $goods_list = StoreGoods::field($field)->where('id','in',$goods_ids)->select()->toArray();
- $total_integral_money = 0;
- $order_money = 0;// 订单商品金额
- 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']];
- $level_set = $goods_info['level_set'] ? json_decode($goods_info['level_set'],true):[];
- $join_field = 't.id,t.num,t.spec_id,t.goods_id,i.goods_spec,i.goods_no,i.spec_title,i.stock,i.is_deleted,i.original_price,i.sell_price,i.weight';
- $item_list = ShoppingTrolley::field($join_field)->alias('t')
- ->where('t.goods_id',$goods_info['id'])->where('t.id','in',$ids)
- ->leftJoin('StoreGoodsItem i','i.id = t.spec_id')
- ->select()->toArray();
- foreach ($item_list as &$item) {
- $item['original_trolley'] = bcmul($item['num'],$item['original_price'],2);// 下划线价格
- $item['sell_trolley'] = bcmul($item['num'],$item['sell_price'],2);// 售价
- }
- $trolley_data['total_price'] =array_sum(array_column($item_list,'sell_trolley'));// 该商品总价格
- $trolley_data['item_list'] = $item_list;
- $order_money = $order_money + $trolley_data['total_price'];
- // 商品券
- /*$trolley_data['goods_coupon'] = UserCouponList::field('id,title,low_amount,type,coupon_type')->where(function ($query)use($goods_info,$user_id){
- return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_info['id']],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
- })->whereOr(function ($query)use($goods_info,$user_id){
- return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',1],['goods_id','=',$goods_info['id']],['type','=',0]]);
- })->select()->toArray();*/
- $goods_num = array_sum(array_column($item_list,'num'));
- if($user_info['level_id'] > 0 && !empty($level_set)) {
- $trolley_data['integral_cash'] = isset($level_set[$user_info['level_id']]) ? $level_set[$user_info['level_id']]['integral_cash']:0;
- $total_integral_money = bcadd($total_integral_money, $trolley_data['integral_cash'] ,2);
- }
- $list[] = $trolley_data;
- }
- $coupon_list = UserCouponList::field('id,title,low_amount,amount,type,coupon_type')->where(function ($query)use($user_id){
- return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',0],['type','=',1],['end','> time',date('Y-m-d')],['start','< time',date('Y-m-d')]]);
- })->whereOr(function ($query)use($user_id){
- return $query->where([['user_id','=',$user_id],['status','=',0],['coupon_type','=',0],['type','=',0]]);
- })->select()->toArray();
- $redu_money = ReductionActivity::getReduction($order_money);
- $add_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
- $this->success('ok',[
- 'list'=>$list,
- 'coupon_list'=>$coupon_list,
- 'order_money'=>$order_money,
- 'total_integral_money'=>$total_integral_money,
- 'reduction' => ReductionActivity::getAct(),
- 'redu_money' => $redu_money,
- 'add_info' => $add_info ? $add_info->toArray():'',
- ]);
- }
- /**
- * @title 生成订单(购物车[活动没有购物车])
- * @desc 购物车场景
- * @author qc
- * @url /api/Order/createOrderByTrolley
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:ids type:int require:1 default:0 desc:购物车记录id
- * @param name:add_id type:int default:1 desc:收货地址id
- * @param name:coupon_ids type:int default:1 desc:使用优惠券id(多个用逗号隔开)
- * @param name:total_integral type:int default:0 desc:使用积分数量
- * @param name:pay_city type:int default:0 desc:本次登录的城市
- * @return name:order_id type:int default:1 desc:订单id
- */
- public function createOrderByTrolley()
- {
- $ids = input('post.ids');
- $add_id = input('post.add_id');
- $coupon_ids = input('post.coupon_ids');
- $total_integral = input('post.total_integral');
- if(!$ids) $this->error('请选择商品');
- if(!$add_id ) $this->error('请选择收货地址');
- $ret_data = ['order_id'=>0];
- Db::startTrans();
- try{
- $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
- $trolley_list = ShoppingTrolley::with(['goodsSpec','goodsInfo'])->where('id','in',$ids)->select()->toArray();
- if(empty($trolley_list)) $this->exception('购物车记录有误');
- // 计算运费
- $price_express = 0;// 总运费
- $goods_list = ShoppingTrolley::where('id','in',$ids)->field('goods_id,sum(num) as total_num')->group('goods_id')->select()->toArray();
- foreach ($goods_list as $v){
- $goods_info = StoreGoods::field('name,freight_type,freight_id,postage')->where('id',$v['goods_id'])->find()->toArray();
- $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$v['total_num']);
- if($freight_data['code'] != 200){
- $this->exception($goods_info['name'].$freight_data['msg']);
- break;
- }
- $price_express += $freight_data['freight'];
- }
- // 计算运费 end
- $price_goods = 0; // 商品总金额
- $original_total = 0; // 商品总原价
- $total_num = 0;// 商品总数量
- $order_item = [];// 订单列表
- foreach ($trolley_list as $trolley)
- {
- $price_goods += $trolley['num'] * $trolley['goods_spec']['sell_price'];
- $original_total += $trolley['num'] * $trolley['goods_spec']['original_price'];
- $total_num +=$trolley['num'];
- $order_item[] = [
- 'user_id' => $this->user_id,
- 'goods_id' => $trolley['goods_id'],
- 'goods_no' => $trolley['goods_spec']['goods_no'],
- 'goods_name' => $trolley['goods_info']['name'],
- 'goods_spec' => $trolley['goods_spec']['goods_spec'],
- 'spec_title' => $trolley['goods_spec']['spec_title'],
- 'spec_id' => $trolley['spec_id'],
- 'is_combo' => $trolley['goods_info']['is_combo'],
- 'cover' => $trolley['goods_info']['cover'],
- 'original_price' => $trolley['goods_spec']['original_price'],
- 'sell_price' => $trolley['goods_spec']['sell_price'],
- 'num' => $trolley['num'],
- 'depot_id' => $trolley['goods_info']['depot_id'],
- 'trolley_id' => $trolley['id'],
- 'belong' => StoreGoods::getGoodsBelong($trolley['goods_id']),
- ];
- }
- $order_insert = [
- 'user_id' => $this->user_id,
- 'order_no' => get_order_sn(),
- 'goods_num' => $total_num,
- 'pro_name' => $add_info['pro_name'],
- 'city_name' => $add_info['city_name'],
- 'county_name' => $add_info['county_name'],
- 'street_name' => $add_info['street_name'],
- 'user_name' => $add_info['name'],
- 'add_detail' => $add_info['detail'],
- 'phone' => $add_info['phone'],
- 'integral' => $total_integral,
- 'int_cash' => 0,
- 'cl_ids' => $coupon_ids,
- 'coupon_cash' => 0,
- ];
- // 积分抵扣金额
- if($total_integral > 0){
- $wallet_integral = UserWallet::where(['user_id'=>$this->user_id])->value('integral');
- if($total_integral > $wallet_integral) $this->exception('积分不足');
- $order_insert['int_cash'] = bcdiv($total_integral,$this->int_rate,2);// 积分抵扣订单金额(全部)
- }
- if($coupon_ids){
- $coupon_list = CouponConfig::field('id,low_amount,amount')->where('id','in',$coupon_ids)
- ->where('status','=','0')->order('low_amount desc')->select()->toArray();
- if($price_goods < $coupon_list[0]['low_amount']) $this->exception('订单金额最低:'. $coupon_list[0]['low_amount']);
- $order_insert['coupon_cash'] = array_sum(array_column($coupon_list,'amount'));
- }
- if($price_goods < $order_insert['int_cash']+$order_insert['coupon_cash']) $this->exception('订单金额有误');
- $redu_money = ReductionActivity::getReduction($price_goods);// 商品满减金额
- $total_discount = $order_insert['int_cash'] + $order_insert['coupon_cash']+$redu_money;
- $price_total = bcsub($price_goods + $price_express,$total_discount,2);
- $order_insert['price_total'] = $price_total;
- $order_insert['price_goods'] = $price_goods ;
- $order_insert['original_total'] = $original_total ;
- $order_insert['price_express'] = $price_express ;
- $order_insert['total_discount'] = $total_discount ;
- $order_insert['redu_money'] = $redu_money ;
- $order_info = GoodsOrder::create($order_insert);// 生成订单
- array_walk($order_item,function (&$v,$k)use($order_info,$total_discount){
- $v['order_id'] = $order_info->id;
- if($total_discount > 0){
- $v['pay_price'] = $v['sell_price']*$v['num'] - bcmul($total_discount , bcdiv($v['sell_price']*$v['num'],$order_info->price_goods,2),2) ;
- }
- });
- (new GoodsOrderItem())->saveAll($order_item);// 生成订单商品详情
- ShoppingTrolley::where('id','in',$ids)->delete();// 删除购物车
- $ret_data['order_id'] = $order_info->id;
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->transReturn($ret_data);
- }
- /**
- * @title 计算商品运费(一个商品id)
- * @desc 计算商品运费(一个商品id)
- * @author qc
- * @url /api/Order/getGoodsFreight
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:0 desc:商品id
- * @param name:num type:int require:1 default:0 desc:商品数量
- * @param name:add_id type:int require:1 default:0 desc:收货地址id
- * @return name:freight type:float require:0 default:0 desc运费金额
- */
- public function getGoodsFreight()
- {
- $goods_id = input('get.goods_id');
- $num = input('get.num');
- $add_id = input('get.add_id');
- $goods_info = StoreGoods::where('id',$goods_id)->find()->toArray();
- $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
- $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$num);
- if($freight_data['code'] != 200 ) $this->error($freight_data['msg']);
- $this->success('ok',['freight'=>$freight_data['freight']]);
- }
- /**
- * @title 计算购物车商品运费
- * @desc 计算购物车商品运费
- * @author qc
- * @url /api/Order/getTrolleyFreight
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:ids type:int require:1 default:0 desc:购物车商品id串
- * @param name:add_id type:int require:1 default:0 desc:收货地址id
- * @return name:freight type:float require:0 default:0 desc运费金额
- */
- public function getTrolleyFreight()
- {
- $trolley_ids = input('get.ids');
- $add_id = input('get.add_id');
- $goods_list = ShoppingTrolley::where('id','in',$trolley_ids)
- ->field('goods_id,sum(num) as total_num')
- ->group('goods_id')
- ->select()->toArray();
- $total_price = 0;
- $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
- foreach ($goods_list as $v){
- $goods_info = StoreGoods::field('name,freight_type,freight_id,postage')->where('id',$v['goods_id'])->find()->toArray();
- $freight_data = ExpressService::getGoodsExpressPrice($goods_info,$add_info,$v['total_num']);
- if($freight_data['code'] != 200){
- $this->error($goods_info['name'].$freight_data['msg']);
- break;
- }
- $total_price += $freight_data['freight'];
- }
- $this->success('ok',['freight'=>$total_price]);
- }
- /**
- * @title 获取订单列表
- * @desc 订单优惠:int_cash + coupon_cash
- * @author qc
- * @method GET
- * @url /api/Order/getOrderList
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:status type:int default:-1 desc:状态
- * @param name:apply_refund type:int default:-1 desc:申请退款状态(-1全部,0未申请||已取消,1已申请)
- * @return name:id type:int default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:price_total type:float default:-- desc:待支付金额(所以优惠完价格+运费)
- * @return name:price_goods type:float default:-- desc:商品金额(用售价计算)
- * @return name:price_express type:float default:-- desc:运费金额
- * @return name:original_total type:float default:-- desc:总原价(下划线)
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
- * @return name:pay_type type:int default:-- desc:支付方式(1微信2余额3支付宝)
- * @return name:pay_no type:string default:-- desc:支付号
- * @return name:pay_at type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
- * @return name:cancel_at type:string default:-- desc:取消时间
- * @return name:cancel_desc type:string default:-- desc:取消原因
- * @return name:cancel_desc type:string default:-- desc:取消原因
- * @return name:refund_state type:int default:-- desc:退款状态(0未申请 1待审核,2审核通过,3审核拒绝)
- * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
- * @return name:express_company_title type:string default:-- desc:发货快递公司名称
- * @return name:express_send_no type:string default:-- desc:物流单号
- * @return name:express_send_at type:string default:-- desc:发货时间
- * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
- * @return name:create_at type:string default:-- desc:下单时间
- * @return name:integral type:string default:-- desc:使用积分数量
- * @return name:int_cash type:float default:-- desc:使用积分抵扣金额
- * @return name:cl_ids type:string default:-- desc:使用优惠券id串
- * @return name:coupon_cash type:string default:-- desc:使用优惠券抵扣金额
- * @return name:blance type:string default:-- desc:使用余额支付金额
- * @return name:redu_money type:float default:-- desc:订单满减金额
- * @return name:remark type:string default:-- desc:订单备注
- * @return name:goods_num type:int default:-- desc:订单商品总数量
- * @return name:pro_name type:string default:-- desc:省名称(收货地址)
- * @return name:city_name type:string default:-- desc:市名称(收货地址)
- * @return name:county_name type:string default:-- desc:县区名称(收货地址)
- * @return name:street_name type:string default:-- desc:街道名称(收货地址)
- * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
- * @return name:user_name type:string default:-- desc:收货人
- * @return name:phone type:string default:-- desc:收货人联系电话
- * @return name:act_type type:int default:-- desc:活动类型0无1秒杀2拼团
- * @return name:act_id type:int default:-- desc:活动id
- * @return name:share_user type:int default:-- desc:分享人id
- * @return name:order_item type:array default:-- desc:订单商品列表
- * @return name:order_item.name type:float default:-- desc:商品名
- * @return name:order_item.cover type:float default:-- desc:商品图
- * @return name:order_item.sell_price type:float default:-- desc:购买价格
- * @return name:order_item.goods_spec type:float default:-- desc:规格
- * @return name:order_item.num type:int default:-- desc:数量
- * @return name:order_bill type:array default:-- desc:订单开票数据(为空则未申请)
- * @return name:order_bill.header type:string require:1 default:0 desc:抬头
- * @return name:order_bill.type type:string require:1 default:0 desc:类型(string)
- * @return name:order_bill.email type:string require:1 default:0 desc:邮箱(string)
- * @return name:order_bill.bank type:string require:1 default:0 desc:开户行(string)
- * @return name:order_bill.card_no type:string require:1 default:0 desc:账号(string)
- * @return name:order_bill.phone type:string require:1 default:0 desc:电话(string)
- * @return name:order_bill.status type:int require:1 default:0 desc:0待开票1已开票
- * @return name:order_refund type:array require:1 default:0 desc:退款申请信息
- * @return name:order_refund.status type:int default:0 desc:审核状态(0待审核1同意退款2审核拒绝3通过切退款4退款异常5取消申请)
- * @return name:order_refund.is_deleted type:int default:0 desc:是否已删除(0否1是)
- */
- public function getOrderList()
- {
- $status = input('get.status',-1);
- $refund_state = input('get.apply_refund',-1);
- $where = [];
- $where[] = ['user_id','=',$this->user_id];
- if($status > -1) $where[] = ['status','=',$status];
- if($refund_state == 0) $where[] = ['refund_state','=',$refund_state];
- if($refund_state == 1) $where[] = ['refund_state','in','1,2,3,4'];
- $list = GoodsOrder::with(['orderItem','orderBill','orderRefund'])->where($where)->order('id desc ')
- ->limit($this->off_set,$this->page_num)
- ->select()->toArray();
- $this->success('',['list'=>$list]);
- }
- /**
- * @title 获取订单详情
- * @desc 获取订单详情
- * @author qc
- * @method GET
- * @url /api/Order/getOrderDetail
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int default:-- desc:订单id
- * @return name:id type:int default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:price_total type:float default:-- desc:待支付金额(所以优惠完价格+运费)
- * @return name:price_goods type:float default:-- desc:商品金额
- * @return name:price_express type:float default:-- desc:运费金额
- * @return name:original_total type:float default:-- desc:总原价(下划线)
- * @return name:original_total type:float default:-- desc:总原价(下划线)
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
- * @return name:pay_type type:int default:-- desc:支付方式(1微信2余额3支付宝)
- * @return name:pay_no type:string default:-- desc:支付号
- * @return name:pay_at type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
- * @return name:cancel_at type:string default:-- desc:取消时间
- * @return name:cancel_desc type:string default:-- desc:取消原因
- * @return name:cancel_desc type:string default:-- desc:取消原因
- * @return name:refund_state type:int default:-- desc:退款状态(0未申请1待审核,2审核拒绝,3审核通过)
- * @return name:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
- * @return name:express_company_title type:string default:-- desc:发货快递公司名称
- * @return name:express_send_no type:string default:-- desc:物流单号
- * @return name:express_send_at type:string default:-- desc:发货时间
- * @return name:status type:int default:-- desc:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,8已退款,9取消)
- * @return name:create_at type:string default:-- desc:下单时间
- * @return name:integral type:string default:-- desc:使用积分数量
- * @return name:cl_ids type:string default:-- desc:使用优惠券id串
- * @return name:coupon_cash type:string default:-- desc:使用优惠券抵扣金额
- * @return name:blance type:string default:-- desc:使用余额支付金额(余额抵扣)
- * @return name:redu_money type:float default:-- desc:订单满减金额
- * @return name:remark type:string default:-- desc:订单备注
- * @return name:goods_num type:int default:-- desc:订单商品总数量
- * @return name:pro_name type:string default:-- desc:省名称(收货地址)
- * @return name:city_name type:string default:-- desc:市名称(收货地址)
- * @return name:county_name type:string default:-- desc:县区名称(收货地址)
- * @return name:street_name type:string default:-- desc:街道名称(收货地址)
- * @return name:add_detail type:string default:-- desc:详细地址(收货地址)
- * @return name:user_name type:string default:-- desc:收货人
- * @return name:phone type:string default:-- desc:收货人联系电话
- * @return name:act_type type:int default:-- desc:活动类型0无1秒杀2拼团
- * @return name:act_id type:int default:-- desc:活动id
- * @return name:share_user type:int default:-- desc:分享人id
- * @return name:order_item type:array default:-- desc:订单商品列表(按商品id分组)
- * @return name:order_item.name type:float default:-- desc:商品名
- * @return name:order_item.cover type:float default:-- desc:商品图
- * @return name:order_item.sell_price type:float default:-- desc:购买价格
- * @return name:order_item.goods_spec type:float default:-- desc:规格
- * @return name:order_item.num type:int default:-- desc:数量
- */
- public function getOrderDetail()
- {
- $order_id = input('get.order_id');
- $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
- $detail['order_item'] = (new GoodsOrderItem())->getOrderItem($order_id);
- $this->success('',['detail'=>$detail]);
- }
- /**
- * @title 取消订单
- * @desc 取消订单
- * @author qc
- * @url /api/Order/cancelOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- */
- public function cancelOrder()
- {
- $order_id = input('post.order_id');
- Db::startTrans();
- try {
- $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if($detail['status'] != 0) $this->exception('订单状态有误');
- // 取消订单状态
- GoodsOrder::update(['status'=>9],['id'=>$order_id]);
- // 优惠券退回
- if($detail['cl_ids']) UserCouponList::where(['id','in',$detail['cl_ids']])->update(['status'=>0]);
- // 积分退回
- if($detail['integral'] > 0) UserWallet::userIntegralChange($detail['user_id'],$detail['integral'],'订单取消退回',2,1,$detail['id']);
- // 余额退回
- if($detail['blance'] > 0) UserWallet::userMoneyChange($detail['user_id'],$detail['blance'],'订单取消退回',2, 1,$detail['id']);
- // 订单处理
- foreach ($detail['order_item'] as $item_info) {
- // 更改订单详情状态
- GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
- // 商品规格库存返回
- StoreGoodsItem::where('id',$item_info['spec_id'])->setInc('stock',$item_info['num']);
- // 商品总库存增加
- StoreGoods::where('id',$item_info['goods_id'])->setInc('stock',$item_info['num']);
- }
- if($detail['act_type'] == 2 && $detail['leader_id']) {
- GroupLeader::where(['id'=>$detail['leader_id']])->update(['status'=>0]);
- }
- /* switch ($detail['act_type']){
- case 1:
- // $redis = new Redis();
- // $redis_prefix = 'SECKILL_'.$detail['act_id'].'_';
- foreach ($detail['order_item'] as $item_info) {
- // 更改订单详情状态
- GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
- // 商品规格库存返回
- $item = SeckillGoodsItem::where(['act_id'=>$detail['act_id'],'goods_id'=>$item_info['goods_id'],'spec_id'=>$item_info['spec_id']])->find()->toArray();
- SeckillGoodsItem::where(['id'=>$item['id']])->setInc('stock',$item_info['num']);
- // $redis->inc($redis_prefix.$item['id'].'_stock',$item_info['num']);//增加redis
- }
- break;
- case 2:
- // $redis = new Redis();
- //$redis_prefix = 'GROUP_'.$detail['act_id'].'_';
- //拼团锁定的退款
- if($detail['leader_id']){
- GroupLeader::where(['id'=>$detail['leader_id']])->update(['status'=>0]);
- // $redis->set( $redis_prefix.$detail['leader_id'],0);
- }
- foreach ($detail['order_item'] as $item_info) {
- // 更改订单详情状态
- GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
- // 商品规格库存返回
- $item = GroupGoodsItem::where(['act_id'=>$detail['act_id'],'goods_id'=>$item_info['goods_id'],'spec_id'=>$item_info['spec_id']])->find()->toArray();
- GroupGoodsItem::where(['id'=>$item['id']])->setInc('stock',$item_info['num']);
- // $redis->inc($redis_prefix.$item['id'].'_stock',$item_info['num']);//增加redis
- }
- break;
- }*/
- 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/Order/deleteOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- */
- public function deleteOrder()
- {
- $order_id = input('post.order_id');
- $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if($detail['status'] == 0) $this->error('请先取消订单');
- GoodsOrder::update(['is_deleted'=>1],['id'=>$order_id]);
- $this->success('删除成功');
- }
- /**
- * @title 更换订单收货地址
- * @desc 更换订单收货地址(仅待支付订单可以修改地址)
- * @author qc
- * @url /api/Order/changeOrderDelivery
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @param name:add_id type:int require:1 default:0 desc:收货地址id
- */
- public function changeOrderDelivery()
- {
- $order_id = input('post.order_id');
- $add_id = input('post.add_id');
- $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
- if($detail['status'] != 0) $this->error('操作错误');
- $add_info = DeliveryAddress::where('id',$add_id)->find()->toArray();
- $order_update =[
- 'pro_name' => $add_info['pro_name'],
- 'city_name' => $add_info['city_name'],
- 'county_name' => $add_info['county_name'],
- 'street_name' => $add_info['street_name'],
- 'add_detail' => $add_info['detail'],
- 'user_name' => $add_info['name'],
- 'phone' => $add_info['phone'],
- ];
- GoodsOrder::update($order_update,['id'=>$order_id]);
- $this->success('修改成功');
- }
- /**
- * @title 确认收货
- * @desc 确认收货
- * @author qc
- * @url /api/Order/confirmReceipt
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- */
- public function confirmReceipt()
- {
- $order_id = input('post.order_id');
- $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if($detail['status'] == 0) $this->error('订单未支付');
- if($detail['status'] == 1) $this->error('订单未发货');
- if($detail['status'] == 3) $this->error('订单已收货');
- GoodsOrder::update(['status'=>3],['id'=>$order_id]);
- GoodsOrderItem::update(['status'=>3],['order_id'=>$order_id]);
- UserMessage::create(['user_id'=>$this->user_id,'type_id'=>3,'relation_id'=>$order_id,'content'=>'订单收货成功']);
- $this->success('已确认收货');
- }
- /**
- * @title 订单评价
- * @desc 订单评价
- * @author qc
- * @url /api/Order/orderComment
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:item_id type:int require:1 default:0 desc:订单item表id
- * @param name:content type:string require:1 default:0 desc:评论内容
- * @param name:images type:string default:0 desc:图片
- * @param name:video type:string default:0 desc:视频
- * @param name:goods_core type:int default:0 desc:商品评分
- * @param name:wl_core type:int default:0 desc:物流评分
- * @param name:serve_core type:int default:0 desc:服务评分
- */
- public function orderComment()
- {
- $item_id = input('post.item_id','');
- $content = input('post.content','');
- $images = input('post.images','');
- $video = input('post.video','');
- $goods_core = input('post.goods_core','');
- $wl_core = input('post.wl_core','');
- $serve_core = input('post.serve_core','');
- $item = GoodsOrderItem::get($item_id);
- if(!$item) $this->error('订单不存在');
- $order_info = GoodsOrder::where('id',$item->order_id)->find();
- if($order_info->status < 3) $this->error('请先确认收货');
- $data = [
- 'user_id' => $this->user_id,
- 'order_id' =>$item->order_id,
- 'goods_id' =>$item->goods_id,
- 'spec_id' =>$item->spec_id,
- 'content' =>$content,
- 'images' =>$images,
- 'video' =>$video,
- 'goods_core' =>$goods_core,
- 'wl_core' =>$wl_core,
- 'serve_core' =>$serve_core,
- ];
- OrderComment::create($data);
- $this->success('评论成功');
- }
- /**
- * @title 获取退款理由
- * @desc 获取退款理由
- * @author qc
- * @url /api/Order/getRefundCase
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @param name:apply_case type:string default:0 desc:退款原因
- * @param name:images type:string default:0 desc:图片
- */
- public function getRefundCase()
- {
- $list= Db::name('order_refund_case')->order('sort desc ,id desc')->select();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 订单申请退款(重新退款||退款编辑)
- * @desc 订单申请退款
- * @author qc
- * @url /api/Order/orderRefund
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @param name:apply_case type:string default:0 desc:退款原因
- * @param name:images type:string default:0 desc:图片
- */
- public function orderRefund()
- {
- $order_id = input('post.order_id');
- $apply_case = input('post.apply_case');
- $images = input('post.images');
- $order_info = GoodsOrder::where('id',$order_id)->find()->toArray();
- if($order_info['status'] == 0) $this->error('订单未支付');
- if($order_info['refund_state'] == 1) $this->error('订单已申请,等待审核');
- if($order_info['refund_state'] == 2) $this->error('已通过审核');
- $refund_info = [
- 'user_id' => $this->user_id ,
- 'order_id' => $order_id ,
- 'num' => $order_info['goods_num'],
- 'apply_case' => $apply_case,
- 'images' => $images,
- 'is_deleted'=> 0,
- 'refund_type'=> $order_info['status'] == 1 ? :2,
- 'create_at'=> date('Y-m-d H:i:s'),
- 'refund_no'=> 'R'.$order_info['order_no'],
- ];
- Data::save('StoreOrderRefund',$refund_info,'order_id',['order_id'=>$order_id]);
- GoodsOrder::where(['id'=>$order_id])->update(['refund_state'=>1]);
- $this->success('申请成功等待审核');
- }
- /**
- * @title 取消退款申请
- * @desc 取消退款申请
- * @author qc
- * @url /api/Order/cancelApply
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:0 desc:退款记录id
- */
- public function cancelApply()
- {
- $id = input('post.id');
- $refund_info = StoreOrderRefund::where('id',$id)->find();
- if($refund_info['status'] != 0) $this->error('该记录已处理!');
- StoreOrderRefund::where('id',$id)->update(['status'=>5]);
- GoodsOrder::where('id',$refund_info['order_id'])->update(['refund_state'=>0]);
- $this->success('取消成功!');
- }
- /**
- * @title 订单退款详情
- * @desc 订单退款详情
- * @author qc
- * @url /api/Order/getRefundInfo
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @return name:apply_case type:string default:0 desc:退款原因
- * @return name:images type:string default:0 desc:图片
- * @return name:create_at type:string default:0 desc:申请时间
- * @return name:status type:int default:0 desc:0待审核1同意退款2审核拒绝(失败)3通过切退款成功4退款异常5取消申请
- * @return name:refund_type type:int default:0 desc:1退款2退款退货
- * @return name:audit_desc type:string default:0 desc:审核描述
- * @return name:refund_money type:float default:0 desc:审核退款金额
- * @return name:refund_time type:float default:0 desc:退款到账时间
- * @return name:audit_at type:float default:0 desc:审核时间
- * @return name:refund_no type:float default:0 desc:退款编号
- * @return name:express_info type:array default:0 desc:退货物流
- * @return name:express_info.express_address type:string default:0 desc:0 desc:退货地址
- * @return name:express_info.express_user type:string default:0 desc:0 desc:收件人
- * @return name:express_info.express_tel type:string default:0 desc:0 desc:电话
- * @return name:express_info.express_name type:string default:0 desc:0 desc:物流公司名称
- * @return name:express_info.express_no type:string default:0 desc:快递号
- * @return name:express_info.express_time type:string default:0 desc:时间
- */
- public function getRefundInfo()
- {
- $order_id = input('get.order_id');
- $refund_info = StoreOrderRefund::where('order_id',$order_id)
- ->field('id,apply_case,images,status,audit_desc,create_at,express_info,refund_type,refund_no,audit_at,refund_time')
- ->find()->toArray();
- if($refund_info['express_info'])$refund_info['express_info']= json_decode($refund_info['express_info'],true);
- $this->success('ok',['detail'=>$refund_info]);
- }
- /**
- * @title 完善退款物流
- * @desc 完善退款物流
- * @author qc
- * @url /api/Order/refundExpress
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:0 desc:退款记录id
- * @param name:express_address type:string default:0 desc:0 desc:退货地址
- * @param name:express_user type:string default:0 desc:0 desc:收件人
- * @param name:express_tel type:string default:0 desc:0 desc:电话
- * @param name:express_name type:string default:0 desc:0 desc:物流公司名称(退货的物流公司是用户自己填写的,,,想用哪个用哪个)
- * @param name:express_no type:string default:0 desc:快递号
- */
- public function refundExpress()
- {
- $id = input('post.id');
- $express_address = input('post.express_address');
- $express_user = input('post.express_user');
- $express_tel= input('post.express_tel');
- $express_name= input('post.express_name');
- $express_no = input('post.express_no');
- $exp_info = [
- 'express_address'=>$express_address,
- 'express_user'=>$express_user,
- 'express_tel'=>$express_tel,
- 'express_name'=>$express_name,
- 'express_no'=>$express_no,
- 'express_time'=>date("Y-m-d H:i:s"),
- ];
- StoreOrderRefund::where('id',$id)->update(['express_info'=>json_encode($exp_info)]);
- $this->success('物流信息完善成功');
- }
- /**
- * @title 申请(修改)订单发票
- * @desc 申请(修改)订单发票
- * @author qc
- * @url /api/Order/applyOrderBill
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @param name:header type:string require:1 default:0 desc:抬头
- * @param name:type type:string require:1 default:0 desc:类型(string)
- * @param name:email type:string require:1 default:0 desc:邮箱(string)
- * @param name:bank type:string require:1 default:0 desc:开户行(string)
- * @param name:card_no type:string require:1 default:0 desc:账号(string)
- * @param name:phone type:string require:1 default:0 desc:电话(string)
- */
- public function applyOrderBill()
- {
- $order_id = input('post.order_id');
- if(!$order_id) $this->error('请选择开票订单');
- $bill_status = OrderBill::where(['order_id'=>$order_id])->value('status');
- if($bill_status) $this->error('已开票');
- $bill= [
- 'user_id' => $this->user_id,
- 'order_id' => $order_id,
- 'header' => input('post.header'),
- 'type' => input('post.type'),
- 'email' => input('post.email'),
- 'card_no' => input('post.card_no'),
- 'bank' => input('post.bank'),
- 'phone' => input('post.phone')
- ];
- $bill['user_id'] = $this->user_id;
- Data::save('OrderBill',$bill,'order_id',['order_id'=>$order_id]);
- $this->success('申请成功');
- }
- /**
- * @title 获取订单发票详情
- * @desc 获取订单发票详情
- * @author qc
- * @url /api/Order/getBillInfo
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @return name:header type:string require:1 default:0 desc:抬头
- * @return name:type type:string require:1 default:0 desc:类型(string)
- * @return name:email type:string require:1 default:0 desc:邮箱(string)
- * @return name:bank type:string require:1 default:0 desc:开户行(string)
- * @return name:card_no type:string require:1 default:0 desc:账号(string)
- * @return name:phone type:string require:1 default:0 desc:电话(string)
- * @return name:remark type:string require:1 default:0 desc:审核备注
- * @return name:bill_img type:string require:1 default:0 desc:电子发票图片
- */
- public function getBillInfo()
- {
- $detail = OrderBill::where('order_id',input('order_id'))->where('source',1)->find();
- $this->success('ok',['detail'=>$detail ?$detail->toArray() :'']);
- }
- }
|