123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?php
- namespace app\api\controller;
- use app\common\model\ExchangeGoods;
- use app\common\service\OrderCallback;
- use app\common\model\ExchangeOrder as EOM;
- use app\common\model\DeliveryAddress;
- use app\common\model\GoodsOrder;
- use app\common\model\User;
- use app\common\model\UserWallet;
- use think\Db;
- use think\Exception;
- /**
- * @title 兑换订单
- * @controller ExchangeOrder
- * @group base
- */
- class ExchangeOrder extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 生成订单(立即购买)
- * @desc 立即购买场景
- * @author qc
- * @url /api/Exchange_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:num type:int default:1 desc:数量
- * @param name:add_id type:int default:1 desc:收货地址id
- * @return name:order_id type:int default:1 desc:订单id
- */
- public function createOrderImmediately()
- {
- $goods_id = input('post.goods_id');
- $num = input('post.num');
- $add_id = input('post.add_id');
- if(!$goods_id ) $this->error('请选择商品');
- if($num <=0 ) $this->error('数量有误');
- if(!$add_id ) $this->error('请选择收货地址');
- Db::startTrans();
- try {
- // 商品详情
- $goods_info = ExchangeGoods::where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
- if(!$goods_info) throw new Exception('商品已下架');
- $goods_info = $goods_info->toArray();
- if($goods_info['stock'] < $num) throw new Exception('库存不足');
- $integral = UserWallet::where('user_id',$this->user_id)->value('integral');
- $need_integral = $goods_info['integral'] * $num;
- if($integral < $need_integral) throw new Exception('积分余额不足');
- $add_info = DeliveryAddress::find($add_id)->toArray();
- $price_goods = bcmul($num,$goods_info['sell_price'],2); // 需要支付价格
- $price_express = 0; // 快递费用
- $order_insert = [
- 'user_id' => $this->user_id,
- 'goods_id' => $goods_id,
- 'goods_name' => $goods_info['name'],
- 'goods_cover' => $goods_info['cover'],
- 'order_no' => get_order_sn(),
- 'price_total' => $price_goods + $price_express,
- 'price_goods' => $price_goods,
- 'price_express' => $price_express,
- 'goods_num' => $num,
- 'integral' => $need_integral,
- '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'],
- ];
- $order_info = EOM::create($order_insert);
- ExchangeGoods::where('id',$goods_id)->setDec('stock',$num);
- if($need_integral) UserWallet::userIntegralChange($this->user_id,$need_integral,'兑换商品',1,-1,$order_info['id']);
- 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/Exchange_order/orderPay
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:-- desc:订单id
- * @param name:pay_type type:int require:1 default:1 desc:支付方式1微信2余额3支付宝
- * @return name:pay_status type:array default:0 desc:支付状态(0待支付,1支付成功,【余额支付或金额为0不需要吊起支付】直接完成订单状态)
- * @return name:config type:array default:0 desc:支付配置
- * @return name:config.appId type:string default:-- desc:公众账号ID(pay_type=1)
- * @return name:config.nonceStr type:string default:-- desc:随机字符串(pay_type=1)
- * @return name:config.signType type:string default:-- desc:签名类型(pay_type=1)
- * @return name:config.paySign type:string default:-- desc:签名(pay_type=1)
- * @return name:config.timestamp type:string default:-- desc:时间戳(pay_type=1)
- */
- public function orderPay()
- {
- $order_id = input('post.order_id');
- $pay_type = input('post.pay_type',1);
- Db::startTrans();
- $ret_data = ['pay_status'=>0,'config'=>[]];
- try{
- $order_info = EOM::where('id',$order_id)->find()->toArray();
- if($order_info['status'] !=0 ) $this->exception('订单状态有误');
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- EOM::where('id',$order_id)->update(['pay_no'=>$pay_no]);
- if($order_info['price_total'] > 0) {
- switch ($pay_type){
- case 1:
- $notify_url = $this->request->root(true) . '/api/WeChatPay/exchangeOrderNotify';
- $user_info = User::where('id',$this->user_id)->find()->toArray();
- $config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if(!$config) $this->exception('获取微信支付配置失败');
- break;
- case 2:
- $money = UserWallet::where('user_id',$this->user_id)->value('money');
- if($money < $order_info['price_total']) $this->exception('佣金余额不足!');
- UserWallet::userMoneyChange($this->user_id,$order_info['price_total'],'佣金支付',1,-1,$order_id);
- $pay_res = OrderCallback::exchangeOrderCallBack($order_info,$pay_type);
- Db::commit();
- $ret_data=['pay_status'=>1,'config'=>[]];
- $this->ret_msg = '支付成功';
- break;
- case 3:
- // 支付宝支付
- break;
- }
- }else{
- OrderCallback::exchangeOrderCallBack($order_info,$pay_type);
- Db::commit();
- $ret_data=['pay_status'=>1,'config'=>[]];
- $this->ret_msg = '支付成功';
- }
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->transReturn($ret_data);
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @method GET
- * @url /api/Exchange_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:状态
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:goods_cover type:string default:--desc:商品封面
- * @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:0 desc:支付状态(0未支付,1已支付)
- * @return name:pay_type type:int default:0 desc:支付方式(1微信2余额3支付宝)
- * @return name:status type:int default:0 desc:订单状态--0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,,9取消
- * @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:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
- * @return name:express_company_id type:int default:-- desc:快递公司id
- * @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:remark type:string 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:收货人电话
- */
- public function getOrderList()
- {
- $status = input('get.status',-1);
- $where = [];
- $where[] = ['user_id','=',$this->user_id];
- if($status > -1) $where[] = ['status','=',$status];
- $list = EOM::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/Exchange_order/getOrderDetail
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int default:-- desc:订单id
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:goods_cover type:string default:--desc:商品封面
- * @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:0 desc:支付状态(0未支付,1已支付)
- * @return name:pay_type type:int default:0 desc:支付方式(1微信2余额3支付宝)
- * @return name:status type:int default:0 desc:订单状态--0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,,9取消
- * @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:express_state type:int default:-- desc:发货状态(0未发货,1已发货,2已签收)
- * @return name:express_company_id type:int default:-- desc:快递公司id
- * @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:remark type:string 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:收货人电话
- */
- public function getOrderDetail()
- {
- $order_id = input('get.order_id');
- $detail = EOM::where('id',$order_id)->find()->toArray();
- $this->success('ok',['detail'=>$detail]);
- }
- /**
- * @title 取消订单
- * @desc 取消订单
- * @author qc
- * @url /api/Exchange_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 = EOM::where('id',$order_id)->find()->toArray();
- if($detail['status'] != 0) $this->exception('订单状态有误');
- // 商品库存
- ExchangeGoods::where('id',$detail['goods_id'])->setInc('stock',$detail['goods_num']);
- // 取消订单状态
- GoodsOrder::update(['status'=>9],['id'=>$order_id]);
- // 积分退回
- if($detail['integral']) UserWallet::userIntegralChange($detail['user_id'],$detail['integral'],'订单取消退回',2,1,$order_id);
- 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/Exchange_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 = EOM::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/Exchange_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 = EOM::where('id',$order_id)->find()->toArray();
- if($detail['status'] != 0) $this->error('操作错误');
- $add_info = DeliveryAddress::find($add_id)->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'],
- ];
- EOM::update($order_update,['id'=>$order_id]);
- $this->success('修改成功');
- }
- /**
- * @title 确认收货
- * @desc 确认收货
- * @author qc
- * @url /api/Exchange_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 =EOM::where('id',$order_id)->find()->toArray();
- if($detail['status'] == 0) $this->error('订单未支付');
- if($detail['status'] == 1) $this->error('订单未发货');
- if($detail['status'] == 3) $this->error('订单已收货');
- EOM::update(['status'=>3],['id'=>$order_id]);
- $this->success('已确认收货');
- }
- }
|