Goods.php
13.3 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
<?php
namespace app\api\controller;
use app\admin\model\Browse;
use app\common\controller\Api;
use app\admin\model\Search;
use think\Db;
use think\Validate;
/**
* 商品接口**
*/
class Goods extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $user_id = '';//token存贮user_id
protected $normal = '';//商品正常状态,1:下架
protected $is_search = [0,1];//检索到数据 0:是 1:否
public function _initialize()
{
parent::_initialize();
$this->normal = config('site.goods_status');
$this->user_id = $this->auth->getUserId();
}
/**
* @ApiTitle (对应分类的商品列表)
* @ApiSummary (对应分类的商品列表)
* @ApiMethod (GET)
* @ApiRoute (/api/goods/getGoodsList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="分类id")
* @ApiParams (name="page", type="integer", required=true, description="分页页码")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1553831462",
"data": [
{
"id": 1,
"type": 0,//类型 0:个人 1:企业
"title": "我的垃圾商品1",//商品名称
"address": "河南省邓州市",//上门地址
"pre_time": "2019-03-19 14:36:08",//预约时间
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
},
{
"id": 3,
"type": 0,
"title": "废品商品2",
"address": "河南省邓州市",
"pre_time": "2019-03-19 19:14:07",
"images": "/uploads/20190328/c7eab178376c71ae1892cd23a1fb5779.jpg"
}
]
})
*/
public function getGoodsList(){
if($this->request->isGet()){
$category_id = $this->request->get('id');//分类id
$page = $this->request->get('page');//分页页码
$limit = config('site.page_limit');//分页限制数量
$rule = config('site.pages');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$category_id,'page'=>$page])) {
$this->error($validate->getError());
}
$data = Db::table('gc_product')
->alias('p')
->join('gc_user u','p.uid = u.id','LEFT')
->where(['p.category_id'=>$category_id,'p.status'=>$this->normal,'u.status'=>'normal'])
->page($page,$limit)
->field('p.id,u.type,p.title,u.address,p.pre_time,p.images')
->order('p.id desc')
->select();
foreach($data as &$value){
$value['pre_time'] = date('Y-m-d H:i:s',$value['pre_time']);
}
$this->success('成功', $data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (搜索对应分类的关键字商品列表)
* @ApiSummary (搜索对应分类的关键字商品列表)
* @ApiMethod (GET)
* @ApiRoute (/api/goods/searchKey)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="分类id")
* @ApiParams (name="keywords", type="string", required=true, description="关键字")
* @ApiParams (name="page", type="integer", required=true, description="分页页码")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1553831605",
"data": [
{
"id": 1,
"type": 0,//类型 0:个人 1:企业
"title": "我的垃圾商品1",//商品名称
"address": "河南省邓州市",//上门地址
"pre_time": "2019-03-19 14:36:08",//预约时间
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
}
]
})
*/
public function searchKey(){
if($this->request->isGet()){
$category_id = $this->request->get('id');//分类id
$rule = config('site.keys');
$validate = new Validate($rule['rule'],$rule['msg']);
$keywords = $this->request->get('keywords');//关键字
$page = $this->request->get('page');//分页页码
$limit = config('site.page_limit');//分页限制数量
if (!$validate->check(['id'=>$category_id,'keywords'=>$keywords,'page'=>$page])) {
$this->error($validate->getError());
}
$data = Db::table('gc_product')
->alias('p')
->join('gc_user u','p.uid = u.id','LEFT')
->where('p.title','like','%'.$keywords.'%')
->where(['p.category_id'=>$category_id,'p.status'=>$this->normal,'u.status'=>'normal'])
->page($page,$limit)
->field('p.id,u.type,p.title,u.address,p.pre_time,p.images')
->select();
foreach($data as &$value){
$value['pre_time'] = date('Y-m-d H:i:s',$value['pre_time']);
}
$search = new Search();
$condition = ['uid'=>$this->user_id,'keywords'=>$keywords];
//未检索到数据
if(!$data){
$condition['is_search'] = $this->is_search[1];
}
$is_keywords = Db::table('gc_search')
->where($condition)
->select();
//判断是否有相同的检索历史
if(!$is_keywords){
$search::create($condition);
}
$this->success('成功', $data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (搜索历史记录)
* @ApiSummary (搜索历史记录)
* @ApiMethod (GET)
* @ApiRoute (/api/goods/keysHistory)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1553831921",
"data": [
{
"id": 4,
"keywords": "我的世界"//关键字
},
]
})
*/
public function keysHistory(){
if($this->request->isGet()){
$data = Db::table('gc_search')
->where(['uid'=>$this->user_id])
->field('id,keywords')
->order('id desc')
->select();
$this->success('成功', $data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (清空历史记录)
* @ApiSummary (清空历史记录)
* @ApiMethod (GET)
* @ApiRoute (/api/goods/clearHistory)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1553831995",
"data": 4
})
*/
public function clearHistory(){
if($this->request->isGet()){
$data = Db::table('gc_search')
->where(['uid'=>$this->user_id])
->delete();
$this->success('成功', $data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (商品详情)
* @ApiSummary (商品详情)
* @ApiMethod (GET)
* @ApiRoute (/api/goods/goodsDetail)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="商品id")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1553832106",
"data": [
"data":[{
"id": 1,
"type": 0, //类型 0:个人 1:企业
"title": "我的垃圾商品1", //商品名称
"mobile": 13752001725, //联系电话
"price": 500, //商品价格
"stock": 1000, //商品库存
"address": "天津市南开区卫津路", //上门地址
"pre_time": "2019-03-19 14:36:08", //预约时间
"introduction" : "商品描述",
"description" : "商品详情",
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
}],
"comment_count":"5"//商品评价条数
]
})
*/
public function goodsDetail(){
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());
}
$comment_count = Db::table('gc_comment')
->where(['uid'=>$this->user_id,'p_id'=>$goods_id])
->count();
$data = Db::table('gc_product')
->alias('p')
->join('gc_user u','p.uid = u.id','LEFT')
->where(['p.id'=>$goods_id,'p.status'=>$this->normal,'u.status'=>'normal'])
->field('p.id,u.type,u.mobile,p.title,p.price,p.stock,u.address,p.pre_time,p.introduction,p.description,p.images')
->select();
foreach($data as &$value){
$value['pre_time'] = date('Y-m-d H:i:s',$value['pre_time']);
}
$this->broGoods($goods_id);
$this->success('成功', ['data'=>$data,'comment_count'=>$comment_count]);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (猜你喜欢)
* @ApiSummary (猜你喜欢)
* @ApiMethod (GET)
* @ApiRoute (/api/goods/guessLikes)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1553832190",
"data": [
{
"id": 1,
"title": "我的垃圾商品1", //商品名称
"price": 500, //商品价格
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
},
{
"id": 2,
"title": "废弃家电",
"price": 1200.08,
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
},
{
"id": 3,
"title": "废品商品2",
"price": 12.05,
"images": "/uploads/20190328/c7eab178376c71ae1892cd23a1fb5779.jpg"
},
]
})
*/
public function guessLikes(){
if($this->request->isGet()){
//随机取出1条检索的历史
$bro_num = Db::table('gc_browse')
->where(['uid'=>$this->user_id])
->order('bro_num desc')
->select();
$where['p.status'] = $this->normal;
$where['u.status'] = 'normal';
if($bro_num){
$g_Ids = array_column($bro_num,'g_id');
$data = Db::table('gc_product')
->alias('p')
->join('gc_user u','p.uid = u.id','LEFT')
->where($where)
->whereIn('p.id',$g_Ids)
->field('p.id,p.title,p.price,p.images')
->limit(20)
->order('p.id desc')
->select();
}else{
$data = Db::table('gc_product')
->alias('p')
->join('gc_user u','p.uid = u.id','LEFT')
->where($where)
->field('p.id,p.title,p.price,p.images')
->limit(20)
->order('p.id desc')
->select();
}
$this->success('成功', $data);
}else{
$this->error('请求方式错误');
}
}
//浏览记录表
private function broGoods($id){
$broModel = new Browse();
$bro_num = Db::table('gc_browse')
->where(['uid'=>$this->user_id,'g_id'=>$id])
->find();
$data['g_id'] = $id;
$data['uid'] = $this->user_id;
if($bro_num){
$data['bro_num'] = $bro_num['bro_num']+1;
$broModel->where(['g_id'=>$id,'uid'=>$this->user_id])->update($data);
}else{
$data['bro_num'] = 1;
$broModel::create($data);
}
}
}