OrderController.php 5.5 KB
<?php
namespace app\order\controller;
use app\coupons\model\CouponModel;
use cmf\controller\HomeBaseController;
use think\Db;

/**
 * @title 订单模块
 */
class OrderController extends HomeBaseController
{

    function __construct()
    {
        if (empty(session('user.id'))) {
            echo json_encode(['msg' => '登陆失败', 'code' => 40001]);
            exit();
        }
    }

    /**
     * @title 下单页面
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /order/Order/confirm
     * @method POST
     *
     * @param name:activity_id  type:int require:1 default:19 desc:上一页选中的活动id
     * @param name:schedule_id  type:int require:1 default:68 desc:上一页选中的批次id
     * @param name:num  type:int require:1 default:2 desc:上一页选中的购买数量
     */
    public function confirm()
    {
        $request = request();
        $post = $request->param();
        $coupons = new CouponModel;
        $couponsList = $coupons->orderCoupon($post['activity_id'], $post['schedule_id'], $post['num'], session('user.id'));
        $schedule = Db::name('activity_schedule')
            ->field('id as schedule_id,start_time,end_time,maximum,real_join_num,addition_join_num')
            ->where(['activity_id' => $post['activity_id']])
            ->select();
        foreach ($schedule as $k => $v) {
            $v['start_time'] = date('Y.m.d', $v['start_time']);
            $v['end_time'] = date('Y.m.d', $v['end_time']);
            $allowMaximum = $v['maximum'] - ($v['real_join_num'] + $v['addition_join_num']);
            $v['allow_maximum'] = $allowMaximum;
            unset($v['real_join_num']);
            unset($v['addition_join_num']);
            unset($v['maximum']);
            $schedule[$k] = $v;
        }
        $result = [
            'activity' => Db::name('activity')->field('id as activity,name')->where(['id' => $post['activity_id']])->find(),
            'schedule' => $schedule,
            'coupons' => $couponsList
        ];
        echo json_encode(['data' => $result]);
        exit();
    }

    /**
     * @title 提交订单
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /order/Order/done
     * @method POST
     *
     * @param name:activity_id  type:int require:1 default:19 desc:上一页选中的活动id
     * @param name:schedule_id  type:int require:1 default:68 desc:上一页选中的批次id
     * @param name:num  type:int require:1 default:2 desc:上一页选中的购买数量
     * @param name:escort  type:char require:1 default:同行人id字符串(逗号分隔) desc:1,2,3
     * @param name:payment  type:int require:1 default:支付方式 desc:0余额,1微信,2支付宝
     * @param name:discount_coupon_id  type:int require:1 default:1 desc:优惠券id
     * @param name:room  type:char require:1 default:1 desc:房间信息
     * @param name:desc  type:text require:1 default:1 desc:商家备注
     */
    public function done()
    {
        $request = request();
        if ($request->isPost()) {
            $post = $request->param();
            $data['user_id'] = session('user.id');
            $data['order_sn'] = date('YmdHis') . rand(0, 9999);
            $data['activity_id'] = $post['activity_id'];
            $data['schedule_id'] = $post['schedule_id'];
            $data['add_time'] = time();
            $activity = Db::name('activity')->field('is_down_payment,down_price')->where(['id' => $post['activity_id']])->find();
            $schedule = Db::name('activity_schedule')->field('deadline,price')->where(['id' => $post['schedule_id']])->find();
            $data['order_type'] = $activity['is_down_payment'];
            $data['is_use_discount_coupon'] = (empty($post['discount_coupon_id'])) ? 0 : 1;
            $data['discount_coupon_id'] = $post['discount_coupon_id'];
            $data['status'] = 0;
            if ($data['order_type'] == 0) {
                $data['payment1'] = $post['payment'];
            } else {
                $post['payment2'] = $post['payment'];
            }
            Db::startTrans();
            if (Db::name('order_info')->insert($data)) {
                $oid = Db::name('order_info')->getLastInsID();
                $detail = [];
                $explode_escort = explode(',', $post['escort']);
                for ($i = 0; $i < $post['num']; $i++) {
                    $detail[$i] = [
                        'oid' => $oid,
                        'escort_id' => $explode_escort[$i],
                        'down_price' => $activity['down_price'],
                        'final_payment' => $schedule['price'],
                        'status' => 0
                    ];
                }
                if (Db::name('order_detail')->insertAll($detail)) {
                    Db::commit();
                    //选择支付方式0,1,2
                    //余额支付
                    if ($post['payment'] == 0) {

                    } elseif ($post['payment'] == 1) {

                    } elseif ($post['payment'] == 2) {

                    } else {
                        echo json_encode(['msg' => '请选择支付方式', 'code' => 40000]);
                        exit();
                    }
                }
            } else {
                Db::rollback();
                echo json_encode(['msg' => '下单失败', 'code' => 40000]);
                exit();
            }

        } else {
            echo json_encode(['msg' => '非法操作', 'code' => 40000]);
            exit();
        }
    }

}