OrderController.php 12.3 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/7/21
 * Time: 14:38
 */

namespace api\portal\controller;

use api\portal\model\AgencyModel;
use api\portal\model\MemberModel;
use api\portal\model\OrderModel;
use api\portal\model\UserNeedModel;
use api\portal\controller\IntermediaryController;

/**
 * @title 订单接口
 * @description 接口说明
 * @group 接口分组
 */
class OrderController extends CommonController
{

    /**
     * @title  生成订单(需求、中介报价、透视卡)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Order/createOrder
     * @method POST
     * @param name:token type:char require:1 default: other: desc:用户token
     * @param name:need_id type:int require:1 default: other: desc:需求(中介报价)ID
     * @param name:_type type:int require:1 default: other: desc:订单类型:1,需求;2,中介报价;3,透视卡
     *
     */
    public function createOrder()
    {
        if($this->request->isPost()) {
            $data = $this->request->param();
            $member_model = new MemberModel();
            $user_id = $member_model->where(['token'=>$data['token']])->value('id');
            $user_need_model = new UserNeedModel();
            $agency_model = new AgencyModel();
            $order_model = new OrderModel();
            if($data['_type'] == 1 || $data['_type'] == 3) {
                $info = $user_need_model->where(['id'=>$data['need_id'],'user_id'=>$user_id])->find();
                if(empty($info['status'])) {
                    $this->apiResponse(0,'需求不存在');
                }
                $need_id = $info['id'];
                $money = $info['pay_money'];
            }
            if($data['_type'] == 2) {
                $info = $agency_model->where(['id'=>$data['need_id'],'agency_id'=>$user_id])->find();
                $need = $user_need_model->where(['id'=>$info['need_id']])->find();
                if($need['status'] != 3) {
                    $this->apiResponse(0,'该需求当前状态无法进行报价');
                }
                $need_id = $info['id'];
                $money = $info['deposit'];
            }
            if(($data['_type'] == 1 && $info['status'] == 1) ||
                ($data['_type'] == 3 && $info['status'] == 3) ||
                ($data['_type'] == 2 && $info['status'] == 1)) {
                $orderInfo = $order_model->where(['type'=>$data['_type'],'need_id'=>$need_id,'user_id'=>$user_id])->find();
                if(empty($orderInfo)) {
                    $order = [
                        'order_sn' => cmf_get_order_sn(),
                        'user_id' => $user_id,
                        'type' => $data['_type'],
                        'need_id' => $need_id,
                        'is_get' => 2,
                        'money' => $money,
                        'status' => 1,
                        'create_time' => time()
                    ];
                    $result = $this->validate($order, 'portal/Order');
                    if ($result !== true) {
                        $this->apiResponse(0,$result);
                    }
                    $order_model = new OrderModel();
                    $insert = $order_model->isUpdate(false)->allowField(true)->insertGetId($order);
                    if(!$insert) {
                        $this->apiResponse(0,'订单生成失败');
                    }
                } else {
                    $order = $orderInfo;
                }
                $this->apiResponse(1,'订单生成成功',$order['order_sn']);
            } else {
                $this->apiResponse(0,'无法生成订单');
            }
        }
    }

