4. APP 接口实例

时间:2020-12-03 05:04:19

4. APP 接口实例


1.单例模式连接数据库
4. APP 接口实例
4. APP 接口实例

1.构造函数需要为非pulic
2.只能被自身实例化,不能在其他类中实例化
3.拥有一个保存类的实例,静态成员变量$_instance;
3.拥有一个访问这个实例的公共的静态方法
4.私有化克隆方法
<?php class Db { private static $_instance; private static $_connectSource; private $_dbConfig = array( 'host' => 'localhost', 'user' => 'root', 'password' => '123456', 'database' =>'test', ); private function __construct() { } public static function getInstance() { if (!(self::$_instance instanceof self)) { self::$_instance = new self(); } return self::$_instance; } public function connect() { if (!self::$_connectSource) { self::$_connectSource = mysql_connect($this->_dbConfig['host'], $this->_dbConfig['user'], $this->_dbConfig['password']); if (!self::$_connectSource) { die('mysql connect error : ' . mysql_error()); } mysql_select_db($this->_dbConfig['database'], self::$_connectSource); mysql_query("set names UTF8", self::$_connectSource); } return self::$_connectSource; } } $connect = Db::getInstance()->connect(); $sql = "select * from student"; $a = mysql_query($sql, $connect); echo mysql_num_rows($a); var_dump($a); 

2.首页APP接口开发
4. APP 接口实例
4. APP 接口实例
4. APP 接口实例

4. APP 接口实例
4. APP 接口实例
4. APP 接口实例

<?php require_once 'response.php'; require_once 'db.php'; //http://app.com/list.php?page=1&pagesize=12 $page = isset($_GET['page']) ? $_GET['page'] : 1; $pageSize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 1; if (!is_numeric($page) || !is_numeric($pageSize)) { return Response::show(401, '数据不合法', null, 'xml'); } $sql = 'select * from student'; $connect = Db::getInstance()->connect(); $result = mysql_query($sql, $connect); var_dump($result);

4. APP 接口实例
4. APP 接口实例

4. APP 接口实例
4. APP 接口实例


3. APP 版本升级
4. APP 接口实例
4. APP 接口实例
4. APP 接口实例
4. APP 接口实例
4. APP 接口实例
4. APP 接口实例
4. APP 接口实例

4. APP 接口实例
4. APP 接口实例
4. APP 接口实例
4. 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 2安卓手机 3ios手机 4iospad', `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(2) DEFAULT NULL, `create_time` int(11) DEFAULT NULL, `update_time` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

/** * app表 客户端表 */
CREATE TABLE `app` ( `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id', `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;


version表  版本表
<?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();
<?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; } }

4. APP 错误日志
4. APP 接口实例

<?php require_once('./common.php'); class ErrorLog extends Common { public function index() { $this->check(); $errorLog = isset($_POST['error_log']) ? $_POST['error_log'] : ''; if(!$errorLog) { return Response::show(401, '日志为空'); } $sql = "insert into error_log( `app_id`, `did`, `version_id`, `version_mini`, `error_log`, `create_time`) values( ".$this->params['app_id'].", '".$this->params['did']."', ".$this->params['version_id'].", ".$this->params['version_mini'].", '".$errorLog."', ".time()." )"; $connect = Db::getInstance()->connect(); if(mysql_query($sql, $connect)) { return Response::show(200, '错误信息插入成功'); } else { return Response::show(400, '错误信息插入失败'); } } } $error = new ErrorLog(); $error->index();