Index.php
14.6 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
<?php
namespace app\api\controller;
use function Couchbase\defaultDecoder;
/**
* 首页相关
* @package app\api\controller
*/
class Index extends BaseApi
{
protected $noNeedLogin = ['search_hot'];
protected $noNeedRight = '*';
/**
* 获取首页动态
* @ApiTitle (获取首页动态)
* @ApiMethod (POST)
* @ApiRoute (/api/index/articleList)
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiParams (name="status", type="integer", required=true, description="1=推荐,2=关注")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询数据成功",
"time": "1608857680",
"data": [
{
"id": 9,
"user_id": 1,
"article_category_id": 7,
"content": "内",
"images": [
"http://qldk2g57y.hn-bkt.clouddn.com/uploads/20201221/667431fe2f31e5d0e76344ba7545ea20.jpg"
],
"createtime": "2020-12-24 15:13:48",
"updatetime": "2020-12-24 15:13:48",
"deletetime": null,
"message": null,
"is_friend": "是否是好友 1=是 0=不是",
"str_time": "首页显示时间"
}
]
})
*/
public function articleList(){
//1.获取数据
$data = $this->get_data_array([
['page','分页次数不能为空'],
['num','分页数量不能为空'],
['status','状态必须填写'],
]);
//2.获取列表数据
if ($data['status'] == 1){
//推荐
$art_list = model('article')
->where('deletetime',null)
->with(['user'])
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
}else if ($data['status'] == 2){
//查询关注的user_id
$user_ids = model('user_attention')->where('user_id',$this->auth->id)->column('in_user_id');
//查询动态
$art_list = model('article')
->where('deletetime',null)
->with(['user'])
->whereIn('user_id',$user_ids)
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
}
//3.判断是不是好友
$user_id = $this->auth->id;
if (!empty($art_list)){
foreach ($art_list as $key => $val){
//1.查询关注和被关注
$attention = model('user_attention')->where(function ($query) use ($val,$user_id){
$query->where('user_id',$user_id)->where('in_user_id',$val['user_id']);
})->whereOr(function ($query) use ($val,$user_id){
$query->where('user_id',$val['user_id'])->where('in_user_id',$user_id);
})->count();
if ($attention == 2){
$art_list[$key]['is_friend'] = 1;
}else{
$art_list[$key]['is_friend'] = 0;
}
}
}
$this->success('查询数据成功',$art_list);
}
/**
* 获取动态栏目列表
* @ApiTitle (获取动态栏目列表)
* @ApiMethod (POST)
* @ApiRoute (/api/index/articleCategoryList)
* @ApiParams (name="num", type="integer", required=false, description="请求数量 传为所有")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ()
*/
public function articleCategoryList(){
$num = input('param.num');
if ($num){
$articleCategoryList = model('article_category')->order('weigh','desc')->limit($num)->select();
}else{
$articleCategoryList = model('article_category')->order('weigh','desc')->select();
}
$this->success('查询成功',$articleCategoryList);
}
/**
* 首页分类列表+搜索
* @ApiTitle (首页分类列表+搜索)
* @ApiMethod (POST)
* @ApiRoute (/api/index/CategoryList)
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiParams (name="article_category_id", type="integer", required=true, description="动态栏目id")
* @ApiParams (name="search", type="string", required=false, description="搜索内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询数据成功",
"time": "1608857680",
"data": [
{
"id": 9,
"user_id": 1,
"article_category_id": 7,
"content": "内",
"images": [
"http://qldk2g57y.hn-bkt.clouddn.com/uploads/20201221/667431fe2f31e5d0e76344ba7545ea20.jpg"
],
"createtime": "2020-12-24 15:13:48",
"updatetime": "2020-12-24 15:13:48",
"deletetime": null,
"message": null,
"is_friend": "是否是好友 1=是 0=不是",
"str_time": "首页显示时间"
}
]
})
*/
public function CategoryList(){
//1.获取数据
$data = $this->get_data_array([
['page','分页次数不能为空'],
['num','分页数量不能为空'],
['article_category_id','动态栏目id必须填写'],
]);
//2.获取列表数据
$art_list = model('article')
->where('deletetime',null)
->where('article_category_id',$data['article_category_id'])
->with(['user'])
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
//3.判断是不是搜索
$search = input('param.search');
if (isset($search) && !empty($search)){
$art_list = model('article')
->where('deletetime',null)
->where('article_category_id',$data['article_category_id'])
->where('content','like','%'.$search.'%')
->with(['user'])
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
}
//3.判断是不是好友
$user_id = $this->auth->id;
if (!empty($art_list)){
foreach ($art_list as $key => $val){
//1.查询关注和被关注
$attention = model('user_attention')->where(function ($query) use ($val,$user_id){
$query->where('user_id',$user_id)->where('in_user_id',$val['user_id']);
})->whereOr(function ($query) use ($val,$user_id){
$query->where('user_id',$val['user_id'])->where('in_user_id',$user_id);
})->count();
if ($attention == 2){
$art_list[$key]['is_friend'] = 1;
}else{
$art_list[$key]['is_friend'] = 0;
}
}
}
$this->success('查询数据成功',$art_list);
}
/**
* 搜索动态
* @ApiTitle (搜索动态)
* @ApiMethod (POST)
* @ApiRoute (/api/index/searchArticleList)
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiParams (name="search", type="string", required=true, description="搜索内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询数据成功",
"time": "1608857680",
"data": [
{
"id": 9,
"user_id": 1,
"article_category_id": 7,
"content": "内容",
"images": [
"http://qldk2g57y.hn-bkt.clouddn.com/uploads/20201221/667431fe2f31e5d0e76344ba7545ea20.jpg"
],
"createtime": "2020-12-24 15:13:48",
"updatetime": "2020-12-24 15:13:48",
"deletetime": null,
"message": null,
"is_friend": "是否是好友 1=是 0=不是",
"str_time": "首页显示时间"
}
]
})
*/
public function searchArticleList(){
//1.获取数据
$data = $this->get_data_array([
['page','分页次数不能为空'],
['num','分页数量不能为空'],
['search','搜索内容必须填写'],
]);
//2.查询搜索数据
$art_list = model('article')
->where('deletetime',null)
->where('content','like','%'.$data['search'].'%')
->with(['user'])
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
//3.判断是不是好友
$user_id = $this->auth->id;
if (!empty($art_list)){
foreach ($art_list as $key => $val){
//1.查询关注和被关注
$attention = model('user_attention')->where(function ($query) use ($val,$user_id){
$query->where('user_id',$user_id)->where('in_user_id',$val['user_id']);
})->whereOr(function ($query) use ($val,$user_id){
$query->where('user_id',$val['user_id'])->where('in_user_id',$user_id);
})->count();
if ($attention == 2){
$art_list[$key]['is_friend'] = 1;
}else{
$art_list[$key]['is_friend'] = 0;
}
}
}
//4.添加搜索历史
$user_search = model('user_search')->where('text',$data['search'])->where('user_id',$this->auth->id)->find();
if ($user_search){
$user_search->num += 1;
$user_search->save();
}else{
model('user_search')->create(['text'=>$data['search'],'user_id'=>$this->auth->id]);
}
//5.添加热门搜索
$search_hot = model('search_hot')->where('text',$data['search'])->find();
if ($search_hot){
$search_hot->num += 1;
$search_hot->save();
}else{
model('search_hot')->create(['text'=>$data['search']]);
}
//6.返回数据
$this->success('查询数据成功',$art_list);
}
/**
* 搜索历史:必须登录
* @ApiTitle (搜索历史:必须登录)
* @ApiMethod (POST)
* @ApiRoute (/api/index/search_lishi)
* @ApiParams (name="num", type="integer", required=true, description="请求数量")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询数据成功",
"time": "1608882197",
"data": [
{
"id": 1,
"user_id": 1,
"text": "搜索内容",
"num": "次数",
"createtime": "2020-12-25 15:42:54",
"updatetime": "2020-12-25 15:42:54"
}
]
})
*/
public function search_lishi(){
//1.获取数据
$data = $this->get_data('num','数量不能为空');
//2.获取搜索历史
$user_search = model('user_search')
->where('user_id',$this->auth->id)
->order('num','desc')
->limit($data)
->select();
//3.返回数据
$this->success('查询数据成功',$user_search);
}
/**
* 删除搜索历史:必须登录
* @ApiTitle (删除搜索历史:必须登录)
* @ApiMethod (POST)
* @ApiRoute (/api/index/search_lishi_del)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "删除数据成功",
"time": "1608882230",
"data": null
})
*/
public function search_lishi_del(){
//1.删除搜索记录
model('user_search')
->where('user_id',$this->auth->id)
->delete();
//2.返回数据
$this->success('删除数据成功');
}
/**
* 热门搜索:无需登录
* @ApiTitle (热门搜索:无需登录)
* @ApiMethod (POST)
* @ApiRoute (/api/index/search_hot)
* @ApiParams (name="num", type="integer", required=true, description="请求数量")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询数据成功",
"time": "1608882251",
"data": [
{
"id": 1,
"text": "搜索内容",
"num": "搜索次数",
"createtime": "2020-12-25 15:42:55",
"updatetime": "2020-12-25 15:42:55"
}
]
})
*/
public function search_hot(){
//1.获取数据
$num = $this->get_data('num','数量不能为空');
//2.获取搜索历史
$user_search = model('search_hot')
->order('num','desc')
->limit($num)
->select();
//3.返回数据
$this->success('查询数据成功',$user_search);
}
}