123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864 |
- <?php
- namespace app\api\controller;
- use app\common\model\BillApply;
- use app\common\model\OrderComment;
- use app\common\model\OrderLogisticsUrge;
- 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\User;
- use app\common\model\UserMessage;
- use app\common\service\ExpressService;
- use app\common\service\UserSynth;
- use think\Db;
- use think\Exception;
- /**
- * @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:total_price type:float default:-- desc:商品金额[不含运费]
- * @return name:freight type:float default:-- desc:运费
- * @return name:freight_code type:int default:1 desc:运费提示编号【1所选地址支持发货,0所选地址不支持发货】
- * @return name:freight_msg type:string default:-- desc:运费提示信息【freight_code=1忽略】
- * @return name:goods_no type:string default:-- desc:规格货号
- * @return name:goods_spec type:string default:-- desc:规格
- * @return name:sell_price 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.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:freight type:float default:-- desc:运费
- * @return name:freight_code type:int default:1 desc:运费提示编号【1所选地址支持发货,0所选地址不支持发货】
- * @return name:freight_msg type:string default:-- desc:运费提示信息【freight_code=1忽略】
- */
- public function confirmOrderImmediately(){
- $goods_id = input('get.goods_id');
- $spec_id = input('get.spec_id');
- $num = input('get.num',1);
- $field = 'id,name,cover,desc,low_price,status,is_deleted,freight_type,postage,freight_id';
- $goods_info = StoreGoods::field($field)->where('id',$goods_id)->find()->toArray();
- $spec_info = StoreGoodsItem::where('id',$spec_id)->find()->toArray();
- $goods_info['total_price']= bcmul($spec_info['sell_price'],$num,2);
- $goods_info['goods_spec']= $spec_info['goods_spec'];
- $goods_info['goods_no']= $spec_info['goods_no'];
- $goods_info['sell_price']= $spec_info['sell_price'];
- $add_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
- $freight = 0;
- if($add_info){
- $goods_freight = ExpressService::getGoodsExpressPrice($goods_info,$add_info->id,$num);
- if($goods_freight['code'] !=200){
- $this->is_commit = false;
- $this->ret_msg = $goods_freight['msg'];
- }
- if($goods_freight['code'] == 200) $freight = $goods_freight['freight'];
- }
- $this->success('ok',['goods_info'=>$goods_info,
- 'add_info' =>$add_info ? $add_info->toArray():null,
- 'freight' =>$freight,
- 'freight_msg' => $this->ret_msg,
- 'freight_code' => $this->is_commit ? 1 :0
- ]);
- }
- /**
- * @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:add_id type:int default:1 desc:收货地址id
- * @param name:remark type:string default:1 desc:订单备注
- *
- * @param name:bill_info type:string default:-- desc:发票信息--json【不开票可以不传】
- * @param name:bill_info.type type:int default:-- desc:类型id
- * @param name:bill_info.header type:string default:-- desc:抬头名称[公司名称||个人名称]
- * @param name:bill_info.identify_number type:string default:-- desc:纳税人识别号
- * @param name:bill_info.email type:string default:-- desc:邮箱
- * @param name:bill_info.remark type:string default:-- 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');
- $remark = input('post.remark');
- $post_bill = input('post.bill_info');
- $bill_info = json_decode(stripslashes($post_bill),true);
- 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'],
- 'user_name' => $add_info['name'],
- 'add_detail' => $add_info['detail'],
- 'phone' => $add_info['phone'],
- 'remark' => $remark,
- ];
- $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
- return $query->where('id',$spec_id)->where('is_deleted',0);
- }])->where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
- if(!$goods_info) throw new Exception('商品已下架');
- $goods_info = $goods_info->toArray();
- if(empty($goods_info['item_list'])) throw new Exception('该规格已下架');
- if($goods_info['item_list'][0]['stock'] < $num) throw new Exception('库存不足');
- $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];
- $price_goods = bcmul($num,$true_cost,2); // 需要支付商品价格
- // 获取运费信息
- $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']; // 商品运费
- //订单金额 = 商品金额 + 运费
- $order_money = bcadd($price_goods,$price_express ,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_info = GoodsOrder::create($order_insert);
- $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'],
- 'cover' => $goods_info['cover'],
- 'original_price' => $spec_info['original_price'],
- 'sell_price' => $true_cost,
- 'pay_price' => $order_money,
- 'num' => $num,
- ];
- GoodsOrderItem::create($order_item);
- StoreGoods::where('id',$goods_id)->setDec('stock',$num);
- StoreGoodsItem::where('id',$spec_id)->setDec('stock',$num);
- // 申请开票
- if($order_money > 0 && !empty($bill_info)){
- $ret_val = UserSynth::buildBillApply($this->user_id, $order_info->id,2,$bill_info,0,1,$bill_info['remark'],0);
- if($ret_val['code'] != 200) $this->exception($ret_val['msg']);
- }
- 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/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:remark type:string default:1 desc:订单备注
- *
- * @param name:bill_info type:string default:-- desc:发票信息--json【不开票可以不传】
- * @param name:bill_info.type type:int default:-- desc:类型id
- * @param name:bill_info.header type:string default:-- desc:抬头名称[公司名称||个人名称]
- * @param name:bill_info.identify_number type:string default:-- desc:纳税人识别号
- * @param name:bill_info.email type:string default:-- desc:邮箱
- * @param name:bill_info.remark type:string default:-- desc:备注
- *
- * @return name:order_id type:int default:1 desc:订单id
- */
- public function createOrderByTrolley()
- {
- $ids = input('post.ids');
- $add_id = input('post.add_id');
- $remark = input('post.remark');
- $post_bill = input('post.bill_info');
- $bill_info = json_decode(stripslashes($post_bill),true);
- 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)
- {
- //验证库存
- if( $trolley['goods_spec']['stock'] < $trolley['num']) $this->exception('商品库存不足');
- // 扣除库存
- StoreGoodsItem::stockChange($trolley['spec_id'],$trolley['goods_id'],$trolley['num'],-1);
- $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'],
- 'cover' => $trolley['goods_info']['cover'],
- 'original_price' => $trolley['goods_spec']['original_price'],
- 'sell_price' => $trolley['goods_spec']['sell_price'],
- 'num' => $trolley['num'],
- 'trolley_id' => $trolley['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'],
- 'user_name' => $add_info['name'],
- 'add_detail' => $add_info['detail'],
- 'phone' => $add_info['phone'],
- 'remark' => $remark,
- ];
- $price_total = bcadd($price_goods ,$price_express,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_info = GoodsOrder::create($order_insert);// 生成订单
- array_walk($order_item,function (&$v,$k)use ($order_info){
- $v['order_id'] = $order_info->id;
- });
- (new GoodsOrderItem())->insertAll($order_item);// 生成订单商品详情
- ShoppingTrolley::where('id','in',$ids)->delete();// 删除购物车
- $ret_data['order_id'] = $order_info->id;
- // 申请开票
- if($price_total > 0 && !empty($bill_info)){
- $ret_val = UserSynth::buildBillApply($this->user_id, $order_info->id,2,$bill_info,0,1,$bill_info['remark'],0);
- if($ret_val['code'] != 200) $this->exception($ret_val['msg']);
- }
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->transReturn($ret_data);
- }
- /**
- * @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.h5微信,2.h5支付宝,3移动支付(微信),4移动支付(支付宝),5.h5微信内支付)
- * @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();
- if(in_array($order_info['pay_type'],[1,3,5]) && in_array($pay_type,[1,3,5]) && $pay_type != $order_info['pay_type'] )$this->error('微信支付冲突,请选择支付宝支付');
- $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>''];
- Db::startTrans();
- GoodsOrder::where('id',$order_id)->update(['pay_type'=>$pay_type]);
- 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,'MWEB');
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $pay_config['config']['mch_id'] = config('app.wx_pay')['mch_id'];
- $pay_config['config']['pay_no'] = $pay_no;
- $pay_config['config']['notify_url'] = $notify_url;
- $pay_config['config']['mweb_url'] = $pay_config['mweb_url'];
- $pay_config['config']['total_fee'] = $order_info['price_total'] * 100;
- $ret_data['config'] = $pay_config['config'];
- break;
- case 2:
- $notify_url = $this->request->root(true) . '/api/Alipay/goodsOrderNotify';
- $return_url ='https://'.$_SERVER['HTTP_HOST']."/dist/#/my";
- $config = Alipay::ali_pay_h5('订单支付',$pay_no,$order_info['price_total'],$notify_url,$return_url);
- $ret_data['config']['ali_url'] = $config;
- $ret_data['ali_url'] = $config;
- break;
- case 3: //移动支付(微信)
- $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 4://移动支付(支付宝)
- $notify_url = $this->request->root(true) . '/api/Alipay/goodsOrderNotify';
- $config = Alipay::ali_pay('订单支付',$pay_no,$order_info['price_total'],$notify_url);
- $ret_data['config']['ali_url'] = $config;
- $ret_data['ali_url'] = $config;
- break;
- case 5 :
- if(!$user_info['openid']) $this->exception('请绑定openid');
- $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;
- }
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->transReturn($ret_data);
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @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:订单状态(0待支付,1已支付(待发货),2已发货(待收货),3已收货(已完成),8已退款,9取消)
- * @param name:pay_state type:int default:-1 desc:支付状态(-1全部,0未支付,1已支付)
- * @param name:cancel_state type:int default:-1 desc:取消状态(-1全部,0未取消,1已取消)
- * @param name:apply_refund type:int default:-1 desc:申请退款状态(-1全部,0未申请||已取消,1已申请)
- * @return name: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:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
- * @return name:pay_type type:int default:-- desc:支付方式(1.h5微信,2.h5支付宝,3移动支付(微信),4移动支付(支付宝),5.h5微信内支付)
- * @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: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已收货(已完成),8已退款,9取消)
- * @return name:create_at type:string 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:add_detail type:string default:-- desc:详细地址(收货地址)
- * @return name:user_name type:string default:-- desc:收货人
- * @return name:phone type:string default:-- desc:收货人联系电话
- * @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:bill_info type:array default:-- desc:发票申请信息
- * @return name:bill_info.header type:string default:0 desc:抬头
- * @return name:bill_info.type type:int default:0 desc:发票类型
- * @return name:bill_info.email type:string default:0 desc:邮箱
- * @return name:bill_info.phone type:string default:0 desc:电话
- * @return name:bill_info.bank type:string default:0 desc:开户行
- * @return name:bill_info.card_no type:string default:0 desc:银行账号
- * @return name:bill_info.identify_number type:string default:0 desc:纳税人识别号
- * @return name:bill_info.bill_img type:string default:0 desc:点子发票url
- * @return name:bill_info.remark type:string default:0 desc:备注
- */
- public function getOrderList()
- {
- $status = input('get.status',-1);
- $pay_state = input('get.pay_state',-1);
- $cancel_state = input('get.cancel_state',-1);
- $refund_state = input('get.apply_refund',-1);
- $where = [];
- $where[] = ['user_id','=',$this->user_id];
- $where[] = ['is_deleted','=',0];
- if($status > -1) $where[] = ['status','=',$status];
- if($pay_state > -1) $where[] = ['pay_state','=',$pay_state];
- if($cancel_state > -1) $where[] = ['cancel_state','=',$cancel_state];
- if($refund_state == 0) $where[] = ['refund_state','=',$refund_state];
- if($refund_state == 1) $where[] = ['refund_state','in','1,2,3,4'];
- $field = 'id,order_no,price_total,price_goods,price_express,create_at,pay_at,pay_type,goods_num,remark,pro_name,city_name,county_name,add_detail,pay_state,user_name,phone,remark,cancel_state,cancel_at,refund_state,express_state,express_company_title,express_send_no,express_send_at';
- $list = GoodsOrder::with(['orderItem'])->field($field)->where($where)->order('id desc ')
- ->limit($this->off_set,$this->page_num)
- ->select()->toArray();
- foreach ($list as &$info) {
- $bill_info = BillApply::field('id,header,type,phone,email,bank,card_no,identify_number,bill_img,remark')->where(['order_type'=>2,'order_id'=>$info['id']])->find();
- $info['bill_info'] = !empty($bill_info) ? $bill_info->toArray() : null;
- foreach ($info['order_item'] as &$v) {
- $v['goods_spec'] = str_replace(['::',';;'],[':',';'],$v['goods_spec']);
- }
- }
- $total_num = GoodsOrder::where($where)->count();
- $this->success('ok',['list'=>$list,'total_count'=>$total_num,'page_num'=>$this->page_num]);
- }
- /**
- * @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:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
- * @return name:pay_type type:int default:-- desc:支付方式(1.h5微信,2.h5支付宝,3移动支付(微信),4移动支付(支付宝),5.h5微信内支付)
- * @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: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已收货(已完成),8已退款,9取消)
- * @return name:create_at type:string 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:add_detail type:string default:-- desc:详细地址(收货地址)
- * @return name:user_name type:string default:-- desc:收货人
- * @return name:phone type:string default:-- desc:收货人联系电话
- * @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:数量
- *
- * @return name:bill_info type:array default:-- desc:发票申请信息
- * @return name:bill_info.header type:string default:0 desc:抬头
- * @return name:bill_info.type type:int default:0 desc:发票类型
- * @return name:bill_info.email type:string default:0 desc:邮箱
- * @return name:bill_info.phone type:string default:0 desc:电话
- * @return name:bill_info.bank type:string default:0 desc:开户行
- * @return name:bill_info.card_no type:string default:0 desc:银行账号
- * @return name:bill_info.identify_number type:string default:0 desc:纳税人识别号
- * @return name:bill_info.bill_img type:string default:0 desc:点子发票url
- * @return name:bill_info.remark type:string default:0 desc:备注
- */
- public function getOrderDetail()
- {
- $order_id = input('get.order_id');
- $field = 'id,order_no,price_total,price_goods,price_express,create_at,pay_at,pay_type,goods_num,remark,pro_name,city_name,county_name,add_detail,pay_state,user_name,phone,remark,cancel_state,cancel_at,refund_state,express_state,express_company_title,express_send_no,express_send_at';
- $detail = GoodsOrder::where('id',$order_id)->field($field)->find()->toArray();
- $detail['order_item'] = (new GoodsOrderItem())->getOrderItem($order_id);
- foreach ($detail['order_item'] as &$v) {
- $v['goods_spec'] = str_replace(['::',';;'],[':',';'],$v['goods_spec']);
- }
- $bill_info = BillApply::field('id,header,type,phone,email,bank,card_no,identify_number,bill_img,remark')->where(['order_type'=>2,'order_id'=>$detail['id']])->find();
- $detail['bill_info'] = !empty($bill_info) ? $bill_info->toArray() : null;
- $this->success('ok',['detail'=>$detail]);
- }
- /**
- * @title 计算商品运费(一个商品)
- * @desc 计算商品运费(一个商品)
- * @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 取消订单
- * @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]);
- // 订单处理
- foreach ($detail['order_item'] as $item_info) {
- // 更改订单详情状态
- GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
- StoreGoodsItem::stockChange($item_info['spec_id'],$item_info['goods_id'],$item_info['num']);
- }
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
- }
- /**
- * @title 删除订单
- * @desc 删除订单
- * @author qc
- * @url /api/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'],
- '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,'express_state'=>2],['id'=>$order_id]);
- GoodsOrderItem::update(['status'=>3,'express_state'=>2],['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/deliveryDetails
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:string require:1 default:-- desc:订单id
- * @return name:number type:string default:-- desc:单号
- * @return name:type type:string default:-- desc:快递类型
- * @return name:list type:array default:-- desc:数据列表
- * @return name:list.time type:string default:-- desc:时间
- * @return name:list.status type:string default:-- desc:内容
- * @return name:deliverystatus type:int default:-- desc:0:快递收件(揽件)1.在途中2.正在派件3.已签收4.派送失败5.疑难件6.退件签收
- * @return name:issign type:int default:-- desc:是否签收(1.是否签收)
- * @return name:expName type:string default:-- desc:快递公司名称
- * @return name:expSite type:string default:-- desc:快递公司官网
- * @return name:expPhone type:string default:-- desc:快递公司电话
- * @return name:courier type:string default:-- desc:快递员 或 快递站(没有则为空)
- * @return name:courierPhone type:string default:-- desc:快递员电话 (没有则为空)
- * @return name:updateTime type:string default:-- desc:快递轨迹信息最新时间
- * @return name:takeTime type:string default:-- desc:发货到收货消耗时长 (截止最新轨迹)
- * @return name:logo type:string default:-- desc:快递公司LOGO
- */
- public function deliveryDetails(){
- $send_no = GoodsOrder::where('id',input('get.order_id'))->value('express_send_no');
- if(!$send_no) $this->error('订单没有发货');
- $data = get_delivery($send_no);
- $this->success('ok',$data);
- }
- /**
- * @title 发货提醒
- * @desc 发货提醒
- * @author qc
- * @url /api/Order/orderUrge
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:string require:1 default:-- desc:订单id
- */
- public function orderUrge()
- {
- $order_id = input('post.order_id');
- $order_info = GoodsOrder::where('id',$order_id)->find();
- if(!$order_info ) $this->error('订单错误');
- if(!$order_info->pay_state != 1 ) $this->error('订单未支付');
- if(!$order_info->express_send_no != '' ) $this->error('订单已发货');
- $urge_ret = ExpressService::orderUrge($order_id,$this->user_id);
- $urge_ret['code'] == 200 ? $this->success($urge_ret['msg']) : $this->error($urge_ret['msg']);
- }
- /**
- * @title 获取订单催发记录
- * @desc 获取订单催发记录
- * @author qc
- * @url /api/Order/getUrgeList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:string require:1 default:-- desc:订单id
- */
- public function getUrgeList()
- {
- $order_id = input('get.order_id');
- $list = OrderLogisticsUrge::where(['order_id'=>$order_id,'table_name'=>'dd_store_order'])->order('id desc')->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- *
- * @title 申请开票
- * @desc 申请开票
- * @author qc
- * @method POST
- * @url /api/Order/applyBill
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:string default:1 desc:订单id
- * @param name:type type:int default:-- desc:类型id
- * @param name:header type:string default:-- desc:抬头名称[公司名称||个人名称]
- * @param name:identify_number type:string default:-- desc:纳税人识别号
- * @param name:email type:string default:-- desc:邮箱
- * @param name:remark type:string default:-- desc:备注
- */
- public function applyBill()
- {
- $order_id = input('post.order_id');
- $post = input('post.');
- $order_info = GoodsOrder::where('id',$order_id)->find()->toArray();
- if(!$order_info['pay_state'])$this->error('订单未支付');
- if(BillApply::checkBillApply($order_id,2)) $this->error('已申请开票');
- $ret_val = UserSynth::buildBillApply($this->user_id, $order_id, 2,$post,0,1,$post['remark']);
- if($ret_val['code'] != 200) $this->error($ret_val['msg']);
- $this->success('申请成功');
- }
- /**
- *
- * @title 订单商品添加购物车
- * @desc 订单商品添加购物车
- * @author qc
- * @method POST
- * @url /api/Order/addTrolleyFromOrder
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:string default:1 desc:订单id
- */
- public function addTrolleyFromOrder(){
- $order_id = input('post.order_id');
- Db::startTrans();
- try {
- $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- foreach ($detail['order_item'] as $item_info) {
- $goods_info = StoreGoods::getGoodsSpec($item_info['goods_id'],$item_info['spec_id']);
- if(!$goods_info) $this->exception('商品已下架');
- if(empty($goods_info['item_list'])) $this->exception('商品规格已下架');
- if($goods_info['item_list'][0]['stock'] < $item_info['num'])$this->exception('库存不足');
- //某商品某规格是否已经添加购物车
- $trolley_id = ShoppingTrolley::checkTrolley($this->user_id,$item_info['goods_id'],$item_info['spec_id']);
- // 添加到购物车
- if($trolley_id) {
- ShoppingTrolley::where('id',$trolley_id)->setInc('num',$item_info['num']);
- }else{
- $trolley_info = [
- 'user_id' => $this->user_id,
- 'goods_id' => $item_info['goods_id'],
- 'spec_id' => $item_info['spec_id'],
- 'goods_no' => $goods_info['item_list'][0]['goods_no'],
- 'num' => $item_info['num'],
- ];
- ShoppingTrolley::create($trolley_info);
- }
- }
- 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);
- }
- }
|