[PHP高可用后端]③③--列表页面开发
2017-11-23 本文已影响24人
子木同
微信截图_20171122172956.png
image.png
route.php
<?php
use think\Route;
Route::get('test', 'api/test/index');
Route::put('test/:id', 'api/test/update');
Route::delete('test/:id', 'api/test/delete');
Route::resource('test', 'api/test');
Route::get('api/:ver/cat', 'api/:ver.cat/read');
Route::get('api/:ver/index', 'api/:ver.index/index');
Route::resource('api/:ver/news', 'api/:ver.news');
News.php(Model)
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/20
* Time: 16:34
*/
namespace app\common\model;
class News extends Base
{
public function getNews($data = [])
{
$data['status'] = [
'neq', config('code.status_delete'),
];
$order = ['id' => 'desc'];
$result = $this->where($data)
->order($order)
->paginate();
return $result;
}
public function getNewsByCondition($condition = [], $from, $size = 5)
{
if (!isset($condition['status'])) {
$condition['status'] = [
'neq', config('code.status_delete')
];
}
$order = ['id' => 'desc'];
$result = $this->where($condition)
->field($this->getListField())
->limit($from, $size)
->order($order)
->select();
return $result;
}
public function getNewsByCountCondition($condition = [])
{
if (!isset($condition['status'])) {
$condition['status'] = [
'neq', config('code.status_delete')];
}
return $this->where($condition)
->count();
}
public function getIndexHadNormalNews($num = 4)
{
$data = [
'status' => 1,
'is_head_figure' => 1,
];
$order = [
'id' => 'desc',
];
return $this->where($data)
->field($this->getListField())
->order($order)
->limit($num)
->select();
}
public function getPositionNormalNews($num = 20)
{
$data = [
'status' => 1,
'is_position' => 1,
];
$order = [
'id' => 'desc',
];
return $this->where($data)
->field($this->getListField())
->order($order)
->limit($num)
->select();
}
private function getListField()
{
return [
'id',
'catid',
'image',
'title',
'read_count',
'status',
'is_position',
'update_time',
'create_time',
];
}
}
->field($this->getListField())
private function getListField()
{
return [
'id',
'catid',
'image',
'title',
'read_count',
'status',
'is_position',
'update_time',
'create_time',
];
}
Common.php
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/23
* Time: 11:30
*/
namespace app\api\controller;
use app\common\lib\Aes;
use app\common\lib\exception\ApiException;
use app\common\lib\IAuth;
use app\common\lib\Time;
use think\Cache;
use think\Controller;
class Common extends Controller
{
public $headers = '';
public $page = 1;
public $size = 5;
public $from = 0;
protected function _initialize()
{
$this->checkRequestAuth();
// $this->testAes();
}
public function checkRequestAuth()
{
$headers = request()->header();
if (empty($headers['sign'])) {
throw new ApiException('sign不存在', 400);
}
if (!in_array($headers['app_type'], config('app.apptypes'))) {
throw new ApiException('app_type不合法', 400);
}
if (!IAuth::checkSignPass($headers)) {
throw new ApiException('授权码sign失败', 401);
}
Cache::set($headers['sign'], config('app.app_sign_cache_time'));
$headers = $this->headers;
}
public function testAes()
{
$data = [
'did' => '12345dg',
'version' => 1,
'time' => Time::get13TimeStamp(),
];
// $str = 'sRCvj52mZ8G+u2OdHYwmysvczmCw+RrAYWiEaXFI/5A=';
// echo (new Aes())->decrypt($str);
// exit;
echo IAuth::setSign($data);
exit;
}
public function getDealNews($news = [])
{
if (empty($news)) {
return [];
}
$cats = config('cat.list');
foreach ($news as $key => $new) {
$news[$key]['catname'] = $cats[$new['catid']] ? $cats[$new['catid']] : '-';
}
return $news;
}
public function getPageAndSize($data)
{
$this->page = !empty($data['page']) ? $data['page'] : 1;
$this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
$this->from = ($this->page - 1) * $this->size;
}
}
public $page = 1;
public $size = 5;
public $from = 0;
public function getPageAndSize($data)
{
$this->page = !empty($data['page']) ? $data['page'] : 1;
$this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
$this->from = ($this->page - 1) * $this->size;
}
News.php(Controller)
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/23
* Time: 17:03
*/
namespace app\api\controller\v1;
use app\api\controller\Common;
class News extends Common
{
public function index()
{
//仿照之前讲解的validate验证机制 做相关检验
$data = input('get.');
$whereData['status'] = config('code.status_normal');
$whereData['catid'] = input('get.catid');
$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);
}
}
image.png