Forum.php 10.2 KB
<?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');
    }
}