[PHP高可用后端]③⑨--版本升级接口开发1

微信截图_20171127095628.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::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);
    }
}
image.png
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 自己做dede的开发时间也比较长了,基本上常用的函数都知道在哪个文件里面,但是时间一长,也有点模糊了,俗话说:好记...
    大刘的英语世界阅读 1,734评论 1 9
  • 转载自cr180大神DiscuzX2.5完整目录结构【source程序文件库】 /source/admincp后台...
    cndaqiang阅读 879评论 1 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,886评论 18 139
  • 去游乐场游玩有个装酷秘诀,就是无论玩什么都不要大声叫出来。我玩过几次挺刺激的游戏,类似过山车那种,什么上下空...
    无言高高阅读 4,304评论 0 1
  • 上周二坐公交去岳母家,站着看手机,给车甩开,扯到手腕,一直痛。用了红花油,再用跌打止痛膏,依然是痛,尤其是跌打止痛...
    king_zhang阅读 235评论 0 0