[Laravel] 13 - WEB API : update & error tracking
前言
一、大纲
Ref: https://www.imooc.com/video/3134
- 版本升级分析以及数据表设计
- 版本升级分析
- 掌握如何设计版本升级数据表
- 版本升级接口开发以及APP演示
二、数据表设计
| 初始化接口init.php参数 | |
| app_id | 客户端id 1, 安卓pad |
| version_id | 版本号 |
/**
* version_upgrade 版本升级信息表
*/
CREATE TABLE `version_upgrade` (
`id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
`app_id` smallint(4) unsigned NOT NULL DEFAULT '' COMMENT '客户端设备id 1安卓pad 2安卓手机 3ios手机 4iospad',
`version_id` smallint(4) unsigned DEFAULT '' COMMENT '大版本号id',
`version_mini` mediumint(8) unsigned DEFAULT '' 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 '' COMMENT '是否加密 1加密 0不加密',
`key` varchar(20) NOT NULL DEFAULT '' 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 '' COMMENT '状态 1正常 0删除',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
开发首页接口方法
一、数据表
- 客户端 返回 服务器的内容

- 客户端 返回 服务器的方式

不建议使用第一个。header倒是不错。
二、公共类
- common.php
该类之后被init.php使用。
<?php
/**
* 处理接口公共业务
*/
require_once('./response.php');
require_once('./db.php');
class Common {
public $params; # 是个map
public $app;
public function check() { # 有则加入params数组,没则赋值为空
$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, '参数不合法');
} Goto: 测试一
---------------------------------------------------------------------------------
// 先判断app的自带信息
$this->app = $this->getApp($appId);
if(!$this->app) {
return Response::show(402, 'app_id不存在');
} // 再判断app是否需要加密
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;
}
}
- 测试一
在此测试了check函数。
<?php
require_once('./common.php'); class Init extends Common {
public function index() {
$this->check();
}
} ----------------------------
$init = new Init();
$init->index();
三、版本升级
<?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('./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();
[Laravel] 13 - WEB API : update & error tracking的更多相关文章
- [Laravel] 10 - WEB API : wrapper
前言 一.常用的解决方案 React 前端 + PHP (Laravel) 后端 Such as "some exposure to WEB API’s and/or RESTful“. 使 ...
- "Asp.Net Web Api MediaTypeFormatter Error for x-www-formurlencoded data" 解决方法
遇到标题中所说的问题原因是使用 jQuery AJAX 以 POST 方式调用 Asp.Net Web API .解决办法请看以下代码中有注释的部分. public static class WebA ...
- [Laravel] 11 - WEB API : cache & timer
前言 一.资源 Ref: https://www.imooc.com/video/2870 二.缓存 缓存:静态缓存.Memcache.redis缓存 Ref: [Laravel] 09 - Func ...
- [Laravel] 12 - WEB API : cache implement
前言 Ref: https://www.imooc.com/video/2873 服务端如何为客户端(app)的首页提供数据接口, 本篇用此作为例子演示接口的实现. 单例模式 一.三大原则 单例实现 ...
- [Laravel] 14 - REST API: Laravel from scratch
前言 一.基础 Ref: Build a REST API with Laravel API resources Goto: [Node.js] 08 - Web Server and REST AP ...
- ASP.NET Web API 应用教程(一) ——数据流使用
相信已经有很多文章来介绍ASP.Net Web API 技术,本系列文章主要介绍如何使用数据流,HTTPS,以及可扩展的Web API 方面的技术,系列文章主要有三篇内容. 主要内容如下: I 数据 ...
- Web API 数据流使用
ASP.NET Web API 应用教程(一) ——数据流使用 相信已经有很多文章来介绍ASP.Net Web API 技术,本系列文章主要介绍如何使用数据流,HTTPS,以及可扩展的Web AP ...
- ASP.NET Web API 2中的错误处理
前几天在webapi项目中遇到一个问题:Controller构造函数中抛出异常时全局过滤器捕获不到,于是网搜一把写下这篇博客作为总结. HttpResponseException 通常在WebAPI的 ...
- [转]Global exception handling in Web API 2.1 and NLog
本文转自:https://stackoverflow.com/questions/25865610/global-exception-handling-in-web-api-2-1-and-nlog ...
随机推荐
- Microsoft visual Studio2017 中番茄visual assist 破解
百度查下,会发现VS(visual Studio)小番茄的破解有很多,但无外乎两种,第一种是输入注册码的,但是大多数[99%之上]都是无效的key值:还有一种是替换文件的.由于网络给的资料太多,也杂乱 ...
- history.pushState无刷新改变url
通过history.pushState无刷新改变url 背景 在浏览器中改变地址栏url,将会触发页面资源的重新加载,这使得我们可以在不同的页面间进行跳转,得以浏览不同的内容.但随着单页应用的增多,越 ...
- v$instance如何生成
参考:http://www.itpub.net/thread-1284858-1-1.html 1.ORACLE 先创建的x$ 表即RDBMS的内部表 2.然后在X$表的基础上创建了GV$ 视图. ...
- 关于ADO.Net SqlConnection的性能优化
Connections Database connections are an expensive and limited resource. Your approach to connection ...
- Revit API创建标注NewTag
start ; ) { eId = item; } ...
- android:四种基本布局
一个丰富的界面总是要由很多个控件组成的,那我们如何才能让各个控件都有条不紊地 摆放在界面上,而不是乱糟糟的呢?这就需要借助布局来实现了.布局是一种可用于放置很 多控件的容器,它可以按照一定的规律调整内 ...
- JAVA在Windows使用apache commons-csv导出CSV解决方案
一.添加依赖到pom.xml <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependenc ...
- SpringBootWEB项目和非Web项目的全局异常捕获
一.简介 SpringBoot的WEB异常捕获,如果是WEB项目的话,可以直接处理Controller中的异常.如果不是WEB项目的话,就需要使用AspectJ来做切面. 二.WEB项目 packag ...
- CentOS6.9下安装python notebook
操作系统:CentOS6.9_x64 python版本 : python2.7.13 添加低权限新用户: useradd mike su mike 使用virtualenv安装python2.7环境 ...
- Redis系列--内存淘汰机制(含单机版内存优化建议)
https://blog.csdn.net/Jack__Frost/article/details/72478400?locationNum=13&fps=1 每台redis的服务器的内存都是 ...