Forum.php
10.2 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
<?php
namespace app\mobile\controller;
use think\Validate;
use think\Db;
use app\common\controller\Api;
use app\mobile\model\Forum as ForumModel;
use app\mobile\model\ForumCategory;
use app\common\model\Attachment;
/**
* 论坛接口
* @ApiWeigh (92)
*/
class Forum extends Api
{
protected $noNeedLogin = ['index','info'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
}
/**
* @ApiTitle (发帖-话题分类)
* @ApiSummary (发帖-话题分类)
* @ApiMethod (POST)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599201246",
"data": [{
"id": 1, //密卷名称
"title": "测试密卷", //密卷标题
"do_num": 20, //做过人数
"current_price": "10.00", //现价
"original_price": "10000.00" //原价
}]
})
*/
public function forumCategory()
{
$list = ForumCategory::order('createtime desc')
->field('id,name')
->select();
$this->success('成功',$list);
}
/**
* @ApiTitle (发帖)
* @ApiSummary (发帖)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="forum_category_id", type="inter", required=false, description="话题分类ID")
* @ApiParams (name="title", type="string", required=false, description="标题")
* @ApiParams (name="content", type="string", required=false, description="话题内容")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599201246",
"data": [{
"id": 1, //密卷名称
"title": "测试密卷", //密卷标题
"do_num": 20, //做过人数
"current_price": "10.00", //现价
"original_price": "10000.00" //原价
}]
})
*/
public function topicAdd()
{
$post = $this->request->param();
empty($post['forum_category_id']) && $this->error('请选择话题分类');
empty($post['title']) && $this->error('请输入标题');
empty($post['content']) && $this->error('请填写话题内容');
ForumModel::create(array_merge([
'user_id' => $this->auth->id
],$post),true);
$this->success('发帖成功');
}
/**
* @ApiTitle (论坛-首页)
* @ApiSummary (论坛-首页)
* @ApiMethod (POST)
* @ApiParams (name="forum_category_id", type="inter", required=false, description="话题分类ID")
* @ApiParams (name="order", type="inter", required=false, description="排序方式:0=最新,1=热门,2=下载量")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599201246",
"data": [{
"id": 1, //密卷名称
"title": "测试密卷", //密卷标题
"do_num": 20, //做过人数
"current_price": "10.00", //现价
"original_price": "10000.00" //原价
}]
})
*/
public function index()
{
$forum_category_id = $this->request->param('forum_category_id');
$order = $this->request->param('order',0);
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
if(empty($forum_category_id)){
$where['forum_category_id'] = $forum_category_id;
}else{
$where['is_recommend'] = '1';
}
switch ($order) {
case 0:
$order = ['createtime' => 'desc'];
break;
case 1:
$order = ['hot_num' => 'desc'];
break;
case 2:
$order = ['download_num' => 'desc'];
break;
}
$data = ForumModel::with(['user'])
->where($where)
->order($order)
->paginate($page_num,false,['page'=>$page])
->each(function($v){
$v->visible(['id','title','content','createtime','agree_num','download_num','user']);
$v->createtime = date('Y.m.d H:i',$v['createtime']);
$v->getRelation('user')->visible(['user_id','avatar','nickname']);
})->toArray();
$this->success('成功',['total'=>$data['total'],'list'=>$data['list']]);
}
/**
* @ApiTitle (搜索历史)
* @ApiSummary (搜索历史)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="secret_id", type="int", required=true, description="密卷ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599046220",
"data": {
"id": 1, //试卷ID
"title": "测试试卷", //试卷标题
"year": 2015, //年费(单位:年)
"time": 100, //答题时间(单位:分)
"pass_score": 80, //合格分数
"description": "这个还行", //试卷描述
"do_num": 10, //回答人数
"full_score": 100 //试卷分数(单位:分)
}
})
*/
public function searchKeyword()
{
$history_list = Db::name('mobile_forum_keyword')
->where('user_id',$this->auth->id)
->limit(10)
->select();
$hot_list = Db::name('mobile_forum_keyword_hot')
->limit(10)
->select();
$this->success('成功',compact('history_list','hot_list'));
}
/**
* @ApiTitle (论坛搜索结果)
* @ApiSummary (论坛搜索结果)
* @ApiMethod (POST)
*
* @ApiParams (name="keyword", type="string", required=true, description="关键词")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599046220",
"data": {
"id": 1, //试卷ID
"title": "测试试卷", //试卷标题
"year": 2015, //年费(单位:年)
"time": 100, //答题时间(单位:分)
"pass_score": 80, //合格分数
"description": "这个还行", //试卷描述
"do_num": 10, //回答人数
"full_score": 100 //试卷分数(单位:分)
}
})
*/
public function search()
{
$keyword = $this->request->param('keyword');
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
empty($keyword) && $this->error('请输入关键词');
$data = ForumModel::with(['user'])
->where('title','like','%'.$keyword.'%')
->order(['hot_num'=>'desc'])
->paginate($page_num,false,['page'=>$page])
->each(function($v){
$v->visible(['id','title','content','createtime','agree_num','download_num','user']);
$v->createtime = date('Y.m.d H:i',$v['createtime']);
$v->getRelation('user')->visible(['user_id','avatar','nickname']);
})->toArray();
$this->success('成功',['total'=>$data['total'],'list'=>$data['list']]);
}
/**
* @ApiTitle (话题详情)
* @ApiSummary (话题详情)
* @ApiMethod (POST)
*
* @ApiParams (name="forum_id", type="int", required=true, description="话题ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599046220",
"data": {
"id": 1, //试卷ID
"title": "测试试卷", //试卷标题
"year": 2015, //年费(单位:年)
"time": 100, //答题时间(单位:分)
"pass_score": 80, //合格分数
"description": "这个还行", //试卷描述
"do_num": 10, //回答人数
"full_score": 100 //试卷分数(单位:分)
}
})
*/
public function info()
{
$forum_id = $this->request->param('forum_id');
empty($forum_id) && $this->error('缺少必要参数');
$info = ForumModel::get($forum_id,['user']);
if(!empty($info)){
$info->visible(['id','title','content','createtime','agree_num','download_num','user']);
$info->createtime = date('Y.m.d H:i',$info['createtime']);
if(!empty($info['attachments'])){
$attachment_arr = explode(',', $info['attachments']);
$name_arr = [];
foreach ($attachment_arr as $v) {
$attachment = Attachment::getByUrl($v);
$name_arr[] = json_decode($attachment['extparam'],true)['name'];
}
$info->attachments = $name_arr;
}
$info->getRelation('user')->visible(['user_id','avatar','nickname']);
}
$this->success('成功',$list);
}
/**
* @ApiTitle (下载)
* @ApiSummary (下载)
* @ApiMethod (POST)
*
* @ApiParams (name="forum_id", type="int", required=true, description="话题ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599046220",
"data": {
"id": 1, //试卷ID
"title": "测试试卷", //试卷标题
"year": 2015, //年费(单位:年)
"time": 100, //答题时间(单位:分)
"pass_score": 80, //合格分数
"description": "这个还行", //试卷描述
"do_num": 10, //回答人数
"full_score": 100 //试卷分数(单位:分)
}
})
*/
public function download()
{
$forum_id = $this->request->param('forum_id');
}
}