OrderController.php
5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?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();
}
}
}