审查视图

application/mobile/controller/Topic.php 11.0 KB
何书鹏 authored
1 2 3 4 5 6
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use app\common\controller\Api;
何书鹏 authored
7 8
use app\mobile\model\Topic as TopicModel;
use app\mobile\model\TopicCategory;
何书鹏 authored
9 10 11 12
use app\common\model\Attachment;

/**
 * 论坛接口
何书鹏 authored
13
 * @ApiWeigh (92)
何书鹏 authored
14
 */
何书鹏 authored
15
class Topic extends Api
何书鹏 authored
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
{
	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" //原价
        }]
    })
     */
何书鹏 authored
43
    public function topicCategory()
何书鹏 authored
44
    {
何书鹏 authored
45
        $list = TopicCategory::order('createtime desc')
何书鹏 authored
46 47 48 49 50 51 52 53 54 55 56
            ->field('id,name')
            ->select();
        $this->success('成功',$list);
    }


    /**
     * @ApiTitle    (发帖)
     * @ApiSummary  (发帖)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
何书鹏 authored
57
     * @ApiParams   (name="topic_category_id", type="inter", required=false, description="话题分类ID")
何书鹏 authored
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
     * @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();
何书鹏 authored
77
        empty($post['topic_category_id']) && $this->error('请选择话题分类');
何书鹏 authored
78 79
        empty($post['title']) && $this->error('请输入标题');
        empty($post['content']) && $this->error('请填写话题内容');
何书鹏 authored
80
        TopicModel::create(array_merge([
何书鹏 authored
81 82 83 84 85 86 87 88 89
            'user_id' => $this->auth->id
        ],$post),true);
        $this->success('发帖成功');
    }

    /**
     * @ApiTitle    (论坛-首页)
     * @ApiSummary  (论坛-首页)
     * @ApiMethod   (POST)
何书鹏 authored
90
     * @ApiParams   (name="topic_category_id", type="inter", required=false, description="话题分类ID")
何书鹏 authored
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
     * @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()
    { 
何书鹏 authored
109
        $topic_category_id = $this->request->param('topic_category_id');
何书鹏 authored
110 111 112
        $order = $this->request->param('order',0);
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
何书鹏 authored
113 114
        if(empty($topic_category_id)){
            $where['topic_category_id'] = $topic_category_id;
何书鹏 authored
115 116 117 118 119 120 121 122 123 124 125 126 127 128
        }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;
        }
何书鹏 authored
129
        $data = TopicModel::with(['user'])
何书鹏 authored
130 131
            ->where($where)
            ->order($order)
1  
何书鹏 authored
132 133 134 135 136 137 138
            ->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']]);
何书鹏 authored
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
    }

    /**
     * @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()
    {
何书鹏 authored
166
        $history_list = Db::name('mobile_topic_keyword')
何书鹏 authored
167 168 169
            ->where('user_id',$this->auth->id)
            ->limit(10)
            ->select();
何书鹏 authored
170
        $hot_list = Db::name('mobile_topic_keyword_hot')
何书鹏 authored
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
            ->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('请输入关键词');
何书鹏 authored
207
        $data = TopicModel::with(['user'])
1  
何书鹏 authored
208
            ->where('title','like','%'.$keyword.'%')
何书鹏 authored
209
            ->order(['hot_num'=>'desc'])
1  
何书鹏 authored
210 211 212 213 214 215 216
            ->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']]);
何书鹏 authored
217 218 219 220 221 222 223
    }

    /**
     * @ApiTitle    (话题详情)
     * @ApiSummary  (话题详情)
     * @ApiMethod   (POST)
     *
何书鹏 authored
224
     * @ApiParams (name="topic_id", type="int", required=true, description="话题ID")
何书鹏 authored
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
     *
     * @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()
    {
何书鹏 authored
244 245 246
        $topic_id = $this->request->param('topic_id');
        empty($topic_id) && $this->error('缺少必要参数');
        $info = TopicModel::get($topic_id,['user']);
何书鹏 authored
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        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)
     *
何书鹏 authored
269
     * @ApiParams (name="topic_id", type="int", required=true, description="话题ID")
何书鹏 authored
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
     *
     * @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()
    {
何书鹏 authored
289
        $topic_id = $this->request->param('topic_id');
何书鹏 authored
290
    }
何书鹏 authored
291 292

    /**
1  
何书鹏 authored
293 294
     * @ApiTitle    (评论-列表)
     * @ApiSummary  (评论-列表)
何书鹏 authored
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="topic_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 //试卷分数(单位:分)
        }
    })
     */
1  
何书鹏 authored
315
    public function appraiseList()
何书鹏 authored
316 317 318
    {
        $topic_id = $this->request->param('topic_id');
    }
何书鹏 authored
319
}