一、版本升级分析及数据表设计
学习要点:
1. 版本升级分析
2. 掌握如何设计版本升级数据表
web升级:由开发工程师进行代码的修改上传的操作;
App更新:由用户自行更新版本;
初始化接口 init.php 参数
app_id version_id
- /**
- * version_upgrade 版本升级信息表
- */
- CREATE TABLE `version_upgrade` (
- `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
- `app_id` smallint(4) unsigned NOT NULL DEFAULT '0' COMMENT '客户端设备id 1安卓pad',
- `version_id` smallint(4) unsigned DEFAULT '0' COMMENT '大版本号id',
- `version_mini` mediumint(8) unsigned DEFAULT '0' COMMENT '小版本号',
- `version_code` varchar(10) DEFAULT NULL COMMENT '版本标识 1.2',
- `type` tinyint(2) unsigned DEFAULT NULL COMMENT '是否升级 1升级,0不升级,2强制升级',
- `apk_url` varchar(255) DEFAULT NULL,
- `upgrade_point` varchar(255) DEFAULT NULL COMMENT '升级提示',
- `status` tinyint(1) DEFAULT NULL,
- `create_time` int(11) NOT NULL,
- `update_time` int(11) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
- INSERT INTO `muke`.`version_upgrade` (`id`, `app_id`, `version_id`, `version_mini`, `version_code`, `type`, `apk_url`, `upgrade_point`, `status`, `create_time`, `update_time`) VALUES ('1', '1', '2', '1', '2.1', '1', 'http://imooc.com', '有新功能了,快来更新', '1', '0', '0');
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- /**
- * app表 客户端表
- */
- CREATE TABLE `app` (
- `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
- `name` varchar(10) DEFAULT NULL COMMENT 'APP类型名称 如:安卓手机',
- `is_encryption` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否加密 1加密 0不加密',
- `key` varchar(20) NOT NULL DEFAULT '0' COMMENT '加密key',
- `image_size` text COMMENT '按json_encode存储',
- `create_time` int(11) NOT NULL COMMENT '创建时间',
- `update_time` int(11) NOT NULL COMMENT '更新时间',
- `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态 1正常 0删除',
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
- INSERT INTO `muke`.`app` (`id`, `name`, `is_encryption`, `key`, `image_size`, `create_time`, `update_time`, `status`) VALUES ('1', '安卓pad', '1', 'ss', NULL, '0', '0', '1');
- INSERT INTO `muke`.`app` (`id`, `name`, `is_encryption`, `key`, `image_size`, `create_time`, `update_time`, `status`) VALUES ('2', '安卓手机', '1', 'singwa@126.com', NULL, '0', '0', '1');
- INSERT INTO `muke`.`app` (`id`, `name`, `is_encryption`, `key`, `image_size`, `create_time`, `update_time`, `status`) VALUES ('3', 'iphone', '1', 'iphone', NULL, '0', '0', '1');
- INSERT INTO `muke`.`app` (`id`, `name`, `is_encryption`, `key`, `image_size`, `create_time`, `update_time`, `status`) VALUES ('4', 'ipad', '1', 'ipad&sg2', NULL, '0', '0', '1');
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
二、版本升级接口开发及演示
学习要点:
1. 掌握如何封装 check 方法;
2. check 方法用于检测最新版本信息!
接口传递参数:
app_id version_id did version_mini encrypt_did基本参数传递方式和获取方法
方式 获取 get $_GET post $_POST header头 $_SERVER$_SERVER 是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script location)等信息的数组
检测APP发送数据
模拟APP发送POST请求页面:/var/www/app/init.html
~~~html
版本号:
小版本号:
APP类型:
encrypt_did:
~~~
获取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
- 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);
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
修改init.php首页文件
百度搜索“就爱阅读”,专业资料,生活学习,尽在就爱阅读网92to.com,您的在线图书馆!
- 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();