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 @@ -
-
- - 新建帖子 - -
-
- - -
-
- -
活跃用户
-
- addLoop($__currentLoopData); foreach($__currentLoopData as $active_user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> - -
- -
- -
- name); ?> -
-
- popLoop(); $loop = $__env->getLastLoop(); ?> - -
-
- - - -
-
- -
资源推荐
-
- addLoop($__currentLoopData); foreach($__currentLoopData as $link): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> - -
- title); ?> -
-
- popLoop(); $loop = $__env->getLastLoop(); ?> - -
-
- \ No newline at end of file diff --git a/storage/framework/views/710bcf0e3348cb9f7a5eb864aea95996ac7ad282.php b/storage/framework/views/710bcf0e3348cb9f7a5eb864aea95996ac7ad282.php deleted file mode 100644 index 11c51d0..0000000 --- a/storage/framework/views/710bcf0e3348cb9f7a5eb864aea95996ac7ad282.php +++ /dev/null @@ -1,109 +0,0 @@ - \ No newline at end of file diff --git a/storage/framework/views/745d1ba2b20ccd7d42b2919201148f1e01c28d73.php b/storage/framework/views/745d1ba2b20ccd7d42b2919201148f1e01c28d73.php deleted file mode 100644 index 3f67426..0000000 --- a/storage/framework/views/745d1ba2b20ccd7d42b2919201148f1e01c28d73.php +++ /dev/null @@ -1,10 +0,0 @@ - \ No newline at end of file diff --git a/storage/framework/views/7a42c9b1c8e24f8d022bdd55b1498079b24d9021.php b/storage/framework/views/7a42c9b1c8e24f8d022bdd55b1498079b24d9021.php deleted file mode 100644 index 9180020..0000000 --- a/storage/framework/views/7a42c9b1c8e24f8d022bdd55b1498079b24d9021.php +++ /dev/null @@ -1,39 +0,0 @@ -startSection('title', isset($category) ? $category->name : '话题列表'); ?> - -startSection('content'); ?> - -
-
- - - - -
- - - -
- - make('topics._topic_list', ['topics' => $topics], array_except(get_defined_vars(), array('__data', '__path')))->render(); ?> - - render(); ?> - -
-
-
- - -
- -stopSection(); ?> -make('layouts.app', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?> \ No newline at end of file diff --git a/storage/framework/views/a2d06ac739ddc554edcfca8cd4f2da4a4e7fd592.php b/storage/framework/views/a2d06ac739ddc554edcfca8cd4f2da4a4e7fd592.php deleted file mode 100644 index 9abdd02..0000000 --- a/storage/framework/views/a2d06ac739ddc554edcfca8cd4f2da4a4e7fd592.php +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - <?php echo $__env->yieldContent('title', 'LaraBBS'); ?> - <?php echo e(setting('site_name', 'Laravel 进阶教程')); ?> - - - - - - yieldContent('styles'); ?> - - - -
- - make('layouts._header', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?> - -
- - make('layouts._message', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?> - yieldContent('content'); ?> - -
- - make('layouts._footer', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?> -
- -isLocal()): ?> - make('sudosu::user-selector', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?> - - - -yieldContent('scripts'); ?> - - \ No newline at end of file diff --git a/storage/framework/views/a4d300dc63ebddaa0de643e64d30719314714153.php b/storage/framework/views/a4d300dc63ebddaa0de643e64d30719314714153.php deleted file mode 100644 index 677de30..0000000 --- a/storage/framework/views/a4d300dc63ebddaa0de643e64d30719314714153.php +++ /dev/null @@ -1,36 +0,0 @@ -hasPages()): ?> - - diff --git a/storage/framework/views/ae8ea148df480243f7f156463dfce0d226b068b4.php b/storage/framework/views/ae8ea148df480243f7f156463dfce0d226b068b4.php deleted file mode 100644 index 9589a30..0000000 --- a/storage/framework/views/ae8ea148df480243f7f156463dfce0d226b068b4.php +++ /dev/null @@ -1,23 +0,0 @@ - -
- - - -
- - - -
- - - -
- - - -
- - - -
- \ No newline at end of file diff --git a/storage/framework/views/c6504c820c85bb948940feaab9d0a258481cec51.php b/storage/framework/views/c6504c820c85bb948940feaab9d0a258481cec51.php deleted file mode 100644 index 7c75ab4..0000000 --- a/storage/framework/views/c6504c820c85bb948940feaab9d0a258481cec51.php +++ /dev/null @@ -1,55 +0,0 @@ - - - - - -
暂无数据 ~_~
- \ No newline at end of file diff --git a/storage/framework/views/c9420f35e4b2ff33d7b923435c423d7a53c9bff7.php b/storage/framework/views/c9420f35e4b2ff33d7b923435c423d7a53c9bff7.php deleted file mode 100644 index 2997936..0000000 --- a/storage/framework/views/c9420f35e4b2ff33d7b923435c423d7a53c9bff7.php +++ /dev/null @@ -1,50 +0,0 @@ - - - -
-
- -
- - -
- - \ No newline at end of file diff --git a/storage/framework/views/f1ef49e9deab90c7f79e4f2472317be608f1d16a.php b/storage/framework/views/f1ef49e9deab90c7f79e4f2472317be608f1d16a.php deleted file mode 100644 index f47ef8f..0000000 --- a/storage/framework/views/f1ef49e9deab90c7f79e4f2472317be608f1d16a.php +++ /dev/null @@ -1,43 +0,0 @@ -
- addLoop($__currentLoopData); foreach($__currentLoopData as $index => $reply): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> -
-
- - <?php echo e($reply->user->name); ?> - -
- -
-
- - user->name); ?> - - - - created_at->diffForHumans()); ?> - - - check('destroy', $reply)): ?> - -
- - - - - -
-
- -
-
- content; ?> - -
-
-
-
- popLoop(); $loop = $__env->getLastLoop(); ?> -
\ No newline at end of file diff --git a/storage/framework/views/f376c5978be7cfb7fbcc4c290a418d4bd0b41662.php b/storage/framework/views/f376c5978be7cfb7fbcc4c290a418d4bd0b41662.php deleted file mode 100644 index ec2c02c..0000000 --- a/storage/framework/views/f376c5978be7cfb7fbcc4c290a418d4bd0b41662.php +++ /dev/null @@ -1,83 +0,0 @@ -startSection('title', $topic->title); ?> -startSection('description', $topic->excerpt); ?> - -startSection('content'); ?> - -
- - - -
-
-
-

- title); ?> - -

- - - -
- body; ?> - -
- - check('update', $topic)): ?> -
-
- - 编辑 - - -
- - - - - -
-
- - -
-
- -
-
- renderWhen(Auth::check(), 'topics._reply_box', ['topic' => $topic], array_except(get_defined_vars(), array('__data', '__path'))); ?> - make('topics._reply_list', ['replies' => $topic->replies()->with('user')->get()], array_except(get_defined_vars(), array('__data', '__path')))->render(); ?> -
-
-
-
-stopSection(); ?> -make('layouts.app', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?> \ No newline at end of file