APP接口开发--版本升级数据表设计

一、版本升级分析及数据表设计

在这里插入图片描述
版本升级信息表

/**
 * 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');

客户端表

/**
 * 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', '[email protected]', 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');

二、版本升级接口开发

接口传递参数:


接口传递参数 Value
app_id 客户端 id 1.安卓pad 2.ios
version_id 版本号
did 客户端设备号
version_mini 小版本号
encrypt_did 加密后的did串

初始化调用接口init.php文件

<?php
require_once("./common.php");
class Init extends Common
{
	public function index()
	{
		$this->check();
		//获取版本信息
		$result = $this->getVersionInfo($this->app['id']);
		if($result){
			//判断客户端版本是否小于系统升级版本
			if($result['type'] && $this->params['version_id'] < $result['version_id']){
				//需要升级,添加字段is_upload
				$result['is_upload'] = 1;
			}else{
				//不需要升级,添加字段is_upload
				$result['is_upload'] = 0;
			}
			Response::show(200,'版本信息获取成功',$result);
		}else{
			Response::show(400,'版本升级信息获取失败');
		}
	}
}

$init = new Init;
$init->index();

公共业务接口common.php

<?php
require_once("./response.php");
require_once("./db.php");
//处理接口公共业务
class Common
{
	public $params;
	public $app;

	public function check()
	{
		//app的id
		$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'] = $versionMiniId = isset($_POST['version_mini']) ? $_POST['version_mini'] : '';
	    $this->params['did'] = $dId = isset($_POST['did']) ? $_POST['did'] : '';
	    //加密串id   == md5($dId.$this->app['key'])
	    $this->params['encrypt_did'] = $encryptid = isset($_POST['encrypt_did']) ? $_POST['encrypt_did'] : '';
	    if(!is_numeric($appId) || !is_numeric($versionId)){
	    	return Response::show(401,'参数不合法');
	    }

	   /* //测试生成的加密id
	    $a = md5('1asd');
	    var_dump($a);exit;*/

	    //判断app是否需要加密  status=1为需要加密
	   	$this->app =  $this->getAppInfo($appId);
	    if(!$this->app){
	    	return Response::show(402,'app_id不存在');
	    }
	    //判断加密id是否相等
	    if($this->app['is_encryption'] && $encryptid != md5($dId.$this->app['key'])){
	    	return Response::show(403,'没有该权限');
	    }
	}

	public function getAppInfo($id)
	{
		$sql = "select * from app where id=".$id." and status =1 limit 1";
	
		//连接数据库
		$db = Db::getInstance()->connect();
		// 查询数据库
		$recordset = $db->query($sql);
		$recordset->setFetchMode(PDO::FETCH_ASSOC);
		$result = $recordset->fetchall();
		foreach ($result as $key ) {			
		}
		return $key;
	}
	//获取版本信息
	public function getVersionInfo($appid)
	{
		$sql = "select * from version_upgrade where app_id=".$appid." and status =1 limit 1";
	
		//连接数据库
		$db = Db::getInstance()->connect();
		// 查询数据库
		$recordset = $db->query($sql);
		$recordset->setFetchMode(PDO::FETCH_ASSOC);
		$result = $recordset->fetchall();
		foreach ($result as $key ) {			
		}
		return $key;
	}
}

封装好的数据库调用类 db.php

<?php

class Db
{
	//拥有一个保护类实例的静态成员变量
	static private $_instance;
	static private $_connectSource;

