Car.php
9.1 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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Validate;
use think\Config;
/**
* 购物车接口
*/
class Car extends Api
{
protected $carModel;
protected function _initialize()
{
parent::_initialize();
$this->carModel = new \app\api\model\Car;
}
/**
* @ApiTitle (获取购物车数量)
* @ApiSummary (获取购物车数量)
* @ApiMethod (POST)
* @ApiRoute (/api/car/getCarNumber)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1587610459",
"data": 1 购物车数量
})
*/
public function getCarNumber(){
$userId = $this->getUserId();
$where['user_id'] = $userId;
$data = $this->carModel->where($where)->count();
$this->success('SUCCESS',$data);
}
/**
* @ApiTitle (获取购物车列表)
* @ApiSummary (获取购物车列表)
* @ApiMethod (POST)
* @ApiRoute (/api/car/getCarList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=page, type=string, required=false, description="页数")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1587610459",
"data": {
"total": 1,
"list": [
{
"createtime": "1970-01-01",
"ch_country_name": "丹麦", 原产地中文名称
"en_country_name": "Denmark", 原产地英文名称
"number": 3, 数量
"goods_id": 3, 商品id
"ch_name": "猪肉", 中文名称
"en_name": "EMP selected", 英文名称
"image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 缩略图
"goods_price": "60.00", 原价
"group_price": 30, 拼团价格
"is_group": "2" 团购:1=开启,2=关闭
}
]
}
})
*/
public function getCarList(){
$userId = $this->getUserId();
$where['c.user_id'] = $userId;
$page = $this->request->param('page');
$limit = Config::get('paginate.index_rows');
$data = $this->carModel->selectPageData($where,$page,$limit,$this->lang);
foreach ($data['list'] as $k => $v){
$v['user_type'] = $this->user['type'];
$data['list'][$k]['price'] = get_price_not_group($v);
}
$this->success('SUCCESS',$data);
}
/**
* @ApiTitle (获取购物车金额)
* @ApiSummary (获取购物车金额,购物车递增递减后调用此接口刷新金额)
* @ApiMethod (POST)
* @ApiRoute (/api/car/getCarMoney)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=car_id, type=string, required=false, description="购物车id 例 1,2,3")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1587610459",
"data": {
"price": 316, 折扣后价格
"discount_price": 50, 折扣金额
"original_price": 366 原价
}
})
*/
public function getCarMoney(){
$userId = $this->getUserId();
$where['c.user_id'] = $userId;
$carid = $this->request->param('car_id');
$priceArr = $this->carModel->getCarMoney(['id'=>['in',$carid]]);
$price = 0; //折扣后价格
$discount_price = 0; //折扣金额
$original_price = 0; //原价
$data['integral'] = 0; //积分
foreach ($priceArr as $k=>$v){
$priceArr[$k]['user_type'] = $this->user['type'];
$price += get_price($v);
$discount_price += get_discount_price($v);
$original_price += $v['goods_price'];
if (!empty($v['integral'])) $data['integral'] += $v['integral'];
}
$data['price'] = round($price,2);
$data['discount_price'] = round($discount_price,2);
$data['original_price'] = $original_price;
$this->success('SUCCESS',$data);
}
/**
* @ApiTitle (添加购物车)
* @ApiSummary (添加购物车)
* @ApiMethod (POST)
* @ApiRoute (/api/car/addCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=goods_id, type=string, required=true, description="商品id")
* @ApiParams (name=number, type=string, required=true, description="数量")
* @ApiParams (name=type, type=string, required=true, description="类型:1=普通商品,2=积分商品")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1587472510",
"data": {
"total": 1,
"list": [
{
"content": "content", 评论内容
"images": [
"http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png" 评论图片
],
"createtime": "2020-04-20", 评论日期
"avatar": "avatar", 用户头像
"nickname": "admin" 用户昵称
}
]
}
})
*/
public function addCar(){
$userId = $this->getUserId();
$data = $this->request->param();
$data['user_id'] = $userId;
$validate = new Validate([
'goods_id' => 'require',
'type' => 'require',
'number' => 'require',
]);
$validate->message([
'goods_id' => '缺少参数 goods_id!',
'type' => '缺少参数 type!',
'number' => '缺少参数 number!',
]);
if (!$validate->check($data)) {
$this->error($validate->getError());
}
// if($data['type'] == 1){
// $is_group = Db::name('goods')->where(['id'=>$data['goods_id'],'is_group'=>1])->count();
// if ($is_group) $this->error('团购商品不可以加入购物车');
// }
$stock_num = Db::name('goods')->where(['id'=>$data['goods_id']])->value('stock_num');
if (!$stock_num) $this->error('库存不足');
$last = $this->carModel->where(['user_id'=>$userId,'goods_id'=>$data['goods_id'],'type'=>$data['type']])->value('id');
if ($last) $res = $this->carModel->where(['id'=>$last])->setInc('number',1);
else $res = $this->carModel->save($data);
if ($res) $this->success('SUCCESS');
else $this->error('ERROR');
}
/**
* @ApiTitle (购物车数量递增)
* @ApiSummary (购物车数量递增)
* @ApiMethod (POST)
* @ApiRoute (/api/car/setIncCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=car_id, type=string, required=true, description="购物车id")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1587472510",
"data": {
}
})
*/
public function setIncCar(){
$userId = $this->getUserId();
$car_id = $this->request->param('car_id');
if (empty($car_id)) $this->error('缺少参数 car_id!');
$where['user_id'] = $userId;
$where['id'] = $car_id;
$carInfo = $this->carModel->where('id',$car_id)->find();
$stock_num = Db::name('goods')->where(['id'=>$carInfo['goods_id']])->value('stock_num');
if ($carInfo['number']+1>$stock_num) $this->error('库存不足');
$res = $this->carModel->where($where)->setInc('number',1);
if ($res) $this->success('SUCCESS');
else $this->error('ERROR');
}
/**
* @ApiTitle (购物车数量递减)
* @ApiSummary (购物车数量递减)
* @ApiMethod (POST)
* @ApiRoute (/api/car/setDecCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=car_id, type=string, required=true, description="购物车id")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1587472510",
"data": {
}
})
*/
public function setDecCar(){
$userId = $this->getUserId();
$car_id = $this->request->param('car_id');
if (empty($car_id)) $this->error('缺少参数 car_id!');
$where['user_id'] = $userId;
$where['id'] = $car_id;
$carInfo = $this->carModel->where('id',$car_id)->find();
if ($carInfo['number'] == 1) $this->error('不可以再减了');
$res = $this->carModel->where($where)->setDec('number',1);
if ($res) $this->success('SUCCESS');
else $this->error('ERROR');
}
/**
* @ApiTitle (删除购物车)
* @ApiSummary (删除购物车)
* @ApiMethod (POST)
* @ApiRoute (/api/car/delCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=car_id, type=string, required=true, description="购物车id 批量1,2,3")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1587472510",
"data": {
}
})
*/
public function delCar(){
$userId = $this->getUserId();
$car_id = $this->request->param('car_id');
if (empty($car_id)) $this->error('缺少参数 car_id!');
$res = $this->carModel->where(['id'=>['in',$car_id]])->delete();
if ($res) $this->success('SUCCESS');
else $this->error('ERROR');
}
}