Skip to content

Commit

Permalink
api/user
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Apr 17, 2018
1 parent eecf18b commit 2d5da6a
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 545 deletions.
14 changes: 13 additions & 1 deletion app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() );
}
}
25 changes: 25 additions & 0 deletions app/Transformers/UserTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Transformers;

use App\Models\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
public function transform ( User $user )
{
return [
'id' => $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 () ,
];
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 44 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
*/

'auth' => [

'jwt' => 'Dingo\Api\Auth\Provider\JWT',
] ,

/*
Expand Down
3 changes: 0 additions & 3 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
] ,
] ,

'auth' => [
'jwt' => 'Dingo\Api\Auth\Provider\JWT' ,
] ,

/*
|--------------------------------------------------------------------------
Expand Down
18 changes: 17 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ( [
Expand Down Expand Up @@ -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' );
} );
} );
} );
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

109 changes: 0 additions & 109 deletions storage/framework/views/710bcf0e3348cb9f7a5eb864aea95996ac7ad282.php

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2d5da6a

Please sign in to comment.