	const HOST='mysql:host=localhost;dbname=rht-test';
	const USER='root';
	const PASS='root';
	//构造方法需要标记为非 public (防止外部使用new操作符创建对象),单例类不能在其他类中实例化,只能被自身实例化
	private function __construct()
	{

	}
	//拥有一个访问这个实例的公共静态方法
	public static function getInstance()
	{
		//判断变量是否实例化
		if(!(self::$_instance instanceof self)){
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	//防止克隆
	private function __clone()
	{
		trigger_error("Can't clone object",E_USER_ERROR);
	}

	//连接数据库
	public function connect()
	{
		self::$_connectSource = new PDO(Db::HOST,Db::USER,Db::PASS);	
		if(!self::$_connectSource){
            throw new Exception("mysql connect error");
            //die("mysql connect error".mysql_error());
        }
        self::$_connectSource->exec("set names utf8");

        return self::$_connectSource;
	}	

}

// //连接数据库
// $db = Db::getInstance()->connect();
// // 查询数据库
// $recordset = $db->query("select * from sys_role");
// $recordset->setFetchMode(PDO::FETCH_ASSOC);
// $result = $recordset->fetchAll();

//  echo "<pre>";
//  var_dump($result);

封装号好的通信接口类 response.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 */

class Response
{
    const TYPE = 'json';
    /**
     * 按综合方式去封装通信接口
     * @param $code 状态码
     * @param string $message 提示信息
     * @param array $data   返回的数据
     * @param string $type  获取的类型
     */
    public static function show($code,$message='',$data=array(),$type=self::TYPE){
        if(!is_numeric($code)){
            return "";
        }
        $type = isset($_GET['format']) ? $_GET['format'] : self::TYPE;
        $result = array(
            'code'=>$code,
            'message'=>$message,
            'data'>$data
        );
        if($type == 'json'){
            self::json($code,$message,$data);
            exit;
        }elseif($type == 'xml'){
            self::xmlEncode($code,$message,$data);
            exit;
        }elseif($type == 'array'){
            print_r($result);exit;
        }else{
            //todo
        }

    }
    /**
     * 按照json方式去封装接口数据方法
     * @param $code 状态码
     * @param string $message 提示消息
     * @param array $data 返回的数据
     * return string
     */
    public static function json($code, $message = '', $data = array())
    {
        if (!is_numeric($code)) {
            return "";
        }
        $result = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );
        echo json_encode($result);
        exit;
    }

    /**
     * php生成xml数据
     */
    public static function xml()
    {
        header("Content-type:text/xml");
        $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
        $xml .= "<root>\n";
        $xml .= "<code>200</code>\n";
        $xml .= "<message>数据返回成功</message>\n";
        $xml .= "<data>\n";
        $xml .= "<id>1</id>\n";
        $xml .= "<name>caicai</name>\n";
        $xml .= "<desc>this is a test</desc>\n";
        $xml .= "</data>\n";
        $xml .= "</root>";
        echo $xml;
        exit;
    }

    /**
     * 按xml方式去封装通信接口
     * @param $code 状态码
     * @param string $message   提示信息
     * @param array $data 返回的数据
     */
    public static function xmlEncode($code,$message ='',$data=array()){
        if(!is_numeric($code)){
            return '';
        }

        $result = array(
            'code'=>$code,
            'message'=>$message,
            'data'=>$data
        );
        header('Content-type:text/xml');
        $xml = "<?xml version='1.0' encoding='UTF-8' ?>\n";
        $xml .= "<root>";
        $xml .= self::xmlToEncode($result);
        $xml .= "</root>";

        echo $xml;exit;
    }

    public static function xmlToEncode($data){
        $xml = $attr = "";
        foreach($data as $key=>$val){
            if(is_numeric($key)){
                $attr = "id= '{$key}'";
                $key = "item ";
            }
            $xml .= "<{$key} {$attr}>";
            $xml .= is_array($val) ? self::xmlToEncode($val) : $val;
            $xml .= "</{$key}>\n";
        }
        return $xml;
    }
}

$data = array(
    'id'=>1,
    'name'=>'caicai',
//    'desc'=>array(1,3,5,'test'),
);

// Response::show(200,'数据返回成功',$data,'array');

返回结果
在这里插入图片描述
链接: postman工具使用和下载地址

发布了40 篇原创文章 · 获赞 0 · 访问量 668

猜你喜欢

转载自blog.csdn.net/weixin_39218464/article/details/104086930
今日推荐