laravel-learn-bbs/app/Http/Controllers/Api/UsersController.php

58 lines
1.9 KiB
PHP
Raw Normal View History

2018-06-03 09:37:54 +00:00
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\UserRequest;
2018-06-10 09:25:41 +00:00
use App\Models\Image;
2018-06-03 09:37:54 +00:00
use App\Models\User;
2018-06-10 08:39:32 +00:00
use App\TransFormers\UserTransformer;
2018-06-03 09:37:54 +00:00
use Illuminate\Support\Facades\Cache;
class UsersController extends Controller
{
public function store(UserRequest $request)
{
$verifyData = Cache::get($request->get('verification_key'), '');
if (!$verifyData) {
return $this->response->error('验证码失效', 422);
}
if (!hash_equals($verifyData['code'], $request->get('verification_code'))) {
return $this->response->errorUnauthorized('验证码错误');
}
$user = User::create($request->all(['name', 'phone', 'password']));
// 清除验证码缓存
\Cache::forget($request->verification_key);
2018-06-10 08:39:32 +00:00
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);
}
2018-06-10 09:25:41 +00:00
public function update(UserRequest $request)
{
$user = $this->user();
2018-06-16 02:18:41 +00:00
$attributes = $request->only(['name', 'email', 'introduction', 'registration_id']);
2018-06-10 09:25:41 +00:00
if ($request->get('avatar_image_id')) {
$image = Image::find($request['avatar_image_id']);
$attributes['avatar'] = $image->path;
}
$user->update($attributes);
return $this->response->item($user, new UserTransformer());
}
2018-06-16 01:15:01 +00:00
public function activedIndex(User $user)
{
$activedUsers = $user->getActiveUsers();
return $this->response->collection($activedUsers, new UserTransformer());
}
2018-06-10 08:39:32 +00:00
public function me()
{
return $this->response->item($this->user(), new UserTransformer());
2018-06-03 09:37:54 +00:00
}
}