微信截图_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
网友评论