<?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');
<?php/**
* Created by PhpStorm.
* User: tong
* Date: 二0一七/一一/二0
* Time: 一六:三四
*/namespace app\co妹妹on\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 = 五)
{
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 = 四)
{
$data = [
'status' => 一,
'is_head_figure' => 一,
];
$order = [
'id' => 'desc',
];
return $this->where($data)
->field($this->getListField())
->order($order)
->limit($num)
->select();
}
public function getPositionNormalNews($num = 二0)
{
$data = [
'status' => 一,
'is_position' => 一,
];
$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',
];
}
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 二0一七/一一/二三
* Time: 一一:三0
*/
namespace app\api\controller;
use app\co妹妹on\lib\Aes;
use app\co妹妹on\lib\exception\ApiException;
use app\co妹妹on\lib\IAuth;
use app\co妹妹on\lib\Time;
use think\Cache;
use think\Controller;
class Co妹妹on extends Controller
{
public $headers = '';
public $page = 一;
public $size = 五;
public $from = 0;
protected function _initialize()
{
$this->checkRequestAuth();
// $this->testAes();
}
public function checkRequestAuth()
{
$headers = request()->header();
if (empty($headers['sign'])) {
throw new ApiException('sign没有存正在', 四00);
}
if (!in_array($headers['app_type'], config('app.apptypes'))) {
throw new ApiException('app_type没有开法', 四00);
}
if (!IAuth::checkSignPass($headers)) {
throw new ApiException('受权码sign得败', 四0一);
}
Cache::set($headers['sign'], config('app.app_sign_cache_time'));
$headers = $this->headers;
}
public function testAes()
{
$data = [
'did' => '一二三四五dg',
'version' => 一,
'time' => Time::get一三TimeStamp(),
];
// $str = 'sRCvj五二mZ八G+u二OdHYwmysvczmCw+RrAYWiEaXFI/五A=';
// 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'] : 一;
$this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
$this->from = ($this->page - 一) * $this->size;
}
}
public $page = 一;
public $size = 五;
public $from = 0;
public function getPageAndSize($data){
$this->page = !empty($data['page']) ? $data['page'] : 一;
$this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
$this->from = ($this->page - 一) * $this->size;}
<?php/**
* Created by PhpStorm.
* User: tong
* Date: 二0一七/一一/二三
* Time: 一七:0三
*/namespace app\api\controller\v一;use app\api\controller\Co妹妹on;class News extends Co妹妹on{
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(一, 'OK', $result, 二00);
}}
转自:https://www.cnblogs.com/yunbao/p/15353415.html
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv2924