Article.php
20.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
<?php
namespace app\api\controller;
use app\api\model\ArticleType;
use app\api\model\LikeArticle;
use app\api\model\LikeStudent;
use app\api\model\LookArticle;
use app\api\model\School;
use app\api\model\Student;
use app\common\controller\Api;
use app\api\model\Article as ArticleModel;
/**
* 文章相关
*/
class Article extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = ['*'];
// 无需鉴权的接口,*表示全部
protected $noNeedRight = ['*'];
/**
* 获取大学或学院或学长的文章
* @ApiTitle (获取大学或学院或学长的文章)
* @ApiSummary (获取大学或学院或学长的文章)
* @ApiMethod (GET)
* @ApiParams (name="university_id", type="integer", required=false, description="大学ID")
* @ApiParams (name="college_id", type="integer", required=false, description="大学ID")
* @ApiParams (name="user_id", type="integer", required=false, description="学长ID")
* @ApiParams (name="page", type="integer", required=true, description="分页码")
* @ApiParams (name="size", type="integer", required=true, description="分页数 默认10")
* @ApiParams (name="type_id", type="integer", required=true, description="文章的分类ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取大学的文章成功",
"time": "1586332327",
"data": [
{
"id": 1,
"createtime": 1586309985,
"updatetime": 1586309985,
"show_switch": 1,
"title": "文章标题",
"article_type_id": 1,
"show_type": "1",
"des_content": "内容",
"des_images": "图片集例:1.jpg,2.jpg",
"weigh": 0,
"user_id": 1,
"deletetime": null,
"level": "1",
"nickname": "谢百川(发布人信息)",
"head_image": "www.a.jpg(头像)",
"school_id": 1,
"subject_ids": "",
"up_id": 0,
"college_id": 0,
"university_id": 1,
"graduated_id": 0,
"starttime": 1286640000,
"endtime": null,
"email": "1",
"article_type_name": "经验"
}
]
})
*/
public function getArticleList()
{
$myId = $this->auth->id;
$myId = 1;
$universityId = $this->request->param('university_id', 0, 'int');
$collegeId = $this->request->param('college_id', 0, 'int');
if(!$universityId && !$collegeId) {
$this->error('您的提交有误');
}
$page = $this->request->param('page', 1, 'int');
$size = $this->request->param('size', 10, 'int');
$typeId = $this->request->param('type_id', 0, 'int');
$userId = $this->request->param('user_id', 0, 'int');
$where['a.show_switch'] = ['=', 1];
$student = new Student();
$studentInfo = $student->infoByUserIdCanShow($myId);
if(!$studentInfo) {
$this->error('您的提交有误');
}
$type = 1;
if($type) { // 学校限制
if(!in_array($type, [1, 2, 3])) {
$this->error('您的提交有误');
}
$schoolIds = [];
// 获取学长
if($type = 1) {
$schoolIds = [$studentInfo['school_id']];
} elseif ($type = 2) {
$school = new School();
$schoolInfo = $school->infoById($studentInfo['school_id']);
$schoolIds = $school->listByCityId($schoolInfo['city_id']);
} elseif ($type = 3) {
$school = new School();
$schoolInfo = $school->infoById($studentInfo['school_id']);
$schoolIds = $school->listByProvinceId($schoolInfo['province_id']);
}
if($schoolIds) {
$where['b.school_id'] = ['in', $schoolIds];
}
}
if($universityId) {
$where['b.university_id'] = ['=', $universityId];
}
if($collegeId) {
$where['b.college_id'] = ['=', $collegeId];
}
if($typeId) {
$where['a.article_type_id'] = ['=', $typeId];
}
if($userId) {
$where['a.user_id'] = ['=', $userId];
}
// 获取文章
$article = new ArticleModel();
$articleList = $article->listByWhere($where, $page, $size, $studentInfo['school_id']);
if(!$articleList) {
$this->success('获取大学或学院的文章成功', $articleList);
}
$articleType = new ArticleType();
$articleTypeList = $articleType->listAll();
$articleTypeListObj = [];
foreach ($articleTypeList as $k => $v) {
$articleTypeListObj[$v['id']] = $v;
}
foreach ($articleList as $k => $v) {
if(isset($articleTypeListObj[$v['article_type_id']])) {
$articleList[$k]['article_type_name'] = $articleTypeListObj[$v['article_type_id']]['name'];
} else {
$articleTypeList[$k]['article_type'] = '';
}
}
$this->success('获取大学或学院的文章成功', $articleList);
}
/**
* 获取文章的详细信息
* @ApiTitle (获取文章的详细信息)
* @ApiSummary (获取文章的详细信息)
* @ApiMethod (GET)
* @ApiParams (name="article_id", type="integer", required=false, description="大学ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取文章的详细信息成功",
"time": "1586418417",
"data": {
"id": 1,
"createtime": 0,
"updatetime": 0,
"show_switch": 1,
"title": "",
"article_type_id": 1,
"show_type": "1",
"des_content": "1",
"des_images": "1.jpg,2.jpg",
"weigh": 0,
"user_id": 1,
"deletetime": null,
"is_like": 0,
"date": "1970-01-01"
}
})
*/
public function getArticleInfo()
{
$userId = $this->auth->id;
$userId = 1;
$articleId = $this->request->param('article_id', 0, 'int');
if(!$articleId) {
$this->error('您的提交有误');
}
$article = new ArticleModel();
$articleInfo = $article->infoById($articleId);
if(!$articleInfo) {
$this->error('您的提交有误');
}
// 记录浏览
$lookArticle = new LookArticle();
$lookArticleInfo = $lookArticle->infoById($userId, $articleId);
if($lookArticleInfo) {
$updateData = ['updatetime' => time()];
$lookArticle->updateOne($lookArticleInfo['id'], $updateData);
} else {
$insertData = [
'user_id' => $userId,
'article_id' => $articleId,
'createtime' => time(),
'updatetime' => time(),
];
$lookArticle->addOne($insertData);
}
$likeArticle = new LikeArticle();
$likeArticleInfo = $likeArticle->infoById($userId, $articleId);
if(!$likeArticleInfo) {
$articleInfo['is_like'] = 0;
} else {
$articleInfo['is_like'] = 1;
}
$articleInfo['date'] = date('Y-m-d', strtotime($articleInfo['createtime']));
$this->success('获取文章的详细信息成功', $articleInfo);
}
/**
* 关注/取关文章
* @ApiTitle (关注/取关文章)
* @ApiSummary (关注/取关文章)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="article_id", type="integer", required=true, description="文章ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "取关成功",
"time": "1586421214",
"data": null
})
*/
public function likeArticle()
{
$userId = $this->auth->id;
$userId = 1;
$articleId = $this->request->param('article_id', 0, 'int');
if(!$articleId) {
$this->error('您的操作有误');
}
$likeArticle = new LikeArticle();
$likeArticleInfo = $likeArticle->infoById($userId, $articleId);
if($likeArticleInfo) {
if($likeArticle->updateOne($likeArticleInfo['id'], ['deletetime' => time()])) {
$this->success('取关成功');
} else {
$this->error('取关失败');
}
} else {
$insertData = [
'user_id' => $userId,
'article_id' => $articleId,
'createtime' => time(),
'updatetime' => time(),
];
if($likeArticle->addOne($insertData)) {
$this->success('关注成功');
} else {
$this->error('关注失败');
}
}
}
/**
* 清空文章浏览记录
* @ApiTitle (清空文章浏览记录)
* @ApiSummary (清空文章浏览记录)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ()
*/
public function delLookArticle()
{
$userId = $this->auth->id;
$userId = 1;
$lookArticle = new LookArticle();
$res = $lookArticle->updateSomeByUserId($userId, ['deletetime' => time()]);
if($res) {
$this->success('清空文章浏览记录成功');
} else {
$this->error('清空文章浏览记录失败,请您重试');
}
}
/**
* 获取我收藏的文章
* @ApiTitle (获取我收藏的文章)
* @ApiSummary (获取我收藏的文章)
* @ApiMethod (GET)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="page", type="integer", required=true, description="分页码")
* @ApiParams (name="size", 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", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取我收藏的文章成功",
"time": "1586483115",
"data": [
{
"id": 1,
"createtime": 0,
"updatetime": 0,
"article_id": 1(文章ID),
"user_id": 1,
"deletetime": null,
"level": "1",
"nickname": "谢百川(发表人的名字)",
"head_image": "www.a.jpg发表人的头像)",
"school_id": 1,
"subject_ids": "",
"up_id": 1,
"college_id": 1,
"university_id": 1,
"graduated_id": 1,
"starttime": 1286640000,
"endtime": null,
"show_switch": 1,
"email": "1",
"title": "",
"article_type_id": 1,
"show_type": "1",
"des_content": "1(文章的内容)",
"des_images": "",
"weigh": 0,
"is_like_user": 1(发表人1关注 0没关注),
"article_type_name": "经验(文章的类型的名字)"
}
]
})
*/
public function getMyLikeArticle()
{
$userId = $this->auth->id;
$userId = 1;
$page = $this->request->param('page', 0, 'int');
$size = $this->request->param('size', 0, 'int');
$likeArticle = new LikeArticle();
$likeArticleList = $likeArticle->listByUserId($userId, $page, $size);
if(!$likeArticleList) {
$this->success('获取我收藏的文章成功', $likeArticleList);
}
$likeStudent = new LikeStudent();
$likeStudentList = $likeStudent->listAllByUserId($userId);
$likeStudentListObj = [];
foreach ($likeStudentList as $v) {
$likeStudentListObj[$v['userb_id']] = $v;
}
$articleType = new ArticleType();
$articleTypeList = $articleType->listAll();
$articleTypeListObj = [];
foreach ($articleTypeList as $k => $v) {
$articleTypeListObj[$v['id']] = $v;
}
foreach ($likeArticleList as $k => $v) {
if(isset($likeStudentListObj[$v['user_id']])) {
$likeArticleList[$k]['is_like_user'] = 1;
} else {
$likeArticleList[$k]['is_like_user'] = 0;
}
if(isset($articleTypeListObj[$v['article_type_id']])) {
$likeArticleList[$k]['article_type_name'] = $articleTypeListObj[$v['article_type_id']]['name'];
} else {
$likeArticleList[$k]['article_type_name'] = '';
}
}
$this->success('获取我收藏的文章成功', $likeArticleList);
}
/**
* 添加一个文章
* @ApiTitle (添加一个文章)
* @ApiSummary (添加一个文章)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="title", type="string", required=true, description="标题")
* @ApiParams (name="article_type_id", type="integer", required=true, description="分类ID")
* @ApiParams (name="show_type", type="integer", required=true, description="1仅校友 2全部")
* @ApiParams (name="des_content", type="string", required=true, description="内容")
* @ApiParams (name="des_images", type="string", required=true, description="图片集例:a.jpg,b.jpg")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ()
*/
public function addArticle()
{
$userId = $this->auth->id;
$userId = 2;
$title = $this->request->param('title', '', 'string');
if(!$title) {
$this->error('您的文章标题不能为空');
}
if(mb_strlen($title) > 255) {
$this->error('您的文章标题不能超过255汉字长度');
}
$articleTypeId = $this->request->param('article_type_id', 0, 'int');
if(!$articleTypeId) {
$this->error('请您选择正确的文章类型');
}
$showType = $this->request->param('show_type', 1, 'int');
$desContent = $this->request->param('des_content', '', 'string');
if(!$desContent) {
$this->error('您的文章内容不能为空');
}
$desImages = $this->request->param('des_images', '', 'string');
$imageExtArr = ['jpg', 'jpeg', 'png'];
if($desImages) {
$desImagesArr = explode(',', $desImages);
foreach ($desImagesArr as $k => $v) {
$pathInfo = pathinfo($v);
if(!isset($pathInfo['extension']) || !in_array($pathInfo['extension'], $imageExtArr)) {
$this->error('请选择正确的图片格式');
}
}
}
$student = new Student();
$studentInfo = $student->infoByUserIdCanShow($userId);
if(!$studentInfo) {
$this->error('您的操作有误');
}
if($studentInfo['level'] == 1) {
$this->error('高中生不能发布文章哦');
}
$insertData = [
'title' => $title,
'article_type_id' => $articleTypeId,
'show_type' => $showType,
'des_content' => $desContent,
'des_images' => '',
'user_id' => $userId
];
$article = new ArticleModel();
$res = $article->addOne($insertData);
if(!$res) {
$this->error('您的文章发表失败,您可以重试一下');
} else {
$this->success('您的文章发表成功,请您耐心等待审核');
}
}
/**
* 获取我发布的文章
* @ApiTitle (获取我发布的文章)
* @ApiSummary (获取我发布的文章)
* @ApiMethod (GET)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="page", type="integer", required=true, description="分页码")
* @ApiParams (name="size", 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", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ()
*/
public function getMyArticle()
{
$userId = $this->auth->id;
$userId = 1;
$page = $this->request->param('page', 1, 'int');
$size = $this->request->param('size', 10, 'int');
$article = new ArticleModel();
$articleList = $article->listByUserId($userId, $page, $size);
if(!$articleList) {
$this->success('获取我发布的文章', $articleList);
}
$articleType = new ArticleType();
$articleTypeList = $articleType->listAll();
$articleTypeListObj = [];
foreach ($articleTypeList as $k => $v) {
$articleTypeListObj[$v['id']] = $v;
}
foreach ($articleList as $k => $v) {
if(isset($articleTypeListObj[$v['article_type_id']])) {
$articleList[$k]['article_type_name'] = $articleTypeListObj[$v['article_type_id']]['name'];
} else {
$articleTypeList[$k]['article_type'] = '';
}
}
$this->success('获取我发布的文章', $articleList);
}
/**
* 获取所有文章类型
* @ApiTitle (获取所有文章类型)
* @ApiSummary (获取所有文章类型)
* @ApiMethod (GET)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取所有文章类型成功",
"time": "1586484966",
"data": [
{
"id": 1,
"name": "经验",
"weigh": 0,
"createtime": 0,
"updatetime": 0,
"show_switch": 1,
"deletetime": null
}
]
})
*/
public function getArticleType()
{
$articleType = new ArticleType();
$articleTypeList = $articleType->listAll();
$this->success('获取所有文章类型成功', $articleTypeList);
}
}