[PHP開發(fā)APP接口]①④--版本升級接口演示二

修改代碼

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
/*
 * 處理接口公共業(yè)務
 */
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;
    }
}

?>
Paste_Image.png
Paste_Image.png
Paste_Image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容