微信截图_20171124112446.png
微信截图_20171124135000.png
微信截图_20171124135428.png

News.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/23
 * Time: 17:03
 */

namespace app\api\controller\v1;

use app\api\controller\Common;
use app\common\lib\exception\ApiException;

class News extends Common
{
    public function index()
    {
        //仿照之前讲解的validate验证机制 做相关检验
        $data = input('get.');

        $whereData['status'] = config('code.status_normal');
        if (!empty($data['catid'])) {
            $whereData['catid'] = input('get.catid', 0, 'intval');
        }

        if (!empty($data['title'])) {
            $whereData['title'] = ['like', '%' . $data['title'] . '%'];
        }


        $this->getPageAndSize($data);
        $total = model('News')->getNewsByCountCondition($whereData);
        $news = model('News')->getNewsByCondition($whereData, $this->from, $this->size);

        $result = [
            'total' => $total,
            'page_num' => ceil($total / $this->size),
            'list' => $this->getDealNews($news),
        ];

        return show(1, 'OK', $result, 200);

    }

    /**
     * 获取详情接口
     */
    public function read()
    {
        //详情页面 APP -》1.x.com/3.html 2.接口

        $id = input('param.id', 0, 'intval');
        if (empty($id)) {
            new ApiException('id is not ', 404);
        }

        //通过id 去获取数据表里的数据
        try {
            $news = model('News')->get($id);
        } catch (\Exception $e) {
            return new ApiException($e->getMessage(), 400);
        }
        if (empty($news) || $news->status != config('code.status_normal')) {
            return new ApiException('不存在该新闻', 404);
        }
        try {
            model('News')->where(['id' => $id])->setInc('read_count');
        } catch (\Exception $e) {
            return new ApiException($e->getMessage(), 400);
        }
        $cats = config('cat.list');
        $news->catname = $cats[$news->catid];

        return show(config('code.success'), 'OK', $news, 200);

    }


}
image.png

本站以现代、古代情诗为主,情诗网创办于2013年,以原创爱情诗歌、经典情诗、现代情诗、古代情诗、英文情诗、情诗绝句为主并收集古诗、古诗词、诗歌大全、诗词名句的文学门户。方便您下次继续阅读;可以放在浏览器的收藏夹中(快捷键Ctrl+D);或者看到喜欢或者有趣的诗词可以通过分享按钮给你的好友分享;情诗网是目前最全情诗大全网站之一。并欢迎广大诗歌爱好者阅览投稿!喜欢本站的话请大家把本站告诉给你朋友哦!地址是 www.qingshiwang.com !