作者 lihan

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

@@ -180,4 +180,4 @@ class ActivityController extends HomeBaseController @@ -180,4 +180,4 @@ class ActivityController extends HomeBaseController
180 return $res; 180 return $res;
181 } 181 }
182 182
183 -} 183 +}
@@ -11,6 +11,15 @@ use think\Db; @@ -11,6 +11,15 @@ use think\Db;
11 11
12 class CouponModel extends Model 12 class CouponModel extends Model
13 { 13 {
  14 + /**
  15 + * 个人中心优惠券列表
  16 + * @param null $status
  17 + * @param $user_id
  18 + * @return array
  19 + * @throws \think\db\exception\DataNotFoundException
  20 + * @throws \think\db\exception\ModelNotFoundException
  21 + * @throws \think\exception\DbException
  22 + */
14 public function getAllDiscountCouponByUniqueIdAndByStatus($status = null, $user_id) 23 public function getAllDiscountCouponByUniqueIdAndByStatus($status = null, $user_id)
15 { 24 {
16 if ($status == null) { 25 if ($status == null) {
@@ -26,4 +35,36 @@ class CouponModel extends Model @@ -26,4 +35,36 @@ class CouponModel extends Model
26 return $res; 35 return $res;
27 } 36 }
28 37
  38 + /**
  39 + * 根据金额获取符合条件的满减优惠券
  40 + * @param $activity_id
  41 + * @param null $schedule_id
  42 + * @param $num
  43 + * @param $userId
  44 + * @return array
  45 + * @throws \think\db\exception\DataNotFoundException
  46 + * @throws \think\db\exception\ModelNotFoundException
  47 + * @throws \think\exception\DbException
  48 + */
  49 + public function orderCoupon($activity_id, $schedule_id = null, $num, $userId)
  50 + {
  51 + $info = Db::name('activity')->field('is_down_payment,down_price')->where(['id' => $activity_id])->find();
  52 + //如果有定金,则按照定金计算满减
  53 + if ($info['is_down_payment'] == 1) {
  54 + $overflow = $info['down_price'] * $num;
  55 + } //如果没有定金,则直接按照选中的活动批次价格计算满减
  56 + else {
  57 + $price = Db::name('activity_schedule')->where(['activity_id' => $activity_id, 'id' => $schedule_id])->value('price');
  58 + $overflow = $price * $num;
  59 + }
  60 + $map = [
  61 + 'user_id' => ['eq', $userId],
  62 + 'overflow' => ['elt', $overflow],
  63 + 'deadline' => ['gt', time()],
  64 + 'status' => ['eq', 1]
  65 + ];
  66 + $coupons = Db::name('discount_coupon')->field('id as discount_coupon_id,discount_coupon_name,reduce')->where($map)->select()->toArray();
  67 + return (empty($coupons)) ? null : $coupons;
  68 + }
  69 +
29 } 70 }
1 <?php 1 <?php
2 namespace app\order\controller; 2 namespace app\order\controller;
  3 +use app\coupons\model\CouponModel;
3 use cmf\controller\HomeBaseController; 4 use cmf\controller\HomeBaseController;
4 use think\Db; 5 use think\Db;
5 6
@@ -9,21 +10,129 @@ use think\Db; @@ -9,21 +10,129 @@ use think\Db;
9 class OrderController extends HomeBaseController 10 class OrderController extends HomeBaseController
10 { 11 {
11 12
  13 + function __construct()
  14 + {
  15 + if (empty(session('user.id'))) {
  16 + echo json_encode(['msg' => '登陆失败', 'code' => 40001]);
  17 + exit();
  18 + }
  19 + }
  20 +
12 /** 21 /**
13 - * @title 活动列表 22 + * @title 下单页面
14 * @description 默认访问接口 23 * @description 默认访问接口
15 * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ 24 * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
16 * @url /order/Order/confirm 25 * @url /order/Order/confirm
17 * @method POST 26 * @method POST
  27 + *
  28 + * @param name:activity_id type:int require:1 default:19 desc:上一页选中的活动id
  29 + * @param name:schedule_id type:int require:1 default:68 desc:上一页选中的批次id
  30 + * @param name:num type:int require:1 default:2 desc:上一页选中的购买数量
18 */ 31 */
19 public function confirm() 32 public function confirm()
20 { 33 {
21 $request = request(); 34 $request = request();
22 - $activity_id = $request->param('activity_id'); 35 + $post = $request->param();
  36 + $coupons = new CouponModel;
  37 + $couponsList = $coupons->orderCoupon($post['activity_id'], $post['schedule_id'], $post['num'], session('user.id'));
  38 + $schedule = Db::name('activity_schedule')
  39 + ->field('id as schedule_id,start_time,end_time,maximum,real_join_num,addition_join_num')
  40 + ->where(['activity_id' => $post['activity_id']])
  41 + ->select();
  42 + foreach ($schedule as $k => $v) {
  43 + $v['start_time'] = date('Y.m.d', $v['start_time']);
  44 + $v['end_time'] = date('Y.m.d', $v['end_time']);
  45 + $allowMaximum = $v['maximum'] - ($v['real_join_num'] + $v['addition_join_num']);
  46 + $v['allow_maximum'] = $allowMaximum;
  47 + unset($v['real_join_num']);
  48 + unset($v['addition_join_num']);
  49 + unset($v['maximum']);
  50 + $schedule[$k] = $v;
  51 + }
  52 + $result = [
  53 + 'activity' => Db::name('activity')->field('id as activity,name')->where(['id' => $post['activity_id']])->find(),
  54 + 'schedule' => $schedule,
  55 + 'coupons' => $couponsList
  56 + ];
  57 + echo json_encode(['data' => $result]);
  58 + exit();
  59 + }
  60 +
  61 + /**
  62 + * @title 提交订单
  63 + * @description 默认访问接口
  64 + * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
  65 + * @url /order/Order/done
  66 + * @method POST
  67 + *
  68 + * @param name:activity_id type:int require:1 default:19 desc:上一页选中的活动id
  69 + * @param name:schedule_id type:int require:1 default:68 desc:上一页选中的批次id
  70 + * @param name:num type:int require:1 default:2 desc:上一页选中的购买数量
  71 + * @param name:escort type:char require:1 default:同行人id字符串(逗号分隔) desc:1,2,3
  72 + * @param name:payment type:int require:1 default:支付方式 desc:0余额,1微信,2支付宝
  73 + * @param name:discount_coupon_id type:int require:1 default:1 desc:优惠券id
  74 + * @param name:room type:char require:1 default:1 desc:房间信息
  75 + * @param name:desc type:text require:1 default:1 desc:商家备注
  76 + */
  77 + public function done()
  78 + {
  79 + $request = request();
23 if ($request->isPost()) { 80 if ($request->isPost()) {
24 - $activity = Db::name('activity')->field('name')->find(); 81 + $post = $request->param();
  82 + $data['user_id'] = session('user.id');
  83 + $data['order_sn'] = date('YmdHis') . rand(0, 9999);
  84 + $data['activity_id'] = $post['activity_id'];
  85 + $data['schedule_id'] = $post['schedule_id'];
  86 + $data['add_time'] = time();
  87 + $activity = Db::name('activity')->field('is_down_payment,down_price')->where(['id' => $post['activity_id']])->find();
  88 + $schedule = Db::name('activity_schedule')->field('deadline,price')->where(['id' => $post['schedule_id']])->find();
  89 + $data['order_type'] = $activity['is_down_payment'];
  90 + $data['is_use_discount_coupon'] = (empty($post['discount_coupon_id'])) ? 0 : 1;
  91 + $data['discount_coupon_id'] = $post['discount_coupon_id'];
  92 + $data['status'] = 0;
  93 + if ($data['order_type'] == 0) {
  94 + $data['payment1'] = $post['payment'];
  95 + } else {
  96 + $post['payment2'] = $post['payment'];
  97 + }
  98 + Db::startTrans();
  99 + if (Db::name('order_info')->insert($data)) {
  100 + $oid = Db::name('order_info')->getLastInsID();
  101 + $detail = [];
  102 + $explode_escort = explode(',', $post['escort']);
  103 + for ($i = 0; $i < $post['num']; $i++) {
  104 + $detail[$i] = [
  105 + 'oid' => $oid,
  106 + 'escort_id' => $explode_escort[$i],
  107 + 'down_price' => $activity['down_price'],
  108 + 'final_payment' => $schedule['price'],
  109 + 'status' => 0
  110 + ];
  111 + }
  112 + if (Db::name('order_detail')->insertAll($detail)) {
  113 + Db::commit();
  114 + //选择支付方式0,1,2
  115 + //余额支付
  116 + if ($post['payment'] == 0) {
  117 +
  118 + } elseif ($post['payment'] == 1) {
  119 +
  120 + } elseif ($post['payment'] == 2) {
  121 +
  122 + } else {
  123 + echo json_encode(['msg' => '请选择支付方式', 'code' => 40000]);
  124 + exit();
  125 + }
  126 + }
  127 + } else {
  128 + Db::rollback();
  129 + echo json_encode(['msg' => '下单失败', 'code' => 40000]);
  130 + exit();
  131 + }
  132 +
25 } else { 133 } else {
26 - return $this->fetch(); 134 + echo json_encode(['msg' => '非法操作', 'code' => 40000]);
  135 + exit();
27 } 136 }
28 } 137 }
29 138
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: yhbr
  5 + * Date: 2018/9/5
  6 + * Time: 15:03
  7 + */
  8 +namespace app\order\model;
  9 +use think\Model;
  10 +use think\Db;
  11 +
  12 +class OrderModel extends Model
  13 +{
  14 +
  15 + //暂定
  16 + public function orderList($status, $orderSn)
  17 + {
  18 + $where = [];
  19 + if ($orderSn != null) {
  20 + $where['order_sn'] = ['like', "%$orderSn%"];
  21 + }
  22 + if ($status !== false) {
  23 + $where['status'] = ['eq', $status];
  24 + }
  25 + return Db::name('order_info')->where($where)->select();
  26 + }
  27 +
  28 +}
@@ -20,7 +20,6 @@ class CenterController extends HomeBaseController @@ -20,7 +20,6 @@ class CenterController extends HomeBaseController
20 20
21 function __construct() 21 function __construct()
22 { 22 {
23 - session('user.id', 2);  
24 if (empty(session('user.id'))) { 23 if (empty(session('user.id'))) {
25 echo json_encode(['msg' => '登陆失败', 'code' => 40001]); 24 echo json_encode(['msg' => '登陆失败', 'code' => 40001]);
26 exit(); 25 exit();