Index.php 4.6 KB
<?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);
    }
}