Commit a5a68374 authored by hetengji's avatar hetengji

第一次提交

parents
.DS_Store
phpunit.phar
/vendor
composer.phar
composer.lock
*.project
.idea/
.php_cs.cache
\ No newline at end of file
MIT License
Copyright (c) 2018 hetengji
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
规范后端 通用返回码
```php
/**
* Notes: 统一数据返回格式
* author 何腾骥
* Date: 2019/8/8
* Time: 10:14
* @param int $code
* @param string $msg
* @param array $data
* @return \Illuminate\Http\JsonResponse
*/
public function buildResponse($code = 200, $msg = '操作成功', $data=[],$jsonp='') {
$res = array(
"meta" => array("code"=>$code,"message"=>$msg,"callback"=>"bamaso"),
"data" => $data
);
if (!$jsonp) {
return response()->json($res);
} else {
return response()->json($res)->setCallback($jsonp);
}
}
/**
* Notes:课程首页信息获取
* author 王金月
* Date: 2019/8/14
* Time: 11:32
* @return \Illuminate\Http\JsonResponse
*/
public function course()
{
$data = $this->course->course();
if ($data) {
return $this->buildResponse(ReturnCode::SUCCESS, ReturnCode::errorCode(ReturnCode::SUCCESS), $data);
}
return $this->buildResponse(ReturnCode::NOT_EXISTS , ReturnCode::errorCode(ReturnCode::NOT_EXISTS), '');
}
```
\ No newline at end of file
<?php
/**
* 规范后端 通用返回码
* Class ReturnCode
*
*/
class ReturnCode {
const SUCCESS = 1;
const INVALID = -1;
const DB_SAVE_ERROR = -2;
const DB_READ_ERROR = -3;
const CACHE_SAVE_ERROR = -4;
const CACHE_READ_ERROR = -5;
const FILE_SAVE_ERROR = -6;
const LOGIN_ERROR = -7;
const REGISTER_ERROR = -70;
const NOT_EXISTS = -8;
const JSON_PARSE_FAIL = -9;
const TYPE_ERROR = -10;
const NUMBER_MATCH_ERROR = -11;
const EMPTY_PARAMS = -12;
const DATA_EXISTS = -13;
const AUTH_ERROR = -14;
const OTHER_LOGIN = -16;
const VERSION_INVALID = -17;
const CURL_ERROR = -18;
const RECORD_NOT_FOUND = -19;
const DELETE_FAILED = -20;
const ADD_FAILED = -21;
const UPDATE_FAILED = -22;
const PARAM_INVALID = -100;
const VIP= -200;
const ACCESS_TOKEN_TIMEOUT = -400;//身份令牌过期
const SESSION_TIMEOUT = -600;
const UNKNOWN = -700;
const EXCEPTION = -800;
const SEND_OFTEN = -900;
public static function errorCode($code) {
$errorInfo = array(
ReturnCode::SUCCESS => '请求成功',
ReturnCode::INVALID => '非法操作',
ReturnCode::DB_SAVE_ERROR => '数据存储失败',
ReturnCode::DB_READ_ERROR => '数据读取失败',
ReturnCode::CACHE_SAVE_ERROR => '缓存存储失败',
ReturnCode::CACHE_READ_ERROR => '缓存读取失败',
ReturnCode::FILE_SAVE_ERROR => '文件读取失败',
ReturnCode::LOGIN_ERROR => '登录失败',
ReturnCode::REGISTER_ERROR => '注册失败',
ReturnCode::NOT_EXISTS => '不存在',
ReturnCode::JSON_PARSE_FAIL => 'JSON数据格式错误',
ReturnCode::TYPE_ERROR => '类型错误',
ReturnCode::NUMBER_MATCH_ERROR => '数字匹配失败',
ReturnCode::EMPTY_PARAMS => '丢失必要数据',
ReturnCode::DATA_EXISTS => '数据已经存在',
ReturnCode::AUTH_ERROR => '权限认证失败',
ReturnCode::OTHER_LOGIN => '别的终端登录',
ReturnCode::VERSION_INVALID => 'API版本非法',
ReturnCode::CURL_ERROR => 'CURL操作异常',
ReturnCode::RECORD_NOT_FOUND => '记录未找到',
ReturnCode::DELETE_FAILED => '删除失败',
ReturnCode::ADD_FAILED => '添加记录失败',
ReturnCode::UPDATE_FAILED => '更新记录失败',
ReturnCode::PARAM_INVALID => '参数非法',
ReturnCode::ACCESS_TOKEN_TIMEOUT => '身份令牌过期',
ReturnCode::SESSION_TIMEOUT => 'SESSION过期',
ReturnCode::UNKNOWN => '未知错误',
ReturnCode::EXCEPTION => '系统异常',
ReturnCode::SEND_OFTEN => '发送频繁',
ReturnCode::VIP => '非会员',
);
return isset($errorInfo[$code]) ? $errorInfo[$code] : $code;
}
}
\ No newline at end of file
{
"name": "xueyoubang/return-code",
"description": "规范后端 通用返回码",
"type": "libaray",
"license": "MIT",
"authors": [
{
"name": "htj",
"email": "hetengji@bamasoso.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=7.0"
},
"autoload": {
"psr-0": {"ReturnCode": ""}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment