123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use EasyWeChat\Factory;
- /**
- * @title 会员订单管理
- * @controller Order
- * @group base
- */
- class Order extends Base
- {
- public function initialize()
- {
- parent::check_login();
- }
- /**
- * @title 订单确认(直接购买&&直接兑换)
- * @desc 生成订单
- * @author qc
- * @url /api/Order/orderConfirm
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:-- desc:商品id
- * @param name:goods_no type:string require:1 default:-- desc:商品货号
- * @param name:num type:int require:0 default:1 desc:商品数量
- * @return name:add_info type:array require:0 default:-- desc:默认地址
- * @return name:add_info.id type:int default:1 desc:收货地址记录id
- * @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.detail type:string default:-- desc:详细地址
- * @return name:add_info.phone type:string default:-- desc:联系电话
- * @return name:add_info.name type:string default:-- desc:联系人
- * @return name:goods_data type:array require:0 default:-- desc:商品详情
- * @return name:goods_data.goods_id type:array require:0 default:-- desc:商品id
- * @return name:goods_data.name type:string require:0 default:-- desc:商品名称
- * @return name:goods_data.cover type:string require:0 default:-- desc:商品图片
- * @return name:goods_data.freight type:float require:0 default:-- desc:商品运费
- * @return name:goods_data.integral type:int require:0 default:-- desc:商品积分数(单个商品积分乘以个数)
- */
- public function orderConfirm()
- {
- $goods_id = input('get.goods_id');
- $goods_no = input('get.goods_no');
- $num = input('get.num',1);
- $add_info = Db::table('delivery_address')
- ->where(['user_id'=>$this->uid,'is_mr'=>1])->find();
- $goods_info = Db::table('store_goods')->find($goods_id);
- $spec = json_decode($goods_info['spec'],true);
- $cover = $goods_info['cover'];
- foreach ($spec as $sk=>$sv)
- {
- if($goods_no == $sv['goods_no']) $cover = $sv['spec_img'];
- }
- $freight = $goods_info['freight_type'] == 0 ? $goods_info['postage'] :0 ;
- // 运费模板计算运费
- if(!empty($add_info) && $goods_info['freight_type']){
- $cal_freight = $this->claFreight($add_info['id'],$goods_info['freight_id'],$num);
- if($cal_freight['code'] != 200){
- $add_info = null ;
- }else{
- $freight = $cal_freight['freight'];
- }
- }
- $goods_data = [
- 'goods_id' => $goods_info['id'],
- 'goods_no' => $goods_no,
- 'name' => $goods_info['name'],
- 'cover' => $cover,
- 'integral'=>$goods_info['integral'] * $num,
- 'freight' => $freight
- ];
- $this->success('ok',['add_info'=> $add_info ,'goods_data'=>$goods_data]);
- }
- /**
- * 计算运费
- * @param $add_id 地址id
- * @param $freight_id 模板id
- * @param $num 商品数量
- * @return array
- */
- public function claFreight($add_id,$freight_id,$num)
- {
- $data = ['code'=>200,'freight'=>0 ,'msg'=>''];
- $address_info = Db::table('delivery_address')->find($add_id);
- $template_info = Db::table('freight_template')->find($freight_id);
- if(empty($address_info)){
- $data['code'] = 201;
- $data['msg'] = '运费模板不存在';
- return $data;
- }
- $city_id = $address_info['city_id'];
- $no_express = explode(',',$template_info['no_express']);
- if(in_array($city_id,$no_express)) return ['code'=>202,'msg'=>'该收货地址不配送物流'];
- // 模板自定义分组
- $custom = json_decode($template_info['custom'],true);
- $base_num = $template_info['base_num'];// 基件数量
- $base_price = $template_info['base_price']; // 基件费用
- $base_keep_price = $template_info['base_keep_price']; // 续件费用
- foreach($custom as $cus_info) {
- $express =explode(',',$cus_info['express']);
- if(in_array($city_id,$express)) {
- $base_num = $cus_info['first_num'];
- $base_price = $cus_info['first_price'];
- $base_keep_price = $cus_info['keep_price'];
- break;
- }
- }
- if($base_num >= $num ) return $data = ['code'=>200,'freight'=>$base_price ,'msg'=>'ok'];
- $sub_keep = bcmul(bcsub($num,$base_num),$base_keep_price,2);
- return $data = ['code'=>200,'freight'=>bcadd($base_price,$sub_keep) ,'msg'=>'ok'];
- }
- /**
- * @title 获取运费
- * @desc 获取运费
- * @author qc
- * @url /api/Order/getFreight
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:-- desc:商品id
- * @param name:add_id type:int require:1 default:-- desc:地址id
- * @param name:num type:int require:0 default:1 desc:商品数量
- * @return name:freight type:float require:0 default:-- desc:运费金额
- */
- public function getFreight()
- {
- $goods_id = input('goods_id',0);
- $add_id = input('add_id',0);
- $num = input('num',0);
- $goods_info = Db::table('store_goods')->find($goods_id);
- $freight = $goods_info['freight_type'] == 0 ? $goods_info['postage'] :0 ;
- if($goods_info['freight_type']){
- $cal_freight = $this->claFreight($add_id,$goods_info['freight_id'],$num);
- if($cal_freight['code'] != 200){
- $this->error($cal_freight['msg']);
- }else{
- $freight = $cal_freight['freight'];
- }
- }
- $this->success('ok',['freight'=>$freight]);
- }
- /**
- * @title 生成订单(立即购买 [商城 && 积分商城])
- * @desc 生成订单(立即购买 [商城 && 积分商城])
- * @author qc
- * @url /api/Order/createOrderByImmediately
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:-- desc:商品id
- * @param name:goods_no type:int require:1 default:-- desc:商品货号
- * @param name:price_total type:float require:1 default:-- desc:支付金额(优惠后的商品金额+运费)
- * @param name:num type:int require:1 default:1 desc:商品数量
- * @param name:price_goods type:float require:0 default:0 desc:商品总金额(没优惠的)
- * @param name:price_express type:float require:0 default:0 desc:运费
- * @param name:add_id type:int require:1 default:0 desc:收货地址id
- * @param name:remark type:string require:0 default:0 desc:订单备注
- * @param name:source type:int require:0 default:0 desc:商城的为0(默认),积分商城的值为1
- * @param name:integral type:int require:0 default:0 desc:使用多少积分,source为1时必传
- * @param name:int_cash type:float require:0 default:0 desc:积分抵扣多少钱,source为1时必传
- * @param name:cl_id type:int require:0 default:0 desc:会员优惠券id,使用优惠券必传
- * @param name:coupon_cash type:float require:0 default:0 desc:会员优惠券抵扣金额
- * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
- */
- public function createOrderByImmediately()
- {
- $goods_id = input("post.goods_id");
- $goods_no = input("post.goods_no");
- $price_total = input("post.price_total",0);
- $price_goods = input("post.price_goods",0);
- $price_express = input("post.price_express",0);
- $num = input("post.num",1);
- $add_id = input("post.add_id",0);
- $source = input("post.source",0);
- $integral = input("post.integral",0);
- $int_cash = input("post.int_cash",0);
- $cl_id = input("post.cl_id",0);
- $coupon_cash = input("post.coupon_cash",0);
- $remark = input("post.remark",'');
- //商品验证start
- $goods_info = Db::table('store_goods')->field('id,name,spec,stock,is_integral')
- ->where(['id'=>$goods_id,'status'=>1,'is_deleted'=>0])
- ->find();
- if(empty($goods_info)) $this->error('商品已下架');
- $spec_info = [];
- $goods_spec = json_decode($goods_info['spec'],true);
- foreach ($goods_spec as &$c){
- if($c['goods_no'] == $goods_no) {
- $spec_info =$c;
- $c['store_num'] -=$num;
- break;
- }
- }
- if(!isset($spec_info['integral'])) $spec_info['integral'] = 0;
- if($spec_info['store_num'] < $num || $goods_info['stock'] < $num) $this->error('商品库存不足');
- // 券验证
- if($cl_id) {
- $cl_info = Db::table('user_coupon_list')
- ->where(['user_id'=>$this->uid,'id'=>$cl_id,'status'=>1])
- ->find();
- if(empty($cl_info)) $this->error('优惠券不存在');
- if($cl_info['past_at'] < date('Y-m-d H:i:s')) $this->error('优惠券已过期');
- if($cl_info['low_amount'] > bcsub($price_total,$price_express,2) ) $this->error('订单金额不够,再看看吧');
- if($cl_info['amount'] != $coupon_cash ) $this->error('优惠金额错误');
- }
- // 积分验证
- $user = Db::table('store_member')->find($this->uid);
- if($integral && $user['integral'] < $integral) $this->error('积分余额不足');
- $pro_info[]=[
- 'goods_id' =>$goods_id,
- 'goods_no' =>$goods_no,
- 'num' => $num,
- 'integral' =>$spec_info['integral'] * $num,
- 'int_cash' =>$int_cash,
- 'goods_name' =>$goods_info['name'],
- 'spec_img' =>$spec_info['spec_img'],
- 'sell_money'=>$spec_info['sell_money'],
- 'spec_exp' =>$spec_info['spec_exp'],
- 'spec_key' =>$spec_info['spec_key'],
- 'is_evaluate' => 0,
- ];
- $order_int =[
- 'uid'=>$this->uid,
- 'order_no' => get_order_sn(),
- 'price_total' => $price_total,
- 'price_goods' => $price_goods,
- 'price_express' => $price_express,
- 'create_at' => date("Y-m-d H:i:s"),
- 'address_id' =>$add_id,
- 'source' =>$source,
- 'integral' =>$integral,
- 'int_cash' =>$int_cash,
- 'cl_id' =>$cl_id,
- 'coupon_cash' =>$coupon_cash,
- 'remark' =>$remark,
- 'goods_num' =>$num,
- 'pro_info' =>json_encode($pro_info)
- ];
- Db::startTrans();
- $msg= '';$com=true;
- try{
- // 生成订单
- Db::table('store_order')->insert($order_int);
- $order_id = Db::getLastInsID();
- //扣减库存
- update_goods_stock($goods_id,$goods_no,$num);
- //扣减积分
- if($integral){
- update_user_integral($this->uid,$integral * -1,5,'积分抵扣',$order_id);
- }
- // 扣除券
- if($cl_id) {
- Db::table('user_coupon_list')->where(['id'=>$cl_id])->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $com=false;
- $msg = $e->getMessage();
- }
- if($com){
- $this->success('ok',['order_id'=>$order_id]);
- }else{
- $this->error($msg);
- }
- }
- /**
- * @title 取消订单
- * @desc 取消订单
- * @author qc
- * @url /api/Order/cancelOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- * @param name:cancel_desc type:string require:1 default:-- desc:取消描述
- */
- public function cancelOrder()
- {
- $order_id = input('post.id');
- $desc = input('post.desc');
- $order_detail = Db::table('store_order')->where(['id'=>$order_id])->find();
- if(empty($order_detail)) $this->error('订单不存在');
- if($order_detail['pay_state'] != 0) $this->error('订单已支付');
- if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
- Db::startTrans();
- try {
- // 积分退回
- if($order_detail['integral'] > 0) {
- update_user_integral($order_detail['uid'],$order_detail['integral'],8,'订单取消退回',$order_id);
- }
- // 优惠券回档
- if($order_detail['cl_id'] > 0) {
- $user_coupon = Db::table('user_coupon_list')->find($order_detail['cl_id']);// 会员优惠券记录
- $coupon_set = Db::table('store_coupon_config')->find($user_coupon['coupon_id']);//优惠券设置
- if($coupon_set['status'] != 1 || $coupon_set['is_deleted'] == 1){
- $coupon_status = 4;
- }else{
- $coupon_status = $user_coupon['past_at'] > date("Y-m-d H:i:s") ? 1:3;
- }
- Db::table('user_coupon_list')->where(['id'=>$order_detail['cl_id']])->update(['status'=>$coupon_status]);
- }
- // 商品库存恢复
- $pro_info = json_decode($order_detail['pro_info'],true);
- foreach ($pro_info as $pv) {
- update_goods_stock($pv['goods_id'],$pv['goods_no'],$pv['num']*-1);
- }
- // 取消订单
- Db::table('store_order')
- ->where(['id'=>$order_id])
- ->update(['cancel_state'=>1,'cancel_at'=>date('Y-m-d H:i:s'),'cancel_desc'=>$desc,'status'=>9]);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- }
- $this->success('取消成功');
- }
- /**
- * @title 生成订单(购物车)
- * @desc 生成订单(购物车)
- * @author qc
- * @url /api/Order/createOrderByTrolley
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:ids type:string : default:1 desc:购物车记录id串(逗号分开)
- * @param name:price_total type:float require:1 default:-- desc:支付金额(优惠后的商品金额+运费)
- * @param name:price_goods type:float require:0 default:0 desc:商品总金额(没优惠的)
- * @param name:price_express type:float require:0 default:0 desc:运费
- * @param name:add_id type:int require:1 default:0 desc:收货地址id
- * @param name:remark type:string require:0 default:0 desc:订单备注
- * @param name:source type:int require:0 default:0 desc:商城的为0(默认),积分商城的值为1
- * @param name:integral type:int require:0 default:0 desc:使用多少积分,source为1时必传
- * @param name:int_cash type:float require:0 default:0 desc:积分抵扣多少钱,source为1时必传
- * @param name:cl_id type:int require:0 default:0 desc:会员优惠券id,使用优惠券必传
- * @param name:coupon_cash type:float require:0 default:0 desc:会员优惠券抵扣金额
- * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
- */
- public function createOrderByTrolley()
- {
- $ids = input("post.ids");
- $price_total = input("post.price_total",0);
- $price_goods = input("post.price_goods",0);
- $price_express = input("post.price_express",0);
- $add_id = input("post.add_id",0);
- $source = input("post.source",0);
- $integral = input("post.integral",0);
- $int_cash = input("post.int_cash",0);
- $cl_id = input("post.cl_id",0);
- $coupon_cash = input("post.coupon_cash",0);
- $remark = input("post.remark",'');
- if(!$ids || !$price_total) $this->error('请选择要结算的商品');
- $trolley_arr = Db::table('shopping_trolley')
- ->where(['user_id'=>$this->uid,'id'=>explode(',',$ids)])
- ->select();
- $pro_info = [];$total_num = 0;$check_goods = true;
- // 验证商品是否下架 && 获取订单商品详情
- foreach ($trolley_arr as $tv) {
- $goods_info = Db::table('store_goods')->field('id,spec,name')
- ->where(['id'=>$tv['goods_id'],'is_deleted'=>0,'status'=>1])
- ->find();
- if(empty($goods_info)){
- $check_goods =false;
- break;
- }
- foreach (json_decode($goods_info['spec'],true) as $sv)
- {
- if($sv['goods_no'] == $tv['goods_no']) {
- $pro_info[]=[
- 'goods_id' =>$tv['goods_id'],
- 'goods_no' =>$tv['goods_no'],
- 'num' =>$tv['num'],
- 'integral' => isset($sv['integral']) ? $sv['integral'] * $tv['num'] : 0,
- 'int_cash' =>'--',
- 'goods_name' =>$goods_info['name'],
- 'spec_img' =>$sv['spec_img'],
- 'sell_money'=>$sv['sell_money'],
- 'spec_exp' =>$sv['spec_exp'],
- 'spec_key' =>$sv['spec_key'],
- 'is_evaluate' => 0,
- ];
- $total_num +=$tv['num'];
- }
- }
- }
- if(!$check_goods) $this->error('所选商品已下架');
- // 券验证
- if($cl_id) {
- $cl_info = Db::table('user_coupon_list')
- ->where(['user_id'=>$this->uid,'id'=>$cl_id,'status'=>1])
- ->find();
- if(empty($cl_info)) $this->error('优惠券不存在');
- if($cl_info['past_at'] < date('Y-m-d H:i:s')) $this->error('优惠券已过期');
- if($cl_info['low_amount'] > bcsub($price_total,$price_express,2) ) $this->error('订单金额不够,再看看吧');
- if($cl_info['amount'] != $coupon_cash ) $this->error('优惠金额错误');
- }
- // 积分验证
- $user = Db::table('store_member')->find($this->uid);
- if($integral && $user['integral'] < $integral) $this->error('积分余额不足');
- $order_int =[
- 'uid'=>$this->uid,
- 'order_no' => get_order_sn(),
- 'price_total' => $price_total,
- 'price_goods' => $price_goods,
- 'price_express' => $price_express,
- 'create_at' => date("Y-m-d H:i:s"),
- 'address_id' =>$add_id,
- 'source' =>$source,
- 'integral' =>$integral,
- 'int_cash' =>$int_cash,
- 'cl_id' =>$cl_id,
- 'coupon_cash' =>$coupon_cash,
- 'remark' =>$remark,
- 'goods_num' =>$total_num,
- 'pro_info' =>json_encode($pro_info)
- ];
- Db::startTrans();
- $msg= '';$com=true;
- try{
- // 生成订单
- Db::table('store_order')->insert($order_int);
- $order_id = Db::getLastInsID();
- // 删除购物车记录
- Db::table('shopping_trolley')->where(['id'=>explode(',',$ids)])->delete();
- //扣减积分
- if($integral){
- Db::table('store_member')->where(['id'=>$this->uid])->setDec('integral',$integral);
- //积分扣减记录
- $integral_info=[
- 'user_id'=> $this->uid,
- 'create_at'=> date("Y-m-d H:i:s"),
- 'integral'=> $integral * -1,
- 'before'=> $user['integral'],
- 'after'=> $user['integral'] - $integral,
- 'desc'=> '积分抵现',
- 'rel_id'=> $order_id,
- ];
- Db::table('integral_info')->insert($integral_info);
- }
- // 扣除券
- if($cl_id) {
- Db::table('user_coupon_list')->where(['id'=>$cl_id])->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $com=false;
- $msg = $e->getMessage();
- }
- if($com){
- $this->success('ok',['order_id'=>$order_id]);
- }else{
- $this->error($msg);
- }
- }
- /**
- * @title 获取订单详情
- * @desc 获取订单详情
- * @author qc
- * @url /api/Order/getOrderDetail
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
- * @return name:price_total type:float default:-- desc:订单金额(需要支付金额【包含运费,扣除所有优惠】)
- * @return name:pro_info type:string default:-- desc:订单商品
- * @return name:pro_info.goods_id type:int default:-- desc:商品id
- * @return name:pro_info.goods_no type:string default:-- desc:商品货号
- * @return name:pro_info.sell_money type:string default:-- desc:售价
- * @return name:pro_info.spec_img type:string default:-- desc:封面
- * @return name:pro_info.is_evaluate type:string default:-- desc:商品是否评价
- * @return name:price_goods type:float default:-- desc:商品金额
- * @return name:price_express type:float default:-- desc:运费
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:pay_no type:string default:-- desc:支付单号
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:pay_at type:string default:-- desc:支付时间
- * @return name:cancel_at type:string default:-- desc:取消时间(status=9)
- * @return name:cancel_desc type:string default:-- desc:取消描述(status=9)
- * @return name:refund_type type:int default:-- desc:订单申请退款、退换货状态0未申请,1部分申请,2全部申请
- * @return name:source type:int default:0 desc:订单来源:0商城,1积分商城
- * @return name:integral type:int default:0 desc:使用多少积分
- * @return name:int_cash type:int default:0 desc:积分抵扣多少金额
- * @return name:cl_id type:int default:0 desc:使用优惠券的id
- * @return name:coupon_cash type:float default:0 desc:券抵扣金额
- */
- public function getOrderDetail()
- {
- $order_id = input('id',0);
- $detail = Db::table('store_order')->find($order_id);
- $this->success('获取成功',$detail);
- }
- /**
- * @title 去支付
- * @desc 去支付
- * @author qc
- * @url /api/Order/payOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:o_id type:int require:1 default:-- desc:订单id
- * @return name:type type:int default:0 desc:类型(1.type=1,调支付,2纯积分兑换成功)
- * @return name:config type:array default:0 desc:支付配置(type=2时忽略)
- * @return name:config.appId type:string default:-- desc:公众账号ID
- * @return name:config.nonceStr type:string default:-- desc:随机字符串
- * @return name:config.signType type:string default:-- desc:签名类型
- * @return name:config.paySign type:string default:-- desc:签名
- * @return name:config.timestamp type:string default:-- desc:时间戳
- */
- public function payOrder()
- {
- $o_id = input('post.o_id');
- $user_info = Db::table('store_member')->where('id',$this->uid)->find();
- if(!$user_info['phone'] || !$user_info['openid']) $this->error('请先完善用户信息');
- $order_info = Db::table('store_order')->where(['uid'=>$this->uid,'id'=>$o_id])->find();
- if(empty($order_info)) $this->error('订单不存在');
- if($order_info['status'] != 0) $this->error('订单已支付');
- if($order_info['cancel_state'] != 0) $this->error('订单已取消');
- if($order_info['price_total'] <= 0 && $order_info['source'] == 0) $this->error('订单金额错误');
- // 纯积分兑换
- if($order_info['price_total'] <= 0 && $order_info['source'] == 1 )
- {
- // 销量
- $sell_data = [];
- foreach (json_decode($order_info['pro_info'],true) as $pv){
- $sell_data[] =[
- 'goods_id' => $pv['goods_id'],
- 'goods_no' => $pv['goods_no'],
- 'num' => $pv['num'],
- 'user_id' => $order_info['uid'],
- 'order_id' => $order_info['id'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'year' => date('Y'),
- 'month' => date('m'),
- 'day' => date('d'),
- ];
- }
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- Db::table('goods_sell_info')->insertAll($sell_data);
- // 更新订单状态
- Db::table('store_order')->where(['uid'=>$this->uid,'id'=>$o_id])
- ->update(['pay_at'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1,'pay_no'=>$pay_no]);
- $this->success('兑换成功',['type'=>2]);
- }else{
- Db::startTrans();
- $config = false;$msg= '';
- try{
- $notify_url = $this->request->root(true) . '/api/Pay/payNotify';
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- Db::table('store_order')->where(['uid'=>$this->uid,'id'=>$o_id])->update(['pay_no'=>$pay_no]);
- $config = Pay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if($config){
- Db::commit();
- }else{
- Db::rollback();
- }
- }catch (\Exception $e){
- Db::rollback();
- $msg = $e->getMessage();
- }
- if($config){
- $this->success('ok',['type'=>1,'jump'=>1,'config'=>$config]);
- }else{
- $this->error($msg);
- }
- }
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @url /api/Order/getMyOrderList
- * @method GET
- * @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:订单状态(-1全部,0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消)
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:price_total type:float default:-- desc:订单金额
- * @return name:pro_info type:float default:-- desc:订单详情
- * @return name:pro_info.goods_id type:int default:-- desc:商品id
- * @return name:pro_info.goods_name type:int default:-- desc:商品名
- * @return name:pro_info.goods_no type:string default:-- desc:商品货号
- * @return name:pro_info.sell_money type:string default:-- desc:售价
- * @return name:pro_info.spec_img type:string default:-- desc:封面
- * @return name:pro_info.is_evaluate type:string default:-- desc:商品是否评价
- * @return name:pay_at type:string default:-- desc:支付时间
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:remark type:string default:-- desc:备注
- * @return name:cancel_at type:string default:-- desc:取消时间(status=9)
- * @return name:cancel_desc type:string default:-- desc:取消描述(status=9)
- * @return name:refund_type type:int default:-- desc:订单申请退款、退换货状态0未申请,1申请
- * @return name:source type:int default:0 desc:订单来源:0商城,1积分商城
- * @return name:integral type:int default:0 desc:使用多少积分
- * @return name:int_cash type:int default:0 desc:积分抵扣多少金额
- * @return name:cl_id type:int default:0 desc:使用优惠券的id
- * @return name:coupon_cash type:float default:0 desc:券抵扣金额
- */
- public function getMyOrderList()
- {
- $status = input('status',-1);
- $where=[];
- if($status > -1) $where['o.status'] = $status;
- $where['o.uid'] = $this->uid;
- $where['o.is_deleted'] = 0;
- $list = Db::table('store_order o')
- ->field('o.id,o.order_no,o.pay_state,o.create_at,pay_at,pro_info,o.price_total,o.remark,source,integral')
- ->where($where)
- ->order('o.id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- /* foreach ($list as &$v)
- {
- $v['pro_info'] = json_decode($v['pro_info'],true);
- }*/
- $this->success('获取成功',$list);
- }
- /**
- * @title 删除订单
- * @desc 删除订单
- * @author qc
- * @url /api/Order/delOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:1 desc:订单id
- */
- public function delOrder()
- {
- $order_id = input('post.id');
- $order_info = Db::table('store_order')
- ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
- ->find();
- if(empty($order_info)) $this->error('订单不存在');
- Db::table('store_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
- $this->success('删除成功');
- }
- /**
- * @title 确认收货
- * @desc 确认收货
- * @author qc
- * @url /api/Order/confirmReceipt
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:1 desc:订单id
- */
- public function confirmReceipt(){
- $order_info = Db::table('store_order')
- ->where(['uid'=>$this->uid,'id'=>input('post.id')])
- ->find();
- if(empty($order_info) || !$order_info['status'] != 2) $this->error('商品未发货');
- Db::table('store_order')->where(['id'=>$order_info['id']])->update(['status'=>3]);
- $this->success('收货成功');
- }
- /**
- * @title 订单商品评论
- * @desc 订单商品评论
- * @author qc
- * @url /api/Order/orderEvaluate
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:-- desc:订单order_id
- * @param name:goods_id type:int require:1 default:-- desc:商品id
- * @param name:goods_no type:int require:1 default:-- desc:商品货号
- * @param name:goods_score type:int require:1 default:-- desc:商品评分
- * @param name:content type:string require:1 default:-- desc:评论内容
- * @param name:images type:int require:1 default:-- desc:图片(多张逗号隔开)
- */
- public function orderEvaluate()
- {
- $order_id = input('post.order_id');
- $goods_id = input('post.goods_id');
- $goods_no = input('post.goods_no');
- $goods_score = input('post.goods_score');
- $images = input('post.images');
- $content = input('post.content');
- $eva_data =[
- 'order_id' => $order_id,
- 'goods_id' => $goods_id,
- 'goods_no' => $goods_no,
- 'goods_score' => $goods_score,
- 'images' => $images,
- 'content' => $content,
- ];
- Db::table('goods_evaluate')->insert($eva_data);
- $order_info = Db::table('order_goods')->where(['id'=>$order_id])->find();
- $pro_info = json_decode($order_info['pro_info'],true);
- foreach ($pro_info as $pk=>$pv) {
- if($pv['goods_id'] == $goods_id && $pv['goods_no'] == $goods_no) {
- $pro_info[$pk]['is_evaluate'] = 1;
- }
- }
- Db::table('order_goods')->where(['id'=>$order_id])->update(['status'=>4,'pro_info'=>json_encode($pro_info)]);
- }
- /**
- * @title 退款部分
- * @desc 退款部分
- */
- public function refundPart(){}
- /**
- * @title 获取平台退款原因
- * @desc 获取平台退款原因
- * @author qc
- * @url /api/Order/getRefundCause
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:id type:int default:-- desc:记录id
- * @return name:title type:string default:-- desc:退款原因描述
- */
- public function getRefundCause(){
- $list = get_refund_cause();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 提交订单退款
- * @desc 提交订单退款
- * @author qc
- * @url /api/Order/refundOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:1 desc:订单id
- * @param name:cause_id type:int require:1 default:1 desc:退款原因id
- * @param name:apply_type type:int require:1 default:1 desc:申请类型 1退款,2退货退款
- * @param name:apply_money type:float require:1 default:1 desc:申请退回金额
- * @param name:re_desc type:string require:0 default:-- desc:描述
- * @param name:re_images type:string require:0 default:-- desc:退款图片最多三张(多张用逗号隔开)
- * @return name:re_id type:int require:0 default:0 desc:成功时返回退款记录id
- */
- public function refundOrder()
- {
- $order_id = input('post.order_id');
- $cause_id = input('post.cause_id');
- $apply_type = input('post.apply_type',1);
- $apply_money = input('post.apply_money');
- $re_desc = input('post.re_desc');
- $re_images = input('post.re_images');
- $ck_refund = Db::table('order_refund')
- ->where(['order_id'=>$order_id,'ck_status'=>0])
- ->value('id');
- if($ck_refund) $this->error('审核中,请勿重新申请');
- $ref_data = [
- 'uid' => $this->uid,
- 'order_id' => $order_id,
- 'apply_type' => $apply_type,
- 'apply_money' => $apply_money,
- 'create_at' => date("Y-m-d H:i:s"),
- 'cause_id' => $cause_id,
- 're_desc' => $re_desc,
- 're_images' => $re_images,
- ];
- Db::table('order_refund')->insert($ref_data);
- $re_id = Db::getLastInsID();
- // 更新订单表退款申请状态
- Db::table('store_order')->where('id',$order_id)->update(['refund_state'=>1]);
- $this->success('ok',['re_id'=>$re_id]);
- }
- /**
- * @title 获取提交订单退款详情
- * @desc 获取提交订单退款详情
- * @author qc
- * @url /api/Order/getRefundInfo
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:1 desc:退款记录id
- * @return name:cause_id type:int default:1 desc:退款原因id
- * @return name:apply_type type:int default:1 desc:申请类型 1退款,2退货退款
- * @return name:apply_money type:float default:1 desc:申请退回金额
- * @return name:create_at type:string default:1 desc:申请时间
- * @return name:re_desc type:string default:-- desc:描述
- * @return name:re_images type:string default:-- desc:退款图片
- * @return name:pro_info type:string default:-- desc:申请订单的商品详情
- * @return name:pro_info.goods_id type:int default:-- desc:商品id
- * @return name:pro_info.goods_no type:string default:-- desc:商品货号
- * @return name:pro_info.sell_money type:string default:-- desc:售价
- * @return name:pro_info.spec_img type:string default:-- desc:封面
- * @return name:pro_info.is_evaluate type:string default:-- desc:商品是否评价
- * @return name:goods_num type:string default:-- desc:购买的商品数量
- * @return name:ck_status type:int default:0 desc:审核状态(0待审核 ,1 审核通过 , 2 审核不通过),
- * @return name:refund_money type:float default:0 desc:审核退款金额,
- * @return name:refund_state type:int default:0 desc:同意退款后的退款状态(0未退款,1待退款,2已退款,3退款失败)
- * @return name:refund_desc type:int default:0 desc:退款描述
- * @return name:wl_no type:string default:0 desc:退货物流单号 ( apply_type = 2)
- * @return name:wl_company type:string default:0 desc:退货物流公司( apply_type = 2)
- */
- public function getRefundInfo()
- {
- $id = input('post.id');
- $detail = Db::table('order_refund')
- ->field('r.*,o.pro_info,o.goods_num')
- ->alias('r')
- ->join('store_order o','r.order_id = o.id','LEFT')
- ->where(['r.order_id'=>$id])
- ->find();
- $re_cause = get_refund_cause();
- $detail['refund_desc'] = array_column($re_cause,null,'id')[$detail['cause_id']]['title'];
- $this->success('ok',['refund_detail'=>$detail]);
- }
- /**
- * @title 完善退货物流信息
- * @desc 完善退货物流信息
- * @author qc
- * @url /api/Order/completeLogistics
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:退款记录id
- * @param name:wl_no type:string require:1 default:-- desc:物流单号
- * @param name:wl_company type:string require:1 default:1 desc:物流公司
- */
- public function completeLogistics()
- {
- $id = input('post.id');
- $wl_no = input('post.wl_no');
- $wl_company = input('post.wl_company');
- $info = $detail = Db::table('order_refund')->find($id);
- if($info['apply_type'] == 1) $this->error('申请类型不符合,无需物流信息');
- // 更新物流信息
- Db::table('order_refund')->where(['id'=>$id])->update(['wl_no'=>$wl_no,'wl_company'=>$wl_company]);
- $this->success('ok');
- }
- /**
- * @title 修改订单退款(退货)申请信息
- * @desc 修改订单退款(退货)申请信息
- * @author qc
- * @url /api/Order/modifyRefundInfo
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:1 desc:需要修改的退款申请id
- * @param name:cause_id type:int require:1 default:1 desc:退款原因id
- * @param name:apply_type type:int require:1 default:1 desc:申请类型 1退款,2退货退款
- * @param name:apply_money type:float require:1 default:1 desc:申请退回金额
- * @param name:re_desc type:string default:-- desc:描述
- * @param name:re_images type:string default:-- desc:退款图片最多三张(多张用逗号隔开)
- * @param name:wl_no type:string default:-- desc:物流单号
- * @param name:wl_company type:string default:1 desc:物流公司
- */
- public function modifyRefundInfo()
- {
- $id = input('post.id');
- $detail = Db::table('order_refund')->find($id);
- if($detail['ck_status'] != 0) $this->error('已审核无法修改,请重新提交');
- $up_data = [];
- if(input('post.cause_id')) $up_data['cause_id'] = input('post.cause_id');
- if(input('post.apply_type')) $up_data['apply_type'] = input('post.apply_type');
- if(input('post.apply_money')) $up_data['apply_money'] = input('post.apply_money');
- if(input('post.re_desc')) $up_data['re_desc'] = input('post.re_desc');
- if(input('post.re_images')) $up_data['re_images'] = input('post.re_images');
- if(input('post.wl_no')) $up_data['wl_no'] = input('post.wl_no');
- if(input('post.wl_company')) $up_data['wl_company'] = input('post.wl_company');
- if(!empty($up_data)) {
- Db::table('order_refund')->where(['id'=>$id])->update($up_data);
- }
- $this->success('修改成功');
- }
- /**
- * @title 取消申请退款(退货)
- * @desc 取消申请退款(退货)
- * @author qc
- * @url /api/Order/cancelRefund
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:1 desc:订单id
- */
- public function cancelRefund(){
- $order_id = input('post.order_id');
- $re_info = Db::table('order_refund')->where(['uid'=>$this->uid,'order_id'=>$order_id,'ck_status'=>0])->count();
- if(!$re_info) $this->error('退款记录不存在');
- // 删除记录
- Db::table('order_refund')->where(['order_id'=>$order_id,'ck_status'=>0])->delete();
- $ck = Db::table('order_refund')->where(['order_id'=>$order_id])->find();
- $stat = empty($ck) ? 0 : 2 ;
- Db::table('store_order')->where(['id'=>$order_id])->update(['refund_state'=>$stat]);
- }
- /**
- * @title 获取提交订单退款列表 ???
- * @desc 获取提交订单退款列表 ???
- */
- public function getRefundList()
- {
- }
- }
|