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::get('api/:ver/init','api/:ver.index/init');
Route::resource('api/:ver/news', 'api/:ver.news');
Route::get('api/:ver/rank','api/:ver.rank/index');
Route::get('api/:ver/init','api/:ver.index/init');
Common.php(Controller)
$this->headers=$headers;
Version.php(Model)
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/27
* Time: 10:06
*/
namespace app\common\model;
class Version extends Base
{
/**
* 通过apptype获取最后一条版本内容
* @param string $appType
*/
public function getLastNormalVersionByAppType($appType = '')
{
$data = [
'status' => 1,
'app_type' => $appType,
];
$order = [
'id' => 'desc',
];
return $this->where($data)
->order($order)
->limit(1)
->find();
}
}
Index.php(Controller)
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/23
* Time: 15:48
*/
namespace app\api\controller\v1;
use app\api\controller\Common;
use app\common\lib\exception\ApiException;
class Index extends Common
{
public function index()
{
$heads = model('News')->getIndexHadNormalNews();
$heads = $this->getDealNews($heads);
$positions = model('News')->getPositionNormalNews();
$positions = $this->getDealNews($positions);
$result = [
'heads' => $heads,
'positions' => $positions,
];
return show(config('code.success'), 'OK', $result, 200);
}
/**
* 客户端初始化接口
* 1.检测APP是否需要升级
*
*/
public function init()
{
//app_type 去ent_version 查询
$version = model('Version')->
getLastNormalVersionByAppType($this->headers['app_type']);
//halt($version);
if (empty($version)) {
return new ApiException('error', 404);
}
if ($version->version > $this->headers['version']) {
$version->is_update = $version->is_force == 1 ? 2 : 1;
} else {
$version->is_update = 0; //0不更新 1 需要更新 2强制更新
}
return show(config('code.success'), 'OK', $version, 200);
}
}