Skip to content

Commit

Permalink
用户信息
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed Aug 6, 2020
1 parent 7b2e3cb commit 31f6413
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
10 changes: 10 additions & 0 deletions app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public function store(UserRequest $request)
// 清除验证码缓存
\Cache::forget($request->verification_key);

return (new UserResource($user))->showSensitiveFields();
}

public function show(User $user, Request $request)
{
return new UserResource($user);
}

public function me(Request $request)
{
return (new UserResource($request->user()))->showSensitiveFields();
}
}
26 changes: 19 additions & 7 deletions app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@

class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected $showSensitiveFields = false;

public function toArray($request)
{
return parent::toArray($request);
if (!$this->showSensitiveFields) {
$this->resource->makeHidden(['phone', 'email']);
}

$data = parent::toArray($request);

$data['bound_phone'] = $this->resource->phone ? true : false;
$data['bound_wechat'] = ($this->resource->weixin_unionid || $this->resource->weixin_openid) ? true : false;

return $data;
}

public function showSensitiveFields()
{
$this->showSensitiveFields = true;

return $this;
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class User extends Authenticatable implements MustVerifyEmailContract, JWTSubjec
];

protected $hidden = [
'password', 'remember_token',
'password', 'remember_token', 'weixin_openid', 'weixin_unionid'
];

protected $casts = [
Expand Down
11 changes: 11 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@

Route::middleware('throttle:' . config('api.rate_limits.access'))
->group(function () {
// 游客可以访问的接口

// 某个用户的详情
Route::get('users/{user}', 'UsersController@show')
->name('users.show');

// 登录后可以访问的接口
Route::middleware('auth:api')->group(function() {
// 当前登录用户信息
Route::get('user', 'UsersController@me')
->name('user.show');
});
});
});

0 comments on commit 31f6413

Please sign in to comment.