PHP 开发 APP 接口--版本升级接口

判定 app 是否需要加密:通过 app 表中的 status 字段来判定,加密的字符串为 app 表中的 key 字段。

在获取的客户端和服务器端(数据库表中相应字段)的版本号不一致时,返回 data['is_upload'] = 1 提示是否升级;一致时返回 data['is_upload'] = 0 不提示升级。

common.php 中 getApp 方法可以添加缓存(静态/Redis等)缓存。

 

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(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);
    }
    
    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);
    }
    
    /**
     * 根据图片大小组装相应图片
     * @param string $imageUrl
     * @param string $size
     */
    public function setImage($imageUrl, $size) {
        if(!$imageUrl) {
            return '';
        }
        if(!$size) {
            return $imageUrl;
        }
        
        $type = substr($imageUrl, strrpos($imageUrl, '.'));
        if(!$type) {
            return '';
        }
        $path = substr($imageUrl, 0, strrpos($imageUrl, '.'));
        
        return $path . '_' . $size . $type;
    }
}
复制代码

 

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 {
                $versionUpgrade['is_upload'] = 0;
            }
            return Response::show(200, '版本升级信息获取成功', $versionUpgrade);
        } else {
            return Response::show(400, '版本升级信息获取失败');
        }
    }
}

$init = new Init();
$init->index();
复制代码

 

init.html 测试接口

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="init.php" method="post">
        APP类型:<input type="text" name='app_id'>
        版本号:<input type="text" name='version_id'>
        <input type="submit" value='提交'>
    </form>
</body>
</html>
复制代码
posted @ 2018-11-23 14:35  L1230205  阅读(1257)  评论(0编辑  收藏  举报