OrderController.php 5.4 KB
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/24
 * Time: 8:54
 */

namespace app\portal\controller;


use cmf\controller\HomeBaseController;
use think\Db;
use wxapp\pay\WeixinPay;

/**
 * @title 订单相关接口
 * @description 订单相关接口
 * @group 订单相关接口
 */
class OrderController extends HomeBaseController{

    /**
     * @title 生成订单
     * @description 生成订单,加入开始时间和用户id
     * @author 董瑞恩
     * @url /portal/order/createOrder
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     * @param name:mac_address type:String require:1 default:无 other: desc:设备Mac地址
     *
     */
    public function createOrder(){
        $users_id=$this->request->param('users_id');
        $mac_address=$this->request->param('mac_address');
        $order=[
            'order_no' => cmf_get_order_sn(),
            'mac_address' => $mac_address,
            'users_id' => $users_id,
            'start_time' => time(),
            'state' => 1
        ];
        try{
            Db::startTrans();
            Db::name('users')->where('id',$users_id)->update(['is_use'=>1,'order_no'=>$order['order_no']]);
            Db::name('order')->insert($order);
        }catch (\Exception $exception){
            Db::rollback();
            $this->apiResponse(301,'订单生成失败,错误信息:'.$exception->getMessage());
        }
        Db::commit();
        $this->apiResponse(200,'success');
    }

    /**
     * @title 完成订单
     * @description 订单完成,加入结束时间和结算费用,并调用微信统一下单
     * @author 董瑞恩
     * @url /portal/order/order
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
     * @return data:返回用于调用支付的参数
     */
    public function order(){
        $users_id=$this->request->param('users_id');
        $users=Db::name('users')->where('id',$users_id)->find();
        $order= Db::name('order')->where('order_no',$users['order_no'])->find();
        $end_time=time();
        $price=$this->getPrice($order['start_time'],$end_time);
        $data=[
            'end_time'=>$end_time,
            'price' => $price,
            'state' => 2
        ];
        try{
            Db::startTrans();
            Db::name('users')->where('users_id',$users_id)->update(['is_use'=>0,'order_no'=>null]);
            Db::name('order')->where('order_no',$users['order_no'])->update($data);
        }catch (\Exception $exception){
            Db::rollback();
            $this->apiResponse(301,'订单生成失败,错误信息:'.$exception->getMessage());
        }
        Db::commit();
        $this->pay($order['order_no']);
    }

    public function getPrice($start_time,$end_time){
        $cost=Db::name('cost')->where('id',1)->find();
        $interval = Db::name('interval')->where('id',1)->find();

        //判断是否在时间段内
        $interval['start_time'];
        if ($end_time > $interval['start_time'] && $end_time < $interval['end_time']){
            if ($start_time > $interval['start_time']){

            }else if ($start_time < $interval['start_time']){

            }
        }elseif ($start_time < $interval['end_time'] && $end_time > $interval['end_time']){

        }else{

        }


        $price=1;
        return $price;
    }

    /**
     * @title 统一下单
     * @description 微信统一下单
     * @author 董瑞恩
     * @url /portal/order/pay
     * @method GET
     *
     * @param name:order_no type:String require:1 default:无 other: desc:订单号
     * @param name:openId type:String require:1 default:无 other: desc:openId
     *
     * @return data:返回用于调用支付的参数
     */
    public function pay($order_no){
        $order=Db::name('order')->where(['order_no'=>$order_no,'state'=>2])->find();
        $openId=Db::name('users')->where('users_id',$order['users_id'])->find()['open_id'];
        $body='支付';
        $price=$order['price']*100;//订单价格
        $notify_url=url('order/notify','','',true);//回调地址
        $wxPay=new WeixinPay($openId,$order_no,$body,$price,$notify_url);
        $data=$wxPay->pay();
        if (isset($data['package'])){
            $this->apiResponse(200,'下单成功',$data);//微信支付下单成功,返回调用支付的参数
        }
    }

    //支付回调接口
    public function notify(){
        $param = $this->request->param();
        if ($param == null) {
            $param = file_get_contents("php://input");
            if ($param == null) {
                $param = $GLOBALS['HTTP_RAW_POST_DATA'];
            }
        }
        $wxPay=new WeixinPay();
        $data = $wxPay->xmlToArray($param);
        $Sign = $data['sign'];
        //支付成功回调后变更订单状态
        $mySign = $wxPay->getSign($data);
        $order_no = $data['out_trade_no'];
        if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
            try{
                Db::name('order')->where(['order_no'=>$order_no])->update(['state'=>3]);
            }catch (\Exception $exception){
                $this->apiResponse(301,'error:'.$exception->getMessage());
            }
            return "<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                    </xml>";

        }
    }

}