Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
czewail committed Sep 11, 2018
0 parents commit ea8c5b6
Show file tree
Hide file tree
Showing 42 changed files with 1,983 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.lock
/vendor
.DS_Store
*/.DS_Store
10 changes: 10 additions & 0 deletions LICENSE
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.
28 changes: 28 additions & 0 deletions README.md
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该项目😄😄😄😄

欢迎贡献代码😄😄😄
19 changes: 19 additions & 0 deletions composer.json
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/"
}
}
}
7 changes: 7 additions & 0 deletions config/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
return [
// api 默认版本号
'version' => 'v1',
// 可选 DataArray, Array
'serializer' => 'DataArray',
];
17 changes: 17 additions & 0 deletions config/jwt.php
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,
];
4 changes: 4 additions & 0 deletions config/resources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return [
// 'user.onlyname' => ['id', 'name']
];
28 changes: 28 additions & 0 deletions src/Api.php
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;
}
}
36 changes: 36 additions & 0 deletions src/Exceptions/HandleException.php
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);
}
}
11 changes: 11 additions & 0 deletions src/Exceptions/JWTException.php
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
{
//
}
13 changes: 13 additions & 0 deletions src/Exceptions/ResponseException.php
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
{

}
10 changes: 10 additions & 0 deletions src/Exceptions/TokenExpiredException.php
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
{
}
11 changes: 11 additions & 0 deletions src/Exceptions/TokenInvalidException.php
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
{
//
}
11 changes: 11 additions & 0 deletions src/Exceptions/TokenNotBeforeException.php
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
{
//
}
12 changes: 12 additions & 0 deletions src/Exceptions/TypeErrorException.php
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
{
//
}
12 changes: 12 additions & 0 deletions src/Exceptions/UnauthenticateException.php
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
{
//
}
17 changes: 17 additions & 0 deletions src/Facades/ApiRoute.php
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';
}
}
17 changes: 17 additions & 0 deletions src/Facades/JWT.php
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';
}
}
17 changes: 17 additions & 0 deletions src/Facades/Response.php
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';
}
}
Loading

0 comments on commit ea8c5b6

Please sign in to comment.