From 4c45d9b92ad81d9338426685b7ef314f2bb2f360 Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Sun, 10 Jun 2018 16:39:32 +0800 Subject: [PATCH] =?UTF-8?q?api=E7=BB=9F=E4=B8=80=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/UsersController.php | 14 +++++- app/Transformers/UserTransformer.php | 27 ++++++++++++ composer.json | 1 + composer.lock | 45 +++++++++++++++++++- routes/api.php | 13 +++++- 5 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 app/Transformers/UserTransformer.php diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 93f91d0..49b177c 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api; use App\Http\Requests\UserRequest; use App\Models\User; +use App\TransFormers\UserTransformer; use Illuminate\Support\Facades\Cache; class UsersController extends Controller @@ -21,6 +22,17 @@ class UsersController extends Controller // 清除验证码缓存 \Cache::forget($request->verification_key); - return $this->response->created(); + return $this->response->item($user, new UserTransformer()) + ->setMeta([ + 'access_token' => \Auth::guard('api')->fromUser($user), + 'token_type' => 'Bearer', + 'expires_in' => \Auth::guard('api')->factory()->getTTL() * 60 + ]) + ->setStatusCode(201); + } + + public function me() + { + return $this->response->item($this->user(), new UserTransformer()); } } diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php new file mode 100644 index 0000000..5d1ad1a --- /dev/null +++ b/app/Transformers/UserTransformer.php @@ -0,0 +1,27 @@ + $user->id, + 'name' => $user->name, + 'email' => $user->email, + 'avatar' => $user->avatar, + 'introduction' => $user->introduction, + 'bound_phone' => $user->phone ? true : false, + 'bound_wechat' => ($user->weixin_unionid || $user->weixin_openid) ? true : false, + 'last_actived_at' => $user->last_actived_at instanceof Carbon ? $user->last_actived_at->toDateTimeString() : $user->last_actived_at, + 'created_at' => $user->created_at instanceof Carbon ? $user->created_at->toDateTimeString() : $user->created_at, + 'updated_at' => $user->updated_at instanceof Carbon ? $user->updated_at->toDateTimeString() : $user->updated_at, + ]; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index ba7f336..1c90989 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "laravel/framework": "5.5.*", "laravel/horizon": "~1.0", "laravel/tinker": "~1.0", + "liyu/dingo-serializer-switch": "^0.3.0", "mews/captcha": "~2.0", "mews/purifier": "~2.0", "overtrue/easy-sms": "^1.0", diff --git a/composer.lock b/composer.lock index d5284f4..1704750 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e9a02a045616c7c6a7078c57f181b2f8", + "content-hash": "3e238e73ff229ba2ec8024b3e85640a5", "packages": [ { "name": "cakephp/chronos", @@ -2052,6 +2052,49 @@ ], "time": "2016-08-17T00:36:58+00:00" }, + { + "name": "liyu/dingo-serializer-switch", + "version": "v0.3.0", + "source": { + "type": "git", + "url": "https://github.com/liyu001989/dingo-serializer-switch.git", + "reference": "82e23a7c9f46f7ec05ae9b5999c618f5c21a3290" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/liyu001989/dingo-serializer-switch/zipball/82e23a7c9f46f7ec05ae9b5999c618f5c21a3290", + "reference": "82e23a7c9f46f7ec05ae9b5999c618f5c21a3290", + "shasum": "" + }, + "require": { + "php": ">=7.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Liyu\\Dingo\\ServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Liyu\\Dingo\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Yu Li", + "email": "liyu001989@gmail.com" + } + ], + "description": "A middleware to switch dingo serializer", + "time": "2018-01-06T05:09:05+00:00" + }, { "name": "mews/captcha", "version": "2.2.0", diff --git a/routes/api.php b/routes/api.php index ac1b26b..f691895 100644 --- a/routes/api.php +++ b/routes/api.php @@ -10,9 +10,13 @@ | is assigned the "api" middleware group. Enjoy building your API! | */ + $api = app('Dingo\Api\Routing\Router'); -$api->version('v1', ['namespace' => 'App\Http\Controllers\Api'], function ($api) { +$api->version('v1', [ + 'namespace' => 'App\Http\Controllers\Api', + 'middleware' => 'serializer:array', +], function ($api) { $api->group([ 'middleware' => 'api.throttle', 'limit' => config('api.rate_limits.sign.limit'), @@ -45,7 +49,12 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\Api'], function ($api) ->name('api.authorizations.destroy'); }); - + // 需要 token 验证的接口 + $api->group(['middleware' => 'api.auth'], function ($api) { + // 当前登录用户信息 + $api->get('user', 'UsersController@me') + ->name('api.user.show'); + }); });