diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 120b236..784e94b 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api; use App\Models\User; +use App\Transformers\UserTransformer; use Illuminate\Http\Request; use App\Http\Requests\Api\UserRequest; @@ -30,6 +31,17 @@ public function store ( UserRequest $request ) // 清除验证码缓存 \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..edd4aaa --- /dev/null +++ b/app/Transformers/UserTransformer.php @@ -0,0 +1,25 @@ + $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->toDateTimeString () , + 'created_at' => $user->created_at->toDateTimeString () , + 'updated_at' => $user->updated_at->toDateTimeString () , + ]; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index fe7363a..77bb0c4 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,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 890258f..5175e08 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "2bda9ade989bb5ebb1a8d46e9323fc39", + "content-hash": "07480550a7ab1d8b10962d497d74dacb", "packages": [ { "name": "cakephp/chronos", @@ -1993,6 +1993,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://files.phpcomposer.com/files/liyu001989/dingo-serializer-switch/82e23a7c9f46f7ec05ae9b5999c618f5c21a3290.zip", + "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.1.8", diff --git a/config/api.php b/config/api.php index d9f9c57..b4c060c 100644 --- a/config/api.php +++ b/config/api.php @@ -169,7 +169,7 @@ */ 'auth' => [ - + 'jwt' => 'Dingo\Api\Auth\Provider\JWT', ] , /* diff --git a/config/auth.php b/config/auth.php index 3c6abbc..3db714c 100644 --- a/config/auth.php +++ b/config/auth.php @@ -47,9 +47,6 @@ ] , ] , - 'auth' => [ - 'jwt' => 'Dingo\Api\Auth\Provider\JWT' , - ] , /* |-------------------------------------------------------------------------- diff --git a/routes/api.php b/routes/api.php index 50afda8..310a7d6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -16,7 +16,8 @@ $api = app ( 'Dingo\Api\Routing\Router' ); $api->version ( 'v1' , [ - 'namespace' => 'App\Http\Controllers\Api' , + 'namespace' => 'App\Http\Controllers\Api' , + 'middleware' => 'serializer:array' ] , function ( $api ) { $api->group ( [ @@ -51,6 +52,21 @@ // 删除token $api->delete ( 'authorizations/current' , 'AuthorizationsController@destroy' ) ->name ( 'api.authorizations.destroy' ); + } ); + + + $api->group ( [ + 'middleware' => 'api.throttle' , + 'limit' => config ( 'api.rate_limits.access.limit' ) , + 'expires' => config ( 'api.rate_limits.access.expires' ) , + ] , function ( $api ) { + // 游客可以访问的接口 + // 需要 token 验证的接口 + $api->group ( [ 'middleware' => 'api.auth' ] , function ( $api ) { + // 当前登录用户信息 + $api->get ( 'user' , 'UsersController@me' ) + ->name ( 'api.user.show' ); + } ); } ); } ); diff --git a/storage/framework/cache/data/85/5f/855f92484c8c414d36c1b25cb24876e30229cbbf b/storage/framework/cache/data/85/5f/855f92484c8c414d36c1b25cb24876e30229cbbf deleted file mode 100644 index a96cbed..0000000 Binary files a/storage/framework/cache/data/85/5f/855f92484c8c414d36c1b25cb24876e30229cbbf and /dev/null differ diff --git a/storage/framework/cache/data/f0/29/f02945dbe518bf3b79c1267d9959c48574b2ebfb b/storage/framework/cache/data/f0/29/f02945dbe518bf3b79c1267d9959c48574b2ebfb deleted file mode 100644 index 3372e5a..0000000 Binary files a/storage/framework/cache/data/f0/29/f02945dbe518bf3b79c1267d9959c48574b2ebfb and /dev/null differ diff --git a/storage/framework/cache/data/f9/47/f947913e0c6993749ff54a26039f2d18c8a26175 b/storage/framework/cache/data/f9/47/f947913e0c6993749ff54a26039f2d18c8a26175 deleted file mode 100644 index f3b33e3..0000000 Binary files a/storage/framework/cache/data/f9/47/f947913e0c6993749ff54a26039f2d18c8a26175 and /dev/null differ diff --git a/storage/framework/views/085b8d01573453f32cedee1224bbb2cc30fcb249.php b/storage/framework/views/085b8d01573453f32cedee1224bbb2cc30fcb249.php deleted file mode 100644 index f99d7f0..0000000 --- a/storage/framework/views/085b8d01573453f32cedee1224bbb2cc30fcb249.php +++ /dev/null @@ -1,48 +0,0 @@ -