On this page
检测更新api
创建相关文件
php
// 创建app更新控制器
php think make:controller api/v1/Update
// 创建app更新模型
php think make:model Update
// 创建app更新验证器
php think make:validate UpdateValidate
controller层:application\api\controller\v1\Update.php
php
<?php
namespace app\api\controller\v1;
use think\Controller;
use think\Request;
use app\common\controller\BaseController;
use app\common\validate\UpdateValidate;
use app\common\model\Update as UpdateModel;
class Update extends BaseController
{
// 检查更新
public function update(){
(new UpdateValidate())->goCheck();
$res = (new UpdateModel())->appUpdate();
return self::showResCode('ok',$res);
}
}
route层:route\route.php
php
// 不需要验证token
Route::group('api/:version/',function(){
// 检测更新
Route::post('update','api/v1.Update/update');
});
validate层:application\common\model\UpdateValidate.php
php
protected $rule = [
'ver'=>'require|NotEmpty'
];
model层:application\common\model\Update.php
php
protected $autoWriteTimestamp = true;
// 检测更新
public function appUpdate(){
$version = request()->param('ver');
$res = $this->where('status',1)->order('create_time','desc')->find();
// 无记录
if (!$res) TApiException('暂无更新版本');
if ( $res['version'] == $version ) TApiException('暂无更新版本');
return $res;
}