作者 lihan

订单确认页面数据、下单逻辑、满减优惠券逻辑

... ... @@ -11,6 +11,15 @@ use think\Db;
class CouponModel extends Model
{
/**
* 个人中心优惠券列表
* @param null $status
* @param $user_id
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getAllDiscountCouponByUniqueIdAndByStatus($status = null, $user_id)
{
if ($status == null) {
... ... @@ -26,4 +35,36 @@ class CouponModel extends Model
return $res;
}
/**
* 根据金额获取符合条件的满减优惠券
* @param $activity_id
* @param null $schedule_id
* @param $num
* @param $userId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function orderCoupon($activity_id, $schedule_id = null, $num, $userId)
{
$info = Db::name('activity')->field('is_down_payment,down_price')->where(['id' => $activity_id])->find();
//如果有定金,则按照定金计算满减
if ($info['is_down_payment'] == 1) {
$overflow = $info['down_price'] * $num;
} //如果没有定金,则直接按照选中的活动批次价格计算满减
else {
$price = Db::name('activity_schedule')->where(['activity_id' => $activity_id, 'id' => $schedule_id])->value('price');
$overflow = $price * $num;
}
$map = [
'user_id' => ['eq', $userId],
'overflow' => ['elt', $overflow],
'deadline' => ['gt', time()],
'status' => ['eq', 1]
];
$coupons = Db::name('discount_coupon')->field('id as discount_coupon_id,discount_coupon_name,reduce')->where($map)->select()->toArray();
return (empty($coupons)) ? null : $coupons;
}
}
\ No newline at end of file
... ...
<?php
namespace app\order\controller;
use app\coupons\model\CouponModel;
use cmf\controller\HomeBaseController;
use think\Db;
... ... @@ -9,21 +10,129 @@ use think\Db;
class OrderController extends HomeBaseController
{
function __construct()
{
if (empty(session('user.id'))) {
echo json_encode(['msg' => '登陆失败', 'code' => 40001]);
exit();
}
}
/**
* @title 活动列表
* @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();
$activity_id = $request->param('activity_id');
$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()) {
$activity = Db::name('activity')->field('name')->find();
$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 {
return $this->fetch();
echo json_encode(['msg' => '非法操作', 'code' => 40000]);
exit();
}
}
... ...
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/9/5
* Time: 15:03
*/
namespace app\order\model;
use think\Model;
use think\Db;
class OrderModel extends Model
{
//暂定
public function orderList($status, $orderSn)
{
$where = [];
if ($orderSn != null) {
$where['order_sn'] = ['like', "%$orderSn%"];
}
if ($status !== false) {
$where['status'] = ['eq', $status];
}
return Db::name('order_info')->where($where)->select();
}
}
\ No newline at end of file
... ...
... ... @@ -20,7 +20,6 @@ class CenterController extends HomeBaseController
function __construct()
{
session('user.id', 2);
if (empty(session('user.id'))) {
echo json_encode(['msg' => '登陆失败', 'code' => 40001]);
exit();
... ...