-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ea8c5b6
Showing
42 changed files
with
1,983 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
composer.lock | ||
/vendor | ||
.DS_Store | ||
*/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Copyright (c) 2017 陈泽韦 | ||
|
||
e-mail: [email protected] [email protected] | ||
website: http://www.zewail.me/ | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
![Packagist Pre Release](https://img.shields.io/badge/packagist-v1.0.0--beta3-orange.svg) | ||
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/czewail/thinp-api/master/LICENSE) | ||
|
||
thinp-api 是给开发者提供的一套针对thinkphp的API扩展工具,帮助开发者方便快捷的建造自己的API应用。 | ||
|
||
**该包是针对thinkphp5.1以上版本** | ||
|
||
### 功能 | ||
|
||
这个包提供了以下等工具: | ||
- API版本管理 | ||
- 响应生成器 | ||
- 数据过滤器 | ||
- JWT(Json Web Token)的支持 | ||
|
||
### 文档 | ||
更多信息请参考 [Wiki](https://github.com/czewail/thinp-api/wiki) 文档 | ||
|
||
### 授权协议 | ||
|
||
本包使用的是 [MIT license](LICENSE) | ||
|
||
### 最后 | ||
|
||
欢迎Star和Fork该项目😄😄😄😄 | ||
|
||
欢迎贡献代码😄😄😄 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "zewail/think-api", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "陈泽韦", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Zewail\\Api\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
return [ | ||
// api 默认版本号 | ||
'version' => 'v1', | ||
// 可选 DataArray, Array | ||
'serializer' => 'DataArray', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
return [ | ||
// 加密算法 | ||
'algorithm' => 'HS256', | ||
// HMAC算法使用的加密字符串 | ||
'key' => 'ex-key', | ||
// RSA算法使用的私钥文件路径 | ||
'privateKeyPath' => '/home/rsa_private_key.pem', | ||
// RSA算法使用的公钥文件路径 | ||
'publicKeyPath' => '/home/rsa_public_key.pem', | ||
// 误差时间,单位秒 | ||
'deviation' => 60, | ||
// 过期时间, 单位分钟 | ||
'ttl' => 120, | ||
// 用户模型路径 | ||
'user' => app\index\model\User::class, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
return [ | ||
// 'user.onlyname' => ['id', 'name'] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
namespace Zewail\Api; | ||
|
||
use Zewail\Api\Response\Factory as ResponseFactory; | ||
use Zewail\Api\JWT\Factory as JWTFactory; | ||
use think\Config; | ||
|
||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
trait Api | ||
{ | ||
|
||
protected $response; | ||
protected $jwt; | ||
|
||
function __construct() | ||
{ | ||
$this->init(); | ||
} | ||
|
||
protected function init() { | ||
$this->response = new ResponseFactory; | ||
$this->jwt = new JWTFactory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
namespace Zewail\Api\Exceptions; | ||
|
||
use think\exception\Handle; | ||
use Zewail\Api\Http\Response; | ||
use Exception; | ||
|
||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class handleException extends Handle | ||
{ | ||
/** | ||
* render | ||
*/ | ||
public function render(Exception $e) | ||
{ | ||
// Token 授权失败的异常 | ||
if ($e instanceof TokenExpiredException || $e instanceof TokenInvalidException || $e instanceof UnauthenticateException) { | ||
return new Response(['message' => $e->getMessage(), 'status_code' => 401], 401); | ||
} | ||
|
||
if ($e instanceof JWTException) { | ||
return new Response(['message' => $e->getMessage(), 'status_code' => 500], 500); | ||
} | ||
|
||
// http状态码异常 | ||
if ($e instanceof ResponseException) { | ||
return new Response(['message' => $e->getMessage(), 'status_code' => $e->getStatusCode()], $e->getStatusCode()); | ||
} | ||
// 其他错误交给系统处理 | ||
return parent::render($e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace Zewail\Api\Exceptions; | ||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class JWTException extends \Exception | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace Zewail\Api\Exceptions; | ||
|
||
use think\exception\HttpException; | ||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class ResponseException extends HttpException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace Zewail\Api\Exceptions; | ||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class TokenExpiredException extends JWTException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace Zewail\Api\Exceptions; | ||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class TokenInvalidException extends JWTException | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace Zewail\Api\Exceptions; | ||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class TokenNotBeforeException extends JWTException | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
namespace Zewail\Api\Exceptions; | ||
|
||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class TypeErrorException extends \Exception | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
namespace Zewail\Api\Exceptions; | ||
|
||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class UnauthenticateException extends JWTException | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace Zewail\Api\Facades; | ||
|
||
use think\Facade; | ||
|
||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class ApiRoute extends Facade | ||
{ | ||
protected static function getFacadeClass() | ||
{ | ||
return 'Zewail\Api\Routing\Router'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace Zewail\Api\Facades; | ||
|
||
use think\Facade; | ||
|
||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class JWT extends Facade | ||
{ | ||
protected static function getFacadeClass() | ||
{ | ||
return 'Zewail\Api\JWT\Factory'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace Zewail\Api\Facades; | ||
|
||
use think\Facade; | ||
|
||
/** | ||
* @author Chan Zewail <[email protected]> | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/czewail/think-api | ||
*/ | ||
class Response extends Facade | ||
{ | ||
protected static function getFacadeClass() | ||
{ | ||
return 'Zewail\Api\Response\Factory'; | ||
} | ||
} |
Oops, something went wrong.