From d86fe14e49d78142fb3bfd968283ed1d56e8b6c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=80=99=E5=AD=A6=E6=9D=B0?= Date: Mon, 19 Mar 2018 16:28:53 +0800 Subject: [PATCH] readme --- README.md | 96 +++++++++++++++++++ ...erviceProvider.php => ServiceProvider.php} | 3 +- src/config/internal_api.php | 36 +++++++ 3 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 README.md rename src/{ServiceProvider/InternalApiServiceProvider.php => ServiceProvider.php} (86%) create mode 100644 src/config/internal_api.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..b4c9442 --- /dev/null +++ b/README.md @@ -0,0 +1,96 @@ +# Internal API Server/Client(PHP版) + + +> 该项目使用 composer 来完成加载 + +执行 +```bash +composer config repositories.php-auth-client vcs git@git.int.haowumc.com:arch/php-internal-api-client.git +composer require arch/php-internal-api-client +``` + + +### 如何使用 + +#### Server + +* 注册中间件 +```PHP +$app->routeMiddleware([ + 'internal' => PdInternalApi\Middleware\InternalApi::class, +]); + +``` +* 增加配置文件:```config/internal_api.php``` +在server数组中为调用方增加 secret。 +``` + 调用方secret + */ + 'server' => [ + // app id => app secret + '{{app name}}' => env('INTERNAL_SERVER_{{app name}}_SECRET'), + ], +]; +``` +* 在项目 .env 文件中增加如下配置 + +``` +INTERNAL_SERVER_{{app name}}_SECRET=323232323 +``` + +* 在路由中启用中间件 +```php +$route->group(['middleware'=>'internal'],function()use($router){ + //这里添加对应路由 +}); +``` + + +#### Client + +* 注册服务 +```PHP +$app->register(PdInternalApi\ServiceProvider::class); +``` +* 增加配置文件:```config/internal_api.php``` +在server数组中为调用方增加 secret。 +``` + [ + /** + * key 为 api 项目的名称。 + * 数组为该系统的配置,由该系统的负责人提供 + */ + '{app name}' => [ + 'base_uri' => env('INTERNAL_CLIENT_{{app name}}_URI', ''), + 'appid' => env('INTERNAL_CLIENT_{{app name}}_APPID', ''), + 'secret' => env('INTERNAL_CLIENT_{{app name}}_SECRET', ''), + 'timeout' => 30, + ], + ], +]; +``` + +* 在项目 .env 文件中增加如下配置 + +``` +INTERNAL_CLIENT_{app name}_URI=http://test.in.haowumc.com/ +INTERNAL_CLIENT_{app name}_APPID=test +INTERNAL_CLIENT_{app name}_SECRET=sdfsdfsdf +``` + +* 调用 +```php +$api = app('internal.api.{{app name}}') +$resp = $api->call('{{URI}}',$params); +``` \ No newline at end of file diff --git a/src/ServiceProvider/InternalApiServiceProvider.php b/src/ServiceProvider.php similarity index 86% rename from src/ServiceProvider/InternalApiServiceProvider.php rename to src/ServiceProvider.php index afbfe0c..34a5dad 100644 --- a/src/ServiceProvider/InternalApiServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,10 +2,9 @@ namespace InternalApi\ServiceProvider; -use Illuminate\Support\ServiceProvider; use InternalApi\Client; -class InternalApiServiceProvider extends ServiceProvider +class ServiceProvider extends Illuminate\Support\ServiceProvider { /** * Register any application services. diff --git a/src/config/internal_api.php b/src/config/internal_api.php new file mode 100644 index 0000000..0f41049 --- /dev/null +++ b/src/config/internal_api.php @@ -0,0 +1,36 @@ + 调用方secret + */ + 'server' => [ + // app id => app secret + 'finance' => 'a249f0e40e31877adedd76fac6c6c116', + 'mall' => '8b54fdc7b317906d93c83152be5f956b', + 'xiaoke' => '8b54fdc7b317906d93c83152be5f956b', + ], + /** + * 配置可以使用的内部系统 + */ + 'client' => [ + /** + * key 为 api 项目的名称。 + * 数组为该系统的配置,由该系统的负责人提供 + */ + 'finance' => [ + 'base_uri' => env('INTERNAL_CLIENT_FINANCE_URI', ''), + 'appid' => env('INTERNAL_CLIENT_FINANCE_APPID', ''), + 'secret' => env('INTERNAL_CLIENT_FINANCE_SECRET', ''), + 'timeout' => 30, + ], + 'mall' => [ + 'base_uri' => env('INTERNAL_CLIENT_MALL_URI', ''), + 'appid' => env('INTERNAL_CLIENT_MALL_APPID', ''), + 'secret' => env('INTERNAL_CLIENT_MALL_SECRET', ''), + 'timeout' => 3, + ], + ], +]; \ No newline at end of file