修改代码
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);
}
$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{
Response::show(400,'版本信息获取失败!');
}
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不存在');
}
if ($this->app['is_encryption'] && $encryptDid != md5($dId . $this->app['key'])) {
return Response::show(402, '没有权限');
}
}
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);
}
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
<?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 {
//is_upload为0表示不用升级
$versionUpgrade['is_upload'] = 0;
}
return Response::show(200, '版本升级信息获取成功!', $versionUpgrade);
} else {
Response::show(400, '版本信息获取失败!');
}
}
}
$init = new Init();
$init->index();
?>
test.html
<html>
<form action="http://127.0.0.1/testapp/init.php" method="post">
设备号:<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>
</html>
response.php
<?php
class Response
{
const JSON = "json";
public static function show($code, $message = '', $data = array(), $type = self::JSON)
{
if (!is_numeric($code)) {
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
$result = array(
'code' => $code,
'message' => $message,
'$data' => $data
);
if ($type == 'json') {
self::json($code, $message, $data);
ecit;
} else if ($type == 'array') {
var_dump($result);
} else if ($type == 'xml') {
self::xmlEncode($code, $message, $data);
} else {
//TODO
}
}
public static function json($code, $message = '', $data = array())
{
if (!is_numeric($code)) {
return "";
} else {
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
$str = json_encode($result);
echo preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $str);
exit;
}
}
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'?>";
$xml .= "<root>";
$xml .= self::xmlToEncode($result);
$xml .= "</root>";
echo $xml;
}
public static function xmlToEncode($data)
{
$xml = $attr = "";
foreach ($data as $key => $value) {
if (is_numeric($key)) {
$attr = "id='{$key}'";
$key = "item ";
}
$xml .= "<{$key}{$attr}>";
$xml .= is_array($value) ? self::xmlToEncode($value) : $value;
$xml .= "</{$key}>";
}
return $xml;
}
}
?>