    /**
     * @title  选择订单支付方式
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Order/changePayType
     * @method POST
     * @param name:token type:char require:1 default: other: desc:用户token
     * @param name:order_sn type:char require:1 default: other: desc:订单编号
     * @param name:_type type:int require:1 default: other: desc:支付类型:1,pc微信;2,手机微信;3,pc支付宝;4,手机支付宝
     *
     */
    public function changePayType()
    {
        if($this->request->isPost()) {
            $data = $this->request->param();
            $member_model = new MemberModel();
            $user = $member_model->field(['id,money'])->where(['token'=>$data['token']])->find();
            $order_model = new OrderModel();
            $orderInfo = $order_model->where(['order_sn'=>$data['order_sn'],'user_id'=>$user['id']])->find();
            if(empty($orderInfo['status'])) {
                $this->apiResponse(0,'订单不存在');
            }
            if($orderInfo['status'] == 1) {
                $user_need_model = new UserNeedModel();
                $agency_model = new AgencyModel();
                if($orderInfo['type'] == 1) {
                    $info = $user_need_model->where(['id'=>$orderInfo['need_id'],'user_id'=>$user['id']])->find();
                    $sort = '需求保证金支付';
                }
                if($orderInfo['type'] == 2) {
                    $info = $agency_model->where(['id'=>$orderInfo['need_id'],'agency_id'=>$user['id']])->find();
                    $need = $user_need_model->where(['id'=>$info['need_id'],'user_id'=>$user['id']])->find();
                    if($need['status'] != 3) {
                        $this->apiResponse(0,'该需求当前状态无法进行报价');
                    }
                    $sort = '中介报价保证金支付';
                }
                if($orderInfo['type'] == 3) {
                    $info = $user_need_model->where(['id'=>$orderInfo['need_id'],'user_id'=>$user['id']])->find();
                    $sort = '透视卡支付';
                }
                if(($orderInfo['type'] == 1 && $info['status'] == 1) ||
                    ($orderInfo['type'] == 3 && $info['status'] == 3) ||
                    ($orderInfo['type'] == 2 && $info['status'] == 1)) {
//                    if($data['_type'] == 5) {
//                        if($user['money'] < $orderInfo['money']) {
//                            $this->apiResponse(0,'余额不足');
//                        } else {
//                            $order_model->startTrans();
//                    $where = [
//                        'order_sn' => $data['order_sn']
//                    ];
//                    $update = [
//                        'status' => 2,
//                        'pay_type' => 5,
//                        'pay_time' => time()
//                    ];
//                    if($orderInfo['type'] == 1) {
//                        $pay = $order_model->where($where)->update($update);
//                        $status = $user_need_model->where()->update(['status'=>2,'pay_time'=>time()]);
//                    }
//                    if($orderInfo['type'] == 2) {
//                        $pay = $order_model->where($where)->update($update);
//                        $status = $agency_model->where()->update(['status'=>2,'pay_time'=>time()]);
//                    }
//                    if($orderInfo['type'] == 3) {
//                        $Intermediary = controller('Intermediary','controller');
//                        $agencyOffer = $Intermediary->agencyOffer($user['id'],$info['id']);
//                        $update['lower_price'] = $agencyOffer['money'];
//                        $pay = $order_model->where($where)->update($update);
//                        $status = 1;
//                    }
//                    if(!$pay || !$status) {
//                        $order_model->rollback();
//                        $this->apiResponse(0,'支付失败');
//                    }
//                            $order_model->commit();
//                            $this->apiResponse(5,'支付成功');
//                        }
//                    }
                    if($data['_type'] == 1) {
                        $return = $this->wxpay(['order_sn'=>$data['order_sn']],$sort);
                        $qrcode_url = url('portal/Qrcode/qrcode',['data'=>urlencode($return['code_url'])],true,true);
                        $this->apiResponse(1,'微信支付',['type'=>1,'url'=>$qrcode_url]);
                    } elseif($data['_type'] == 3) {
                        $alipay_url = url('portal/Alipay/alipay',['order_sn'=>$data['order_sn'],'name'=>$sort,'price'=>0.01],true,true);
                        $this->apiResponse(1,'支付宝支付',['type'=>3,'url'=>$alipay_url]);
                    }
                } else {
                    $this->apiResponse(0,'该订单已失效');
                }
            } else {
                $this->apiResponse(0,'该订单已支付');
            }
        }
    }

    /**
     * @title  支付页面订单金额查询
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Order/getOrderPrice
     * @method POST
     * @param name:token type:char require:1 default: other: desc:用户token
     * @param name:order_sn type:char require:1 default: other: desc:订单编号
     *
     */
    public function getOrderPrice()
    {
        if($this->request->isPost()) {
            $data = $this->request->param();
            $member_model = new MemberModel();
            $user = $member_model->field(['id,money'])->where(['token'=>$data['token']])->find();
            $order_model = new OrderModel();
            $orderInfo = $order_model->where(['order_sn'=>$data['order_sn'],'user_id'=>$user['id']])->find();
            if(empty($orderInfo['status'])) {
                $this->apiResponse(0,'订单不存在');
            }
            if($orderInfo['status'] == 2) {
                $this->apiResponse(0,'订单已付款');
            }
            if($orderInfo['status'] == 4) {
                $this->apiResponse(0,'订单已退款');
            }
            $this->apiResponse(1,'成功',$orderInfo['money']);
        }
    }


    /**
     * @title  支付页面检测微信支付是否完成
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Order/checkWechatWebPay
     * @method POST
     * @param name:token type:char require:1 default: other: desc:用户token
     * @param name:order_sn type:char require:1 default: other: desc:订单编号
     *
     */
    public function checkWechatWebPay() {
        if($this->request->isPost()) {
            $data = $this->request->param();
            $member_model = new MemberModel();
            $user = $member_model->field(['id,money'])->where(['token'=>$data['token']])->find();
            $order_sn = $data['order_sn'];
            $order_model = new OrderModel();
            $status = $order_model->where(['order_sn'=>$order_sn])->value('status');
            if(empty($status)) {
                $this->apiResponse(0,'订单不存在');
            }
            if($status == 1 || $status == 9) {
                $this->apiResponse(0,'未支付成功');
            }
            if($status == 2) {
                $this->apiResponse(1,'支付成功');
            }
        }
    }

    /**
     * @title  根据订单号查询订单状态,支付结果页面显示不同内容
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Order/getOrderType
     * @method POST
     * @param name:token type:char require:1 default: other: desc:用户token
     * @param name:order_sn type:char require:1 default: other: desc:订单编号
     *
     */
    public function getOrderType() {
        if($this->request->isPost()) {
            $data = $this->request->param();
            $member_model = new MemberModel();
            $user = $member_model->field(['id,money'])->where(['token'=>$data['token']])->find();
            $order_sn = $data['order_sn'];
            $order_model = new OrderModel();
            $type = $order_model->where(['order_sn'=>$order_sn,'user_id'=>$user['id'],'status'=>2])->value('type');
            if(empty($type)) {
                $this->apiResponse(0,'订单未支付');
            }
            $name = '';
            if($type == 1) {
                $name = '需求押金缴纳成功';
            }
            if($type == 2) {
                $name = '中介押金缴纳成功';
            }
            if($type == 3) {
                $name = '透视卡支付成功';
            }
            $this->apiResponse(1,'成功', $name);
        }
    }
}