Cars.php
5.0 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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\Car;
use think\Db;
use think\Validate;
/**
* 购物车接口**
*/
class Cars extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $user_id = '';//token存贮user_id
protected $normal = '';//商品正常状态,1:下架
public function _initialize()
{
parent::_initialize();
$this->normal = config('site.goods_status');
$this->user_id = $this->auth->getUserId();
}
/**
* @ApiTitle (加入购物车)
* @ApiSummary (加入购物车)
* @ApiMethod (GET)
* @ApiRoute (/api/cars/addCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="商品id")
* @ApiReturn({
"code": 1,
"msg": "加入成功",
"time": "1553836646",
"data": null
})
*/
public function addCar(){
if($this->request->isGet()){
$goods_id = $this->request->get('id');//商品id
$rule = config('site.goods');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$goods_id])) {
$this->error($validate->getError());
}
$data = ['uid'=>$this->user_id,'p_id'=>$goods_id];
$car = new Car();
$cars = Db::table('gc_car')
->field('id,num')
->where($data)
->find();
if($cars){
$data['num'] = $cars['num'] + 1;
$res = $car->where(['id'=>$cars['id']])->update($data);
}else{
$res = $car::create($data);
}
if($res){
$this->success('加入成功');
}else{
$this->error('加入失败');
}
}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": "1553836697",
"data": [
{
"id": 1,
"p_id": 1,//商品id
"title": "我的垃圾商品1",//商品名称
"price": 500,//商品价格
"num": 3,/商品数量
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
},
{
"id": 2,
"p_id": 2,
"title": "废弃家电",
"price": 1200.08,
"num": 3,
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
}
]
})
*/
public function carList(){
if($this->request->isGet()){
$data = Db::table('gc_car')
->alias('c')
->join('gc_product p','c.p_id = p.id','LEFT')
->join('gc_user u','p.uid = u.id','LEFT')
->where(['c.uid'=>$this->user_id,'p.status'=>$this->normal,'u.status'=>'normal'])
->field('c.id,c.p_id,p.title,p.price,c.num,p.images')
->select();
$this->success('成功',$data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (购物车列表)
* @ApiSummary (购物车列表)
* @ApiMethod (GET)
* @ApiRoute (/api/cars/clearCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="string", required=true, description="购物车id(多个以逗号隔开)")
* @ApiReturn({
"code": 1,
"msg": "删除成功",
"time": "1555504723",
"data": 2
})
*/
public function clearCar(){
if($this->request->isGet()){
$c_id = $this->request->get('id');//购物车id,以逗号隔开字符(“1,2,3”)
$rule = config('site.cars');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$c_id])) {
$this->error($validate->getError());
}
$c_ids = explode(',',$c_id);
$data = Db::table('gc_car')
->whereIn('id',$c_ids)
->where(['uid'=>$this->user_id])
->delete();
if($data){
$this->success('删除成功',$data);
}else{
$this->error('删除失败');
}
}else{
$this->error('请求方式错误');
}
}
}