Cars.php
22.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
namespace app\api\controller;
use app\admin\model\Car;
use app\common\controller\Api;
use think\Validate;
use think\Db;
/**
* 购物车接口
*/
class Cars extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
protected $uid = '';
public function _initialize()
{
parent::_initialize();
$this->uid = $this->auth->getUserId();
}
/**
* @ApiTitle (加入购物车)
* @ApiSummary (加入购物车)
* @ApiMethod (POST)
* @ApiRoute (/api/cars/addCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="goods_id", type="inter", required=true, description="商品id")
* @ApiParams (name="style", type="string", required=true, description="商品款式")
* @ApiParams (name="price", type="inter", required=true, description="商品价格(新人价格或销售价)")
* @ApiParams (name="goods_number", type="inter", required=true, description="商品数量")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575103466",
"data": null
})
*/
public function addCar(){
if($this->request->isPost()){
$data = $this->request->post();
$rule = config('verify.add_car');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check($data)) {
$this->error($validate->getError());
}
//检测库存
$stock = Common::findSoftWhereData('goods',['id'=>$data['goods_id']],'id,stock');
if($stock){
if(($stock['stock'] <=> $data['goods_number']) == -1){
$this->error('库存不足');
}
}
//查询有无记录
$where = [
'uid'=>$this->uid,
'g_id'=>$data['goods_id'],
'style'=>trim($data['style']),
];
$res = Common::findWhereData('car',$where,'id,g_id');
$carModel = new Car();
if($res){
$carModel->where($where)->setInc('goods_number',$data['goods_number']);
}else{
$data['g_id'] = $data['goods_id'];
$data['uid'] = $this->uid;
unset($data['goods_id']);
$carModel->create($data);
}
$this->success('成功');
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (购物车列表)
* @ApiSummary (购物车列表)
* @ApiMethod (GET)
* @ApiRoute (/api/cars/carList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575102913",
"data": {
"data": [
{
"id": 1,//购物车id
"image": "http://jinglong.springchunjia.cn/uploads/20191127/287ca016a4d41a239ec77c91d982309d.png",//商品图片
"price": 100,//商品价格
"style": "",//商品款式
"goods_id": 1,//商品id
"goods_number": 2,//商品数量
"name": "MONENT 动感系列动感系列动感系列",//商品名称
"introduce": "轻波款,为客厅缀上霞光淡雾"//商品简介
"stock": "12"//商品库存
},
{
"id": 2,
"price": 100,
"goods_id": 2,
"goods_number": 1,
"name": "MONENT 动感系列动感系列",
"introduce": "轻波款,为客厅缀上霞光淡雾"
}
],
"total_price": 300
}
})
*/
public function carList(){
if($this->request->isGet()){
$res = Db::name('car')
->alias('c')
->join('goods g','c.g_id = g.id','LEFT')
->where(['c.uid'=>$this->uid])
->field('c.id,c.g_id goods_id,c.price,c.goods_number,c.style,g.name,g.image,g.stock,g.introduce')
->useSoftDelete('g.deletetime')
->select();
$total_price = 0;
foreach ($res as &$value){
$value['image'] = $this->auth->absolutionUrlOne($value['image']);
$total_price += $value['price'] * $value['goods_number'];
if(($value['stock'] <=> $value['goods_number']) == -1){
//购物车数量大于库存
$value['goods_number'] = $value['stock'];
}
}
// $total_price = array_sum(array_map(function ($product_row) {
// return $product_row['price'] * $product_row['goods_number'];
// }, $res));
$arr['data'] = $res;
$arr['total_price'] = $total_price;
$this->success('成功',$arr);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (删除购物车)
* @ApiSummary (删除购物车)
* @ApiMethod (GET)
* @ApiRoute (/api/cars/deleteCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="car_id", type="inter", required=true, description="购物车id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575103466",
"data": null
})
*/
public function deleteCar(){
if($this->request->isGet()){
$car_id = $this->request->get('car_id');
$rule = config('verify.delete_car');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['car_id'=>$car_id])) {
$this->error($validate->getError());
}
$carModel = new Car();
$res = $carModel->where(['id'=>$car_id,'uid'=>$this->uid])->delete();
if($res){
$this->success('成功');
}else{
$this->error('失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (猜你喜欢商品列表)
* @ApiSummary (猜你喜欢商品列表)
* @ApiMethod (GET)
* @ApiRoute (/api/cars/likeGodsList)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="page", type="inter", required=true, description="分页页码")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1574941706",
"data": {
"data": [
{
"id": 7,//商品id
"image": "http://jinglong.springchunjia.cn/uploads/20191128/8a677f5a0418059bf1b974c50026af13.png",//图片路径
"name": "MONENT 动感系列",//商品名称
"tag": [//商品标签
"日式简约",
"隐秘乡奢",
"家庭情侣"
],
"style": [//商品规格
"主餐匙,茶匙各1件",
"古堡灰"
],
"sale_price": 2299//销售价格
"expense_price": //运费(0:显示包运费标签)
"is_new_tag": 0//新人价格标签(0:不显示,1:显示)
},
{
"id": 4,
"image": "http://jinglong.springchunjia.cn/uploads/20191128/93971e55b83d1a09c1831f8197514305.png",
"name": "MONENT 动感系列",
"tag": [
"AB级",
"ABX级",
"ABN级"
],
"new_price": 2499,
"sale_price": 2599
},
],
"total_page": 1
}
})
*/
public function likeGodsList(){
if($this->request->isGet()){
$page = $this->request->get('page');
$rule = config('verify.page');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['page'=>$page])) {
$this->error($validate->getError());
}
$arr = Common::goodsList('',$page,$this->uid,'hots desc,id desc');
$this->success('成功',$arr);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (商品结算)
* @ApiSummary (商品结算)
* @ApiMethod (POST)
* @ApiRoute (/api/cars/settleGoods)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="goods_id", type="string", required=true, description="商品id(多个以逗号隔开,如:1,3,4)")
* @ApiParams (name="price", type="string", required=true, description="商品价格(多个以逗号隔开,如:100,10,200)")
* @ApiParams (name="goods_number", type="string", required=true, description="商品数量(多个以逗号隔开,如:10,10,10)")
* @ApiParams (name="style", type="string", required=true, description="商品款式(多个以逗号隔开,如:‘款式1,款式2,款式3’)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575106933",
"data": {
"data": [
{
"id": 2,//商品id
"name": "MONENT 动感系列动感系列",//商品名称
"image": "http://jinglong.springchunjia.cn/uploads/20191128/8a677f5a0418059bf1b974c50026af13.png",//商品图片路径
"introduce": "轻波款,为客厅缀上霞光淡雾",//商品简介
"price": "250",//商品价格
"goods_number": "5"//商品数量
"style": "款式1"//商品款式
},
{
"id": 1,
"name": "MONENT 动感系列动感系列动感系列",
"image": "http://jinglong.springchunjia.cn/uploads/20191128/93971e55b83d1a09c1831f8197514305.png",
"introduce": "轻波款,为客厅缀上霞光淡雾",
"price": "100",
"goods_number": "3"
}
],
"total_goods_price": 1550,//商品总金额
"total_expense_price": 10,//运费总金额
"total_price": 1560//总金额(商品总金额+运费总金额)
}
})
*/
public function settleGoods(){
if($this->request->isPost()){
$goods_id = $this->request->post('goods_id');
$price = $this->request->post('price');
$goods_number = $this->request->post('goods_number');
$style = $this->request->post('style');
$rule = config('verify.settle_goods');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['goods_id'=>$goods_id,'price'=>$price,'goods_number'=>$goods_number,'style'=>$style])) {
$this->error($validate->getError());
}
$goods_id_s = explode(',',$goods_id);
$price_s = explode(',',$price);
$goods_number_s = explode(',',$goods_number);
$style_s = explode(',',$style);
//合并数组
$key = ['goods_id','price','goods_number','style'];
$res_goods = Common::array_merge_more($key,$goods_id_s,$price_s,$goods_number_s,$style_s);
//查询商品
$res = Common::selectSoftWhereData('goods',['id'=>['in',$goods_id_s]],'id,name,image,expense_price,stock,introduce');
$total_price = 0;//总商品费用(不含运费)
$total_expense_price = 0;//总运费
foreach ($res as &$value){
$value['image'] = $this->auth->absolutionUrlOne($value['image']);
foreach($res_goods as $g_value){
if($value['id'] == $g_value['goods_id']){
$value['price'] = $g_value['price'];//商品价格
if(($value['stock'] <=> $g_value['goods_number']) == -1){
$this->error('商品名称为'.$value['name'].'的库存不足');
}
$value['goods_number'] = $g_value['goods_number'];//商品数量
$value['style'] = $g_value['style'];//商品款式
$total_price += $g_value['price'] * $g_value['goods_number'];
break;
}
}
$total_expense_price += $value['expense_price'];
unset($value['expense_price']);
}
$arr['data'] = $res;
$arr['total_goods_price'] = $total_price;
$arr['total_expense_price'] = $total_expense_price;
$arr['total_price'] = $total_price + $total_expense_price;
$this->success('成功',$arr);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (结算可使用优惠券)
* @ApiSummary (结算可使用优惠券)
* @ApiMethod (GET)
* @ApiRoute (/api/cars/settleUseCoupon)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="goods_id", type="string", required=true, description="商品id(多个以逗号隔开,如:1,3,4)")
* @ApiParams (name="total_goods_price", type="inter", required=true, description="商品总金额")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575372162",
"data": [
{
"id": 2,//优惠券id
"coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
"coupon_price": "¥300",//(折扣或减少金额)
"coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
"coupon_name": "全场优惠券",//优惠券名称
"end_time": "2020.1.31",//优惠券有效期
"type": "全场通用"//优惠券用途
"is_discount": "0"//是否为折扣(0:否,1:是)
"price_tag": 100 //折扣或者减少金额
},
{
"id": 10,
"coupon_tag": "折扣券",
"coupon_price": "9.5折",
"coupon_tag1": "满2000可用",
"coupon_name": "商品优惠券",
"end_time": "2020.1.31",
"type": "商品可用"
}
]
})
*/
public function settleUseCoupon(){
if($this->request->isGet()){
$goods_id = $this->request->get('goods_id');
$total_goods_price = $this->request->get('total_goods_price');
$rule = config('verify.settle_use_coupon');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['goods_id'=>$goods_id,'total_goods_price'=>$total_goods_price])) {
$this->error($validate->getError());
}
//查询已经领取过,未使用
$flag = config('verify.flag');
$type = config('verify.type');
$receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid,'is_use'=>$flag[0]],'id,c_id');
$receive_s = array_column($receive,'c_id');
$goods_id_s = explode(',',$goods_id);
//查询商品所属品牌id
$res1 = Common::selectSoftWhereData('goods',['id'=>['in',$goods_id_s],'type'=>$flag[1]],'id,t_id');
$b_id_s = array_unique(array_values(array_column($res1,'t_id')));
$time = time();
$where['id'] = ['in',$receive_s];
$where['end_time'] = ['>',$time];
$data = Common::selectWhereData('coupon',$where,'id,type,coupon_name,c_type,bg_id,coupon_type,full_reduce,reduce,discount,coupon_number,end_time','sort desc,id desc');
$res = [];
$key = 0;
foreach($data as $value){
$key+=0;
$flag = 0;
if(!empty($b_id_s)){
//有品牌,看商品所属品牌
if($value['type'] == $type[0]){
$flag = 1;
}else{
if($value['type'] == $type[1] && in_array($value['bg_id'],$b_id_s)){
//去除不是该商品的
$flag = 1;
}
}
}else{
//无品牌
if($value['type'] == $type[0]){//是全场券
$flag = 1;
}else{
if($value['type'] == $type[2] && in_array($value['bg_id'],$goods_id_s)){
//去除不是该商品的
$flag = 1;
}
}
}
if($flag == 1){
//无门槛,有门槛
if($value['c_type'] == 0){
$res[$key]['id'] = $value['id'];
//无门槛
if($value['coupon_type'] == 0){
//减少券,去掉满减金额,折扣字段,
unset($value['full_reduce']);
unset($value['discount']);
$res[$key]['coupon_tag'] = '无门槛';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
$res[$key]['coupon_tag1'] = '无门槛';
$res[$key]['is_discount'] = 0;//是否为折扣(0:否,1:是)
$res[$key]['price_tag'] = $value['reduce'];
}else{
//折扣券,去掉满减金额,减少金额字段,
unset($value['full_reduce']);
unset($value['reduce']);
$res[$key]['coupon_tag'] = '折扣券';
$res[$key]['coupon_price'] = $value['discount'].'折';
$res[$key]['coupon_tag1'] = '无门槛';
$res[$key]['is_discount'] = 1;//是否为折扣(0:否,1:是)
$res[$key]['price_tag'] = $value['discount'];
}
$res[$key]['coupon_name'] = $value['coupon_name'];//优惠券名称
$res[$key]['end_time'] = date('Y.n.j',$value['end_time']);//优惠券有效期
//全场,品牌,商品
if($value['type'] == 0){
$res[$key]['type'] = '全场通用';
}else if($value['type'] == 1){
$res[$key]['type'] = '部分品牌可用';
}else{
$res[$key]['type'] = '部分商品可用';
}
$key++;
}else{
//有门槛
if(($total_goods_price <=> $value['full_reduce']) >= 0){
$res[$key]['id'] = $value['id'];
//商品总金额大于等于满减金额
if($value['coupon_type'] == 0){
//减少券,去掉折扣字段,
unset($value['discount']);
$res[$key]['coupon_tag'] = '满减券';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
$res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
$res[$key]['is_discount'] = 0;//是否为折扣(0:否,1:是)
$res[$key]['price_tag'] = $value['reduce'];
}else{
//折扣券,去掉减少金额字段,
unset($value['reduce']);
$res[$key]['coupon_tag'] = '折扣券';
$res[$key]['coupon_price'] = $value['discount'].'折';
$res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
$res[$key]['is_discount'] = 1;//是否为折扣(0:否,1:是)
$res[$key]['price_tag'] = $value['discount'];
}
$res[$key]['coupon_name'] = $value['coupon_name'];//优惠券名称
$res[$key]['end_time'] = date('Y.n.j',$value['end_time']);//优惠券有效期
//全场,品牌,商品
if($value['type'] == 0){
$res[$key]['type'] = '全场通用';
}else if($value['type'] == 1){
$res[$key]['type'] = '部分品牌可用';
}else{
$res[$key]['type'] = '部分商品可用';
}
$key++;
}
}
}
}
$this->success('成功',$res);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (获取最近订单的地址)
* @ApiSummary (获取最近订单的地址)
* @ApiMethod (GET)
* @ApiRoute (/api/cars/getOrderAddress)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575462785",
"data": {
"id": 2,
"receive_name": "景龙1",
"receive_mobile": "13752011725",
"receive_address": "天津市南开区1"
}
})
*/
public function getOrderAddress(){
if($this->request->isGet()){
$res = Common::findSoftWhereData('order',['uid'=>$this->uid],'id,receive_name,receive_mobile,receive_address');
$this->success('成功',$res);
}else{
$this->error('请求方式错误');
}
}
}