##版本升级接口开发及演示
学习要点:
1. 掌握如何封装 check 方法;
2. check 方法用于检测最新版本信息!
接口传递参数:
|||
|---|---|---|
| app_id | 客户端ID1、安卓pad |
| version_id | 版本号 |
| did | 客户端设备号 |
| version_mini | 小版本号 |
| encrypt_did | 加密后的 did 串 |
基本参数传递方式和获取方法
| 方式 | 获取 |
|---|---|
| get | $_GET |
| post | $_POST |
| header头 | $_SERVER |
$_SERVER 是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script location)等信息的数组
###**检测APP发送数据**
*模拟APP发送POST请求页面:/var/www/app/init.html*
~~~html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="http://192.168.2.110/app/init.php?format=xml" method="post" accept-charset="utf-8">
设备号:<input type="text" value="" name="did" /> <br />
版本号:<input type="text" value="" name="version_id" /> <br />
小版本号:<input type="text" value="" name="version_mini" /> <br />
APP类型:<input type="text" value="" name="app_id" /> <br />
encrypt_did:<input type="text" value="c39f07bf54425745d642498395ce144c" name="encrypt_did" /> <br />
<input type="submit" value="提交">
</form>
</body>
</html>
~~~
*获取POST请求数据页面:/var/www/app/init.php*
~~~php
/**
* 处理版本升级
*/
require_once('./common.php');
class Init extends Common {
public function index() {
$this->check();
}
}
$init = new Init();
$init->index();
~~~
*处理接口公共业务:/var/www/app/common.php*
~~~php
require_once('./response.php');
require_once('./db.php');
/**
* 处理接口公共业务
*/
class Common {
public $params;
public $app;
public function check() {
$this->params['app_id'] = $appId = isset($_POST['app_id']) ? $_POST['app_id'] : '';
$this->params['version_id'] = $versionId = isset($_POST['version_id']) ? $_POST['version_id'] : '';
$this->params['version_mini'] = $versionMini = isset($_POST['version_mini']) ? $_POST['version_mini'] : '';
$this->params['did'] = $did = isset($_POST['did']) ? $_POST['did'] : '';
$this->params['encrypt_did'] = $encryptDid = isset($_POST['encrypt_did']) ? $_POST['encrypt_did'] : '';
if(!is_numeric($appId) || !is_numeric($versionId)) {
return Response::show(401, '参数不合法');
}
// 判定app是否需要加密
$this->app = $this->getApp($appId);
if (!$this->app) {
return Response::show(402, 'app_id不存在');
}
// 该设备需要加密,并且对设备号did连上加密key进行加密的结果不等于encrypt_did,则没有权限
if($this->app['is_encryption'] && $encryptDid != md5($did . $this->app['key'])) {
return Response::show(403, '没有该权限');
}
}
// 获取设备信息
public function getApp($id) {
$sql = "select * from `app` where id = " . $id . " and status = 1 limit 1";
$connect = Db::getInstance()->connect();
$result = mysql_query($sql, $connect);
return mysql_fetch_assoc($result);
}
}
~~~
###**判断是否升级APP**
*common类里增加获取版本信息的方法*
```
// 获取版本信息
public function getversionUpgrade($appId) {
$sql = "select * from `version_upgrade` where app_id = " . $appId . " and status = 1 limit 1";
$connect = Db::getInstance()->connect();
$result = mysql_query($sql, $connect);
return mysql_fetch_assoc($result);
}
```
*修改init.php首页文件*
```
require_once('./common.php');
class Init extends Common {
public function index() {
$this->check();
// 获取版本升级信息
$versionUpgrade = $this->getversionUpgrade($this->app['id']);
if ($versionUpgrade) {
// 要升级 并且 当前版本号小于要升级的版本号
if ($versionUpgrade['type'] && $this->params['version_id'] < $versionUpgrade['version_id']) {
// 要升级
$versionUpgrade['is_upload'] = $versionUpgrade['type'];
} else {
// 不升级
$versionUpgrade['is_upload'] = 0;
}
return Response::show(200, '版本升级信息获取成功', $versionUpgrade);
} else {
return Response::show(400, '版本升级信息获取失败');
}
}
}
$init = new Init();
$init->index();
```