Index.php
4.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
<?php
namespace app\mobile\controller;
use app\common\controller\Api;
use app\mobile\model\IndexBanner;
use app\mobile\model\News;
use app\mobile\model\Exam;
/**
* 首页接口
* @ApiWeigh (99)
*/
class Index extends Api
{
protected $noNeedLogin = ['index','newsInfo'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
}
/**
* @ApiTitle (首页)
* @ApiSummary (首页)
* @ApiMethod (POST)
*
* @ApiParams (name="exam_id", type="int", required=true, description="考试ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599026313",
"data": {
"exam_list": [{ //二级考试列表
"id": 3, //ID
"nickname": "" //昵称
}],
"banner_list": [{ //轮播图列表
"id": 1, //ID
"image": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png", //图片地址
"link": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png" //链接地址
}],
"countdown": 26, //距离考试倒计时天数
"news_list": [{ //报考资讯列表
"id": 1, //资讯ID
"cover": "", //封面图
"title": "关于建造师", //标题
"createtime": "1970.01.01", //发布时间
"read_num": 120 //浏览量
}]
}
})
*/
public function index()
{
$exam_id = $this->request->param('exam_id');
empty($exam_id) && $this->error('缺少必要参数');
// 所有二级分类
$exam_list = Exam::where('pid','>',0)->field('id,nickname')->select();
// 轮播图
$banner_list = IndexBanner::order('createtime desc')->field('id,image,url')->select();
// 距离考试倒计时天数
$exam_time = Exam::where('id',$exam_id)->value('exam_time');
$countdown = ceil(($exam_time-time())/86400);
// 报考资讯
$news_list = News::order('createtime desc')->select();
foreach ($news_list as $value) {
$value->visible(['id','cover','title','read_num','createtime']);
}
$this->success('成功',compact('exam_list','banner_list','countdown','news_list'));
}
/**
* @ApiTitle (报考资讯)
* @ApiSummary (报考资讯)
* @ApiMethod (POST)
*
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599026840",
"data": {
"id": 1, //资讯ID
"title": "关于建造师", //资讯标题
"content": "关于建造师内容", //资讯内容
"createtime": "1970.01.01", //发布时间
"read_num": 120 //阅读量
}
})
*/
public function newsList()
{
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
$data = News::order('createtime desc')
->paginate($page_num,false,['page'=>$page])
->each(function($v){
$v->visible(['id','cover','title','read_num','createtime']);
})->toArray();
$this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
}
/**
* @ApiTitle (资讯详情)
* @ApiSummary (资讯详情)
* @ApiMethod (POST)
*
* @ApiParams (name="news_id", type="int", required=true, description="资讯ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599026840",
"data": {
"id": 1, //资讯ID
"title": "关于建造师", //资讯标题
"content": "关于建造师内容", //资讯内容
"createtime": "1970.01.01", //发布时间
"read_num": 120 //阅读量
}
})
*/
public function newsInfo()
{
$news_id = $this->request->param('news_id');
empty($news_id) && $this->error('缺少必要参数');
$info = News::get($news_id);
empty($info) && $this->error('资讯信息不存在');
$info = $info->visible(['id','title','createtime','content']);
// 阅读量加1
News::where('id',$news_id)->setInc('read_num_real');
$this->success('成功',$info);
}
}