├── LICENSE ├── README.html ├── README.md ├── _config.yml └── app ├── Http ├── Controllers │ └── Api │ │ └── RouterController.php └── routes.php ├── Models └── App.php └── Services └── ApiServer ├── App.php ├── Error.php ├── Response ├── BaseResponse.php ├── Demo.php └── InterfaceResponse.php └── Server.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 叶子坑(http://flc.ren | http://flc.io) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.html: -------------------------------------------------------------------------------- 1 | README

API服务端架构代码

1081 |
1082 | 1109 |
1110 |

1. 部署说明

1111 |
1112 |

现有API基于laravel框架开发,本次介绍也针对laravel。可根据文档自行调整,以适用其他框架下使用

1113 |
1114 |

1.1. 数据库相关

1115 |

执行如下SQL语句

1116 |
CREATE TABLE `prefix_apps` (
1117 |   `id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '自增长',
1118 |   `app_id` VARCHAR(60) NOT NULL COMMENT 'appid',
1119 |   `app_secret` VARCHAR(100) NOT NULL COMMENT '密钥',
1120 |   `app_name` VARCHAR(200) NOT NULL COMMENT 'app名称',
1121 |   `app_desc` TEXT COMMENT '描述',
1122 |   `status` TINYINT(2) DEFAULT '0' COMMENT '生效状态',
1123 |   `created_at` INT(10) NOT NULL DEFAULT '0' COMMENT '创建时间',
1124 |   `updated_at` INT(10) NOT NULL DEFAULT '0' COMMENT '更新时间',
1125 |   PRIMARY KEY (`id`),
1126 |   UNIQUE KEY `app_id` (`app_id`),
1127 |   KEY `status` (`status`)
1128 | ) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='应用表';
1129 | 
1130 | 1131 | 1132 |

1.2. 目录相关

1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1155 | 1156 | 1157 | 1158 | 1159 | 1160 | 1161 | 1162 |
标题路径
API核心目录app/Services/ApiServer/
API接口目录app/Services/ApiServer/Response/
apps数据库模型app/Models/App.php
路由配置app/Http/routes.php
API入口控制器app/Http/Controllers/Api/RouterController.php
1163 |

2. API文档及开发规范

1164 |

2.1. API调用协议

1165 |

2.1.1. 请求地址及请求方式

1166 |
1167 |

请求地址:/api/router;

1168 |

请求方式:POST/GET

1169 |
1170 |

2.1.2. 公共参数

1171 | 1172 | 1173 | 1174 | 1175 | 1176 | 1177 | 1178 | 1179 | 1180 | 1181 | 1182 | 1183 | 1184 | 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 | 1217 | 1218 |
参数名类型是否必须描述
app_idstring应用ID
methodstring接口名称
formatstring回调格式,默认:json(目前仅支持)
sign_methodstring签名类型,默认:md5(目前仅支持)
noncestring随机字符串,长度1-32位任意字符
signstring签名字符串,参考签名规则
1219 |

2.1.3. 业务参数

1220 |
1221 |

API调用除了必须包含公共参数外,如果API本身有业务级的参数也必须传入,每个API的业务级参数请考API文档说明。

1222 |
1223 |

2.1.4. 签名规则

1224 | 1230 |

2.1.5. 返回结果

1231 |
// 成功
1232 | {
1233 |     "status": true,
1234 |     "code": "200",
1235 |     "msg": "成功",
1236 |     "data": {
1237 |         "time": "2016-08-02 12:07:09"
1238 |     }
1239 | }
1240 | 
1241 | // 失败
1242 | {
1243 |     "status": false,
1244 |     "code": "1001",
1245 |     "msg": "[app_id]缺失"
1246 | }
1247 | 
1248 | 1249 | 1250 |

2.2. API开发规范

1251 |

2.2.1. API接口命名规范(method)

1252 | 1262 |

2.2.2. 错误码

1263 |
1264 |

错误码配置:app/Services/ApiServer/Error.php

1265 |
1266 |

命名规范:

1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 1278 | 1279 | 1280 | 1281 | 1282 | 1283 | 1284 | 1285 | 1286 | 1287 | 1288 | 1289 | 1290 | 1291 | 1292 |
类型长度说明
系统码3http状态码
公共错误码4公共参数错误相关的错误码
业务错误码6+2位业务码+4位错误码,不足补位
1293 |

现有错误码:

1294 | 1295 | 1296 | 1297 | 1298 | 1299 | 1300 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | 1309 | 1310 | 1311 | 1312 | 1313 | 1314 | 1315 | 1316 | 1317 | 1318 | 1319 | 1320 | 1321 | 1322 | 1323 | 1324 | 1325 | 1326 | 1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1336 | 1337 | 1338 | 1339 | 1340 | 1341 | 1342 | 1343 | 1344 | 1345 | 1346 | 1347 | 1348 | 1349 | 1350 | 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1357 | 1358 | 1359 | 1360 | 1361 | 1362 | 1363 | 1364 | 1365 | 1366 | 1367 |
错误码错误内容
200成功
400未知错误
401无此权限
500服务器异常
1001[app_id]缺失
1002[app_id]不存在或无权限
1003[method]缺失
1004[format]错误
1005[sign_method]错误
1006[sign]缺失
1007[sign]签名错误
1008[method]方法不存在
1009run方法不存在,请联系管理员
1010[nonce]缺失
1011[nonce]必须为字符串
1012[nonce]长度必须为1-32位
1368 |

2.2.3. API DEMO 示例

1369 |

文件路径:app/Services/ApiServer/Response/Demo.php

-------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # API服务端架构代码 2 | 3 | [![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu) 4 | [![LICENSE](https://img.shields.io/badge/license-Anti%20996-blue.svg)](https://github.com/996icu/996.ICU/blob/master/LICENSE) 5 | 6 | ## 1. 部署说明 7 | 8 | > 现有API基于laravel框架开发,本次介绍也针对laravel。可根据文档自行调整,以适用其他框架下使用 9 | 10 | ### 1.1. 数据库相关 11 | 12 | 执行如下SQL语句 13 | 14 | ```sql 15 | CREATE TABLE `prefix_apps` ( 16 | `id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '自增长', 17 | `app_id` VARCHAR(60) NOT NULL COMMENT 'appid', 18 | `app_secret` VARCHAR(100) NOT NULL COMMENT '密钥', 19 | `app_name` VARCHAR(200) NOT NULL COMMENT 'app名称', 20 | `app_desc` TEXT COMMENT '描述', 21 | `status` TINYINT(2) DEFAULT '0' COMMENT '生效状态', 22 | `created_at` INT(10) NOT NULL DEFAULT '0' COMMENT '创建时间', 23 | `updated_at` INT(10) NOT NULL DEFAULT '0' COMMENT '更新时间', 24 | PRIMARY KEY (`id`), 25 | UNIQUE KEY `app_id` (`app_id`), 26 | KEY `status` (`status`) 27 | ) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='应用表'; 28 | ``` 29 | 30 | ### 1.2. 目录相关 31 | 32 | |标题|路径| 33 | |----|----| 34 | |API核心目录|`app/Services/ApiServer/`| 35 | |API接口目录|`app/Services/ApiServer/Response/`| 36 | |apps数据库模型|`app/Models/App.php`| 37 | |路由配置|`app/Http/routes.php`| 38 | |API入口控制器|`app/Http/Controllers/Api/RouterController.php`| 39 | 40 | ## 2. API文档及开发规范 41 | 42 | ### 2.1. API调用协议 43 | 44 | #### 2.1.1. 请求地址及请求方式 45 | 46 | > 请求地址:`/api/router`; 47 | > 48 | > 请求方式:`POST`/`GET` 49 | 50 | #### 2.1.2. 公共参数 51 | 52 | |参数名|类型|是否必须|描述| 53 | |----|----|----|----| 54 | |app_id|string|是|应用ID| 55 | |method|string|是|接口名称| 56 | |format|string|否|回调格式,默认:json(目前仅支持)| 57 | |sign_method|string|否|签名类型,默认:md5(目前仅支持)| 58 | |nonce|string|是|随机字符串,长度1-32位任意字符| 59 | |sign|string|是|签名字符串,参考[签名规则](#签名规则)| 60 | 61 | #### 2.1.3. 业务参数 62 | 63 | > API调用除了必须包含公共参数外,如果API本身有业务级的参数也必须传入,每个API的业务级参数请考API文档说明。 64 | 65 | #### 2.1.4. 签名规则 66 | 67 | - 对所有API请求参数(包括公共参数和请求参数,但除去`sign`参数),根据参数名称的ASCII码表的顺序排序。如:`foo=1, bar=2, foo_bar=3, foobar=4`排序后的顺序是`bar=2, foo=1, foo_bar=3, foobar=4`。 68 | - 将排序好的参数名和参数值拼装在一起,根据上面的示例得到的结果为:bar2foo1foo_bar3foobar4。 69 | - 把拼装好的字符串采用utf-8编码,使用签名算法对编码后的字节流进行摘要。如果使用`MD5`算法,则需要在拼装的字符串前后加上app的`secret`后,再进行摘要,如:md5(secret+bar2foo1foo_bar3foobar4+secret) 70 | - 将摘要得到的字节结果使用大写表示 71 | 72 | #### 2.1.5. 返回结果 73 | 74 | ```json 75 | // 成功 76 | { 77 | "status": true, 78 | "code": "200", 79 | "msg": "成功", 80 | "data": { 81 | "time": "2016-08-02 12:07:09" 82 | } 83 | } 84 | 85 | // 失败 86 | { 87 | "status": false, 88 | "code": "1001", 89 | "msg": "[app_id]缺失" 90 | } 91 | ``` 92 | 93 | ### 2.2. API开发规范 94 | 95 | #### 2.2.1. API接口命名规范(method) 96 | 97 | - 接口名称统一小写字母 98 | - 多单词用`.`隔开 99 | - 对应的类文件(目录:`app/Services/ApiServer/Response/`);以接口名去`.`,再首字母大写作为类名及文件名。如接口名:`user.add`,对应的类文件及类名为:`UserAdd` 100 | - 接口命名规范 101 | - 命名字母按功能或模块从大到小划分,依次编写;如后台用户修改密码:'admin.user.password.update' 102 | - 字母最后单词为操作。查询:`get`;新增:`add`;更新:`update`;删除:`delete`;上传:`upload`;等 103 | 104 | #### 2.2.2. 错误码 105 | 106 | > 错误码配置:`app/Services/ApiServer/Error.php` 107 | 108 | 命名规范: 109 | 110 | |类型|长度|说明| 111 | |----|----|----| 112 | |系统码|3|同`http状态码`| 113 | |公共错误码|4|公共参数错误相关的错误码| 114 | |业务错误码|6+|2位业务码+4位错误码,不足补位| 115 | 116 | 现有错误码: 117 | 118 | |错误码|错误内容| 119 | |----|----| 120 | |200|成功| 121 | |400|未知错误| 122 | |401|无此权限| 123 | |500|服务器异常| 124 | |1001|[app_id]缺失| 125 | |1002|[app_id]不存在或无权限| 126 | |1003|[method]缺失| 127 | |1004|[format]错误| 128 | |1005|[sign_method]错误| 129 | |1006|[sign]缺失| 130 | |1007|[sign]签名错误| 131 | |1008|[method]方法不存在| 132 | |1009|run方法不存在,请联系管理员| 133 | |1010|[nonce]缺失| 134 | |1011|[nonce]必须为字符串| 135 | |1012|[nonce]长度必须为1-32位| 136 | 137 | #### 2.2.3. API DEMO 示例 138 | 139 | 文件路径:`app/Services/ApiServer/Response/Demo.php` 140 | 141 | 142 | ## 捐赠 143 | 144 | 如果你觉得本扩展对你有帮助,请捐赠以表支持,谢谢~~ 145 | 146 | 147 | 148 | 149 | 150 | 151 |

微信

支付宝

152 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /app/Http/Controllers/Api/RouterController.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class RouterController extends Controller 13 | { 14 | /** 15 | * API总入口 16 | * @return [type] [description] 17 | */ 18 | public function index() 19 | { 20 | $server = new Server(new Error); 21 | return $server->run(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/Http/routes.php: -------------------------------------------------------------------------------- 1 | 'api', 'namespace' => 'Api'], function(){ 5 | Route::any('router', 'RouterController@index'); // API 入口 6 | }); -------------------------------------------------------------------------------- /app/Models/App.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class App extends Model 13 | { 14 | /** 15 | * 表名 16 | * @var string 17 | */ 18 | protected $table = 'apps'; 19 | 20 | /** 21 | * The attributes that are mass assignable. 22 | * 23 | * @var array 24 | */ 25 | protected $fillable = [ 26 | 'id', 'app_id', 'app_secret', 'app_name', 'app_desc', 'status', 'created_at', 'updated_at' 27 | ]; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /app/Services/ApiServer/App.php: -------------------------------------------------------------------------------- 1 | 12 | * @example App::getInstance('10001')->info(); 13 | */ 14 | class App 15 | { 16 | /** 17 | * appid 18 | * @var [type] 19 | */ 20 | protected $app_id; 21 | 22 | /** 23 | * 缓存key前缀 24 | * @var string 25 | */ 26 | protected $cache_key_prefix = 'api:app:info:'; 27 | 28 | /** 29 | * 初始化 30 | * @param [type] $app_id [description] 31 | */ 32 | public function __construct($app_id) 33 | { 34 | $this->app_id = $app_id; 35 | } 36 | 37 | /** 38 | * 获取当前对象 39 | * @param string $app_id appid 40 | * @return object 41 | */ 42 | public static function getInstance($app_id) 43 | { 44 | static $_instances = []; 45 | 46 | if (array_key_exists($app_id, $_instances)) 47 | return $_instances[$app_id]; 48 | 49 | return $_instances[$app_id] = new self($app_id); 50 | } 51 | 52 | /** 53 | * 获取app信息 54 | * @return AppModel 55 | */ 56 | public function info() 57 | { 58 | $cache_key = $this->cache_key_prefix . $this->app_id; 59 | 60 | if (Cache::has($cache_key)) { 61 | return Cache::get($cache_key); 62 | } 63 | 64 | $app = AppModel::where(['status' => 1, 'app_id' => $this->app_id])->first(); 65 | 66 | if ($app) 67 | Cache::put($cache_key, $app, Carbon::now()->addMinutes(60)); // 写入缓存 68 | 69 | return $app; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/Services/ApiServer/Error.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | class Error 9 | { 10 | /** 11 | * 错误码 12 | * @var [type] 13 | */ 14 | public static $errCodes = [ 15 | // 系统码 16 | '200' => '成功', 17 | '400' => '未知错误', 18 | '401' => '无此权限', 19 | '500' => '服务器异常', 20 | 21 | // 公共错误码 22 | '1001' => '[app_id]缺失', 23 | '1002' => '[app_id]不存在或无权限', 24 | '1003' => '[method]缺失', 25 | '1004' => '[format]错误', 26 | '1005' => '[sign_method]错误', 27 | '1006' => '[sign]缺失', 28 | '1007' => '[sign]签名错误', 29 | '1008' => '[method]方法不存在', 30 | '1009' => 'run方法不存在,请联系管理员', 31 | '1010' => '[nonce]缺失', 32 | '1011' => '[nonce]必须为字符串', 33 | '1012' => '[nonce]长度必须为1-32位', 34 | ]; 35 | 36 | /** 37 | * 返回错误码 38 | * @var string 39 | */ 40 | public static function getError($code = '400', $_ = false) 41 | { 42 | if (! isset(self::$errCodes[$code])) { 43 | $code = '400'; 44 | } 45 | 46 | return ($_ ? "[{$code}]" : '') 47 | . self::$errCodes[$code]; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/Services/ApiServer/Response/BaseResponse.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | abstract class BaseResponse 9 | { 10 | /** 11 | * 接口名称 12 | * 13 | * @var [type] 14 | */ 15 | protected $method; 16 | 17 | /** 18 | * 返回接口名称 19 | * @return string 20 | */ 21 | public function getMethod() 22 | { 23 | return $this->method; 24 | } 25 | } -------------------------------------------------------------------------------- /app/Services/ApiServer/Response/Demo.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | class Demo extends BaseResponse implements InterfaceResponse 9 | { 10 | /** 11 | * 接口名称 12 | * @var string 13 | */ 14 | protected $method = 'demo'; 15 | 16 | /** 17 | * 执行接口 18 | * @param array &$params 请求参数 19 | * @return array 20 | */ 21 | public function run(&$params) 22 | { 23 | return [ 24 | 'status' => true, 25 | 'code' => '200', 26 | 'data' => [ 27 | 'current_time' => date('Y-m-d H:i:s') 28 | ] 29 | ]; 30 | } 31 | } -------------------------------------------------------------------------------- /app/Services/ApiServer/Response/InterfaceResponse.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | Interface InterfaceResponse 9 | { 10 | /** 11 | * 执行接口 12 | * @return array 13 | */ 14 | public function run(&$params); 15 | 16 | /** 17 | * 返回接口名称 18 | * @return string 19 | */ 20 | public function getMethod(); 21 | } -------------------------------------------------------------------------------- /app/Services/ApiServer/Server.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class Server 12 | { 13 | /** 14 | * 请求参数 15 | * @var array 16 | */ 17 | protected $params = []; 18 | 19 | /** 20 | * API请求Method名 21 | * @var string 22 | */ 23 | protected $method; 24 | 25 | /** 26 | * app_id 27 | * @var string 28 | */ 29 | protected $app_id; 30 | 31 | /** 32 | * app_secret 33 | * @var string 34 | */ 35 | protected $app_secret; 36 | 37 | /** 38 | * 回调数据格式 39 | * @var string 40 | */ 41 | protected $format = 'json'; 42 | 43 | /** 44 | * 签名方法 45 | * @var string 46 | */ 47 | protected $sign_method = 'md5'; 48 | 49 | /** 50 | * 是否输出错误码 51 | * @var boolean 52 | */ 53 | protected $error_code_show = false; 54 | 55 | /** 56 | * 初始化 57 | * @param Error $error Error对象 58 | */ 59 | public function __construct(Error $error) 60 | { 61 | $this->params = Request::all(); 62 | $this->error = $error; 63 | } 64 | 65 | /** 66 | * api服务入口执行 67 | * @param Request $request 请求参数 68 | * @return [type] [description] 69 | */ 70 | public function run() 71 | { 72 | // A.1 初步校验 73 | $rules = [ 74 | 'app_id' => 'required', 75 | 'method' => 'required', 76 | 'format' => 'in:,json', 77 | 'sign_method' => 'in:,md5', 78 | 'nonce' => 'required|string|min:1|max:32|', 79 | 'sign' => 'required', 80 | ]; 81 | $messages = [ 82 | 'app_id.required' => '1001', 83 | 'method.required' => '1003', 84 | 'format.in' => '1004', 85 | 'sign_method.in' => '1005', 86 | 'nonce.required' => '1010', 87 | 'nonce.string' => '1011', 88 | 'nonce.min' => '1012', 89 | 'nonce.max' => '1012', 90 | 'sign.required' => '1006' 91 | ]; 92 | 93 | $v = Validator::make($this->params, $rules, $messages); 94 | 95 | if ($v->fails()) { 96 | return $this->response(['status' => false, 'code' => $v->messages()->first()]); 97 | } 98 | 99 | // A.2 赋值对象 100 | $this->format = !empty($this->params['format']) ? $this->params['format'] : $this->format; 101 | $this->sign_method = !empty($this->params['sign_method']) ? $this->params['sign_method'] : $this->sign_method; 102 | $this->app_id = $this->params['app_id']; 103 | $this->method = $this->params['method']; 104 | 105 | // B. appid校验 106 | $app = App::getInstance($this->app_id)->info(); 107 | if (! $app) 108 | return $this->response(['status' => false, 'code' => '1002']); 109 | 110 | $this->app_secret = $app->app_secret; 111 | 112 | // C. 校验签名 113 | $signRes = $this->checkSign($this->params); 114 | if (! $signRes || ! $signRes['status']) { 115 | return $this->response(['status' => false, 'code' => $signRes['code']]); 116 | } 117 | 118 | // D. 校验接口名 119 | // D.1 通过方法名获取类名 120 | $className = self::getClassName($this->method); 121 | 122 | // D.2 判断类名是否存在 123 | $classPath = __NAMESPACE__ . '\\Response\\' . $className; 124 | if (!$className || !class_exists($classPath)) { 125 | return $this->response(['status' => false, 'code' => '1008']); 126 | } 127 | 128 | // D.3 判断方法是否存在 129 | if (! method_exists($classPath, 'run')) { 130 | return $this->response(['status' => false, 'code' => '1009']); 131 | } 132 | 133 | $this->classname = $classPath; 134 | 135 | // E. api接口分发 136 | $class = new $classPath; 137 | return $this->response((array) $class->run($this->params)); 138 | } 139 | 140 | /** 141 | * 校验签名 142 | * @param [type] $params [description] 143 | * @return [type] [description] 144 | */ 145 | protected function checkSign($params) 146 | { 147 | $sign = array_key_exists('sign', $params) ? $params['sign'] : ''; 148 | 149 | if (empty($sign)) 150 | return array('status' => false, 'code' => '1006'); 151 | 152 | unset($params['sign']); 153 | 154 | if ($sign != $this->generateSign($params)) 155 | return array('status' => false, 'code' => '1007'); 156 | 157 | return array('status' => true, 'code' => '200'); 158 | } 159 | 160 | /** 161 | * 生成签名 162 | * @param array $params 待校验签名参数 163 | * @return string|false 164 | */ 165 | protected function generateSign($params) 166 | { 167 | if ($this->sign_method == 'md5') 168 | return $this->generateMd5Sign($params); 169 | 170 | return false; 171 | } 172 | 173 | /** 174 | * md5方式签名 175 | * @param array $params 待签名参数 176 | * @return string 177 | */ 178 | protected function generateMd5Sign($params) 179 | { 180 | ksort($params); 181 | 182 | $tmps = array(); 183 | foreach ($params as $k => $v) { 184 | $tmps[] = $k . $v; 185 | } 186 | 187 | $string = $this->app_secret . implode('', $tmps) . $this->app_secret; 188 | 189 | return strtoupper(md5($string)); 190 | } 191 | 192 | 193 | /** 194 | * 通过方法名转换为对应的类名 195 | * @param string $method 方法名 196 | * @return string|false 197 | */ 198 | protected function getClassName($method) 199 | { 200 | $methods = explode('.', $method); 201 | 202 | if (!is_array($methods)) 203 | return false; 204 | 205 | $tmp = array(); 206 | foreach ($methods as $value) { 207 | $tmp[] = ucwords($value); 208 | } 209 | 210 | $className = implode('', $tmp); 211 | return $className; 212 | } 213 | 214 | /** 215 | * 输出结果 216 | * @param array $result 结果 217 | * @return response 218 | */ 219 | protected function response(array $result) 220 | { 221 | if (! array_key_exists('msg', $result) && array_key_exists('code', $result)) { 222 | $result['msg'] = $this->getError($result['code']); 223 | } 224 | 225 | if ($this->format == 'json') { 226 | return response()->json($result); 227 | } 228 | 229 | return false; 230 | } 231 | 232 | /** 233 | * 返回错误内容 234 | * @param string $code 错误码 235 | * @return string 236 | */ 237 | protected function getError($code) 238 | { 239 | return $this->error->getError($code, $this->error_code_show); 240 | } 241 | } 242 | --------------------------------------------------------------------------------