OrderController.php 10.1 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'],'user_id'=>$user_id])->find();
                $need = $user_need_model->where(['id'=>$info['need_id'],'user_id'=>$user_id])->find();
                if($need != 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,手机支付宝;5,余额
     *
     */
    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'],'user_id'=>$user['id']])->find();
                    $need = $user_need_model->where(['id'=>$info['need_id'],'user_id'=>$user['id']])->find();
                    if($need != 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)->isUpdate(true)->save($update);
//                                $status = $user_need_model->where()->isUpdate(true)->save(['status'=>2,'pay_time'=>time()]);
//                            }
//                            if($orderInfo['type'] == 2) {
//                                $pay = $order_model->where($where)->isUpdate(true)->save($update);
//                                $status = $agency_model->where()->isUpdate(true)->save(['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)->isUpdate(true)->save($update);
//                                $status = 1;
//                            }
//                            if(!$pay || !$status) {
//                                $order_model->rollback();
//                                $this->apiResponse(0,'支付失败');
//                            }
//                            $order_model->commit();
//                            $this->apiResponse(1,'支付成功');
//                        }
//                    } else
                    if($data['_type'] == 2) {
                        $return = $this->wxpay(['order_sn'=>$data['order_sn']],$sort);
                        $qrcode_url = url('portal/Index/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',array('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']);
        }
    }
}