Order.php
13.7 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
namespace app\api\controller;
use app\admin\model\Third;
use app\api\model\goods\GoodsSku;
use app\api\model\order\OrderItem;
use app\api\model\promoter\PromoterLast;
use addons\epay\library\Service;
use app\api\validate\OrderValidate;
use app\common\controller\Api;
use think\Db;
use think\Model;
/**
* 订单接口
*/
class Order extends Api {
protected $noNeedLogin = [''];
protected $noNeedRight = ['*'];
/**
* 下单是否显示推广码和可填推广码
* @ApiTitle (下单是否显示推广码和可填推广码)
* @ApiReturnParams (name="status", type="string", description="推广码状态:true=可以填写推广码,false=不可填写,显示推广码,置灰显示")
* @ApiReturnParams (name="promotion_code", type="string", description="订单id")
*/
public function promotion_code_status() {
//判断下单人是否有上级
$up_user_id = $this->auth->up_user_id;
//如果没有绑定上级
if ( $up_user_id == 0 ) {
$status = true;
$this->success( '状态为', ['status' => $status] );
}
//如果存在
$status = false;
$promotion_code = \db( 'user' )->where( 'id', $up_user_id )->value( 'promotion_no' );
$username = \db( 'user' )->where( 'id', $up_user_id )->value( 'username' );
$this->success( '状态为', ['status' => $status, 'promotion_code' => $promotion_code, 'username' => $username] );
}
/**
* 立即下单
* @ApiTitle (立即下单)
* @ApiParams (name="goods_id", type="int", description="商品id")
* @ApiParams (name="goods_sku_id", type="int", description="规格ID")
* @ApiParams (name="order_type", type="int", description="下单类型:self=自己下单,replace=待客户下单")
* @ApiParams (name="invoice_image", type="int", description="购车发票")
* @ApiParams (name="purchase_price", type="int", description="查购车价格")
* @ApiParams (name="mileage_image", type="int", description="表显里程图")
* @ApiParams (name="mileage", type="int", description="表显里程")
* @ApiParams (name="driving_license_image", type="int", description="行驶证")
* @ApiParams (name="vin", type="int", description="VIN码")
* @ApiParams (name="plate_number", type="int", description="车牌号")
* @ApiParams (name="brand", type="int", description="品牌型号")
* @ApiParams (name="engine_number", type="int", description="发动机型号")
* @ApiParams (name="register_time", type="int", description="注册日期")
* @ApiParams (name="issue_time", type="int", description="发证日期")
* @ApiParams (name="owner_name", type="int", description="车主姓名")
* @ApiParams (name="mobile", type="int", description="联系电话")
* @ApiParams (name="id_number", type="int", description="身份证号")
* @ApiParams (name="id_number_image", type="int", description="身份证图片")
* @ApiParams (name="exterior_images", type="int", description="车辆外观")
* @ApiParams (name="promotion_code", type="int", description="推广码")
* @ApiReturnParams (name="order_id", type="string", description="订单id")
*/
public function pay_order() {
$validate = new OrderValidate();
if ( !$validate->scene( 'pay' )->check( $this->param ) ) {
$this->error( $validate->getError() );
}
//查找商品
$goodsModel = new \app\api\model\goods\Goods();
$goods_info = $goodsModel->field( 'service_type,cart_type,contract_ids,service_claims,service_period,service_mileage' )
->where( ['id' => $this->param['goods_id'], 'goods_status' => '10'] )->find();
if ( empty( $goods_info ) ) {
$this->error( '您所购买的商品不存在或已下架' );
}
//查找商品规格
$skuModel = new GoodsSku();
$goods_sku = $skuModel->where( 'id', $this->param['goods_sku_id'] )->find();
if ( empty( $goods_sku ) ) {
$this->error( '您所选的规格不存在' );
}
//判断购车价格是否在规格区间
$purchase_price = $this->param['purchase_price'];
if ( $purchase_price < $goods_sku['minimum'] * 10000 || $purchase_price > $goods_sku['max'] * 10000 ) {
$this->error( '您的购车金额不在所选服务区间内' );
}
//判断客户权限
if ( $this->param['order_type'] == 'replace' && $this->auth->is_promoter == 2 ) {
$this->error( '您还不是推广员,不能代为下单' );
}
$promotion_code = isset( $this->param['promotion_code'] ) ? $this->param['promotion_code'] : '';
//判断推荐码
if ( !empty( $promotion_code ) ) {
$user = \app\api\model\User::get( ['promotion_no' => $promotion_code] );
if ( !$user ) {
$this->error( '您所填写的推广码不存在' );
}
//判断自己身份为推广员 填写了自己的推广码时 绑定自己的身份
if ( $user['id'] == $this->auth->id ) {
$this->error( '不能绑定自己的推广码!' );
}
//判断当前用户有没有上级,没有则绑定关系
if ( $this->auth->up_user_id == 0 ) {
$ups_user_id = db( 'user' )->where( 'promotion_no', $promotion_code )->value( 'up_user_id' );
if($ups_user_id) {
//递归查询所有上级并保存 后台展示,前台仅展示我的下一级的直系成员'is_last' => 1,
$up_arrays = $this->getParent($ups_user_id);
if($up_arrays) {
$ids = [];
foreach ($up_arrays as $value){
$ids[] = $value['id'];
}
if(in_array($this->auth->id,$ids)) {
$this->error('推广码用户或其下级中有人是您的下级,无法进行绑定');
}
}
}
Db::startTrans();
try {
//绑定上下级
$promoterModel = new PromoterLast();
$promoterModel->insert( [
'user_id' => $user['id'],
'last_user_id' => $this->auth->id,
'binding_time' => time(),
'createtime' => time()
] );
//↓2022-6-27查询被扫码者上级 :实现后台A->B->C 显示A->B、A->C、B->C
if ( $ups_user_id ) {
$up_arrays = $this->getParent($ups_user_id);
if ($up_arrays){
foreach ($up_arrays as $value){
$binding = $promoterModel->insert( [
'user_id' => $value['id'],
'last_user_id' => $this->auth->id,
'is_last' => 2,
'binding_time' => time(),
'createtime' => time()
] );
}
}
// //绑定上级的上级的下级
// $up_up_id = Db::name( 'user' )->where( 'id', $ups_user_id )->value( 'id' );
// $promoterModel->insert( [
// 'user_id' => $up_up_id,
// 'last_user_id' => $this->auth->id,
// 'is_last' => 2,
// 'binding_time' => time(),
// 'createtime' => time()
// ] );
}
//↑2022-6-27查询被扫码者上级 :实现后台A->B->C 显示A->B、A->C、B->C
\app\api\model\User::where( 'id', $this->auth->id )->update( [
'up_user_id' => $user['id'],
'binding_time' => time(),
] );
Db::commit();
} catch ( \Exception $e ) {
Db::rollBack();
$this->error( $e->getMessage() );
}
}
}
if ( count( $this->param['exterior_images'] ) != 2 ) {
$this->error( '车辆外观照片需两张' );
}
//开启事务
Db::startTrans();
//开始下单
$order_data = [
'user_id' => $this->auth->id,
'order_type' => $this->param['order_type'],
'order_sn' => getOrderNo( $this->auth->id ),
'status' => '0',
'goods_id' => $this->param['goods_id'],
'goods_sku_id' => $this->param['goods_sku_id'],
'goods_sku_text' => $goods_sku['level'] . $goods_sku['minimum'] . '-' . $goods_sku['max'] . '万元',
'goods_price' => $goods_sku['price'],
'pay_price' => $goods_sku['price'],
'service_type' => $goods_info['service_type'],
'cart_type' => $goods_info['cart_type'],
'contract_ids' => $goods_info['contract_ids'],
'service_claims' => $goods_info['service_claims'],
'service_period' => $goods_info['service_period'],
'service_mileage' => $goods_info['service_mileage'],
];
$orderModel = new \app\api\model\order\Order();
$order_return = $orderModel->save( $order_data );
//获取ID
$order_id = $orderModel->getLastInsID();
//记录订单表单信息
$item_data = [
'user_id' => $this->auth->id,
'order_id' => $order_id,
'invoice_image' => implode( ',', $this->param['invoice_image'] ),
'purchase_price' => $this->param['purchase_price'],
'mileage_image' => implode( ',', $this->param['mileage_image'] ),
'mileage' => $this->param['mileage'],
'driving_license_image' => implode( ',', $this->param['driving_license_image'] ),
'vin' => $this->param['vin'],
'plate_number' => $this->param['plate_number'],
'brand' => $this->param['brand'],
'engine_number' => $this->param['engine_number'],
'register_time' => strtotime( $this->param['register_time'] ),
'issue_time' => strtotime( $this->param['issue_time'] ),
'owner_name' => $this->param['owner_name'],
'mobile' => $this->param['mobile'],
'id_number' => $this->param['id_number'],
'id_number_image' => implode( ',', $this->param['id_number_image'] ),
'exterior_images' => implode( ',', $this->param['exterior_images'] ),
'promotion_code' => isset( $promotion_code ) ? $promotion_code : '',
];
$itemModel = new OrderItem();
$item_return = $itemModel->save( $item_data );
Db::commit();
if ( !$order_return || !$item_return ) {
Db::rollBack();
$this->error( '下单失败' );
}
$this->success( '下单成功', ['id' => $order_id] );
}
/**
* 递归查询所有上级
*
*/
public function getParent( $pid, $array = [] ) {
static $level = 1;
$is_parent = Db::name( 'user' )->field('id,up_user_id')->where( ['id' => $pid] )->find();
$array[] = $is_parent;
if ( $is_parent['up_user_id'] ) {
$level ++;
return $this->getParent( $is_parent['up_user_id'], $array );
}
return $array;
}
/**
* 调起支付
* @ApiTitle (调起支付)
* @ApiParams (name="order_id", type="string", description="订单id")
*/
public function payment() {
$order_id = $this->request->param( 'order_id' );
if ( empty( $order_id ) ) {
$this->error( '参数缺失' );
}
//查询用户open_id
$open_id = ( new Third() )->where( 'user_id', $this->auth->id )->value( 'openid' );
//确认订单->唤起微信支付信息->返回支付信息
$orderModel = new \app\api\model\order\Order();
$order_info = $orderModel->where( 'id', $order_id )->find();
//订单产品信息
$order_buy_info = Db::name('goods')->where('id',$order_info['goods_id'])->find();
//根据下单类型,判断支付人是否符合,
if ( $order_info['order_type'] == 'replace' ) {
//如果是代人下单,那么下单人与支付人不允许是同一个
if ( $order_info['user_id'] == $this->auth->id ) {
$this->error( '这是您代为下单,不能自己支付' );
}
//判断当前用户有没有上级,没有则绑定关系、代下单人是否为推广员
$is_promoter = Db::name( 'user' )->where( 'id', $order_info['user_id'] )->value( 'is_promoter' );
if ( $this->auth->up_user_id == 0 && $is_promoter == 1 ) {
//绑定上下级
$promoterModel = new PromoterLast();
$promoterModel->insert( [
'user_id' => $order_info['user_id'],
'last_user_id' => $this->auth->id,
'binding_time' => time(),
'createtime' => time()
] );
//↓2022-6-27查询被扫码者上级 :实现后台A->B->C 显示A->B、A->C、B->C
//判断是否有上上级
//代下单人为推广人,查询推广人的信息
// $up_user_info = Db::name( 'user' )->where( 'id', $order_info['user_id'] )->find();
// $ups_user_id = db( 'user' )->where( 'promotion_no', $up_user_info['promotion_no'] )->value( 'up_user_id' );
// if ( $ups_user_id ) {
// //绑定上级的上级的下级
// $up_up_id = Db::name( 'user' )->where( 'id', $ups_user_id )->value( 'id' );
// $promoterModel->insert( [
// 'user_id' => $up_up_id,
// 'last_user_id' => $this->auth->id,
// 'is_last' => 2,
// 'binding_time' => time(),
// 'createtime' => time()
// ] );
// }
//↑2022-6-27查询被扫码者上级 :实现后台A->B->C 显示A->B、A->C、B->C
\app\api\model\User::where( 'id', $this->auth->id )->update( [
'up_user_id' => $order_info['user_id'],
'binding_time' => time(),
] );
}
}
$payservice = new Service();
//提交预支付订单参数
$order_param = [
'amount' => $order_info['goods_price'],
// 'amount' => 0.01,
'orderid' => $order_info['order_sn'],
'type' => 'wechat', //微信
'title' => $order_buy_info['title'] . '-' . $order_info['goods_sku_text'],
'notifyurl' => $this->request->domain() . '/api/notify/service/type/wechat',
'returnurl' => '',
'method' => 'mp', //公众号
'openid' => $open_id,
];
$result = $payservice::submitOrder( $order_param );
if ( $result ) {
//更新支付用户id
$orderModel->where( 'id', $order_id )->update( ['pay_user_id' => $this->auth->id] );
Db::commit();
$this->success( '支付信息', ['payDetail' => $result, 'order_id' => $order_info['id']] );
} else {
Db::rollback();
$this->error( '支付参数错误' );
}
}
}