Skip to content

Commit

Permalink
用户注册
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed Aug 2, 2020
1 parent 98e8b29 commit 30a9d51
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
37 changes: 37 additions & 0 deletions app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Resources\UserResource;
use App\Http\Requests\Api\UserRequest;
use Illuminate\Auth\AuthenticationException;

class UsersController extends Controller
{
public function store(UserRequest $request)
{
$verifyData = \Cache::get($request->verification_key);

if (!$verifyData) {
abort(403, '验证码已失效');
}

if (!hash_equals($verifyData['code'], $request->verification_code)) {
// 返回401
throw new AuthenticationException('验证码错误');
}

$user = User::create([
'name' => $request->name,
'phone' => $verifyData['phone'],
'password' => $request->password,
]);

// 清除验证码缓存
\Cache::forget($request->verification_key);

return new UserResource($user);
}
}
24 changes: 24 additions & 0 deletions app/Http/Requests/Api/UserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Requests\Api;

class UserRequest extends FormRequest
{
public function rules()
{
return [
'name' => 'required|between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name',
'password' => 'required|alpha_dash|min:6',
'verification_key' => 'required|string',
'verification_code' => 'required|string',
];
}

public function attributes()
{
return [
'verification_key' => '短信验证码 key',
'verification_code' => '短信验证码',
];
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function notify($instance)
}

protected $fillable = [
'name', 'email', 'password', 'introduction', 'avatar',
'name', 'phone', 'email', 'password', 'introduction', 'avatar',
];

protected $hidden = [
Expand Down
3 changes: 2 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Resources\Json\JsonResource;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -29,6 +30,6 @@ public function boot()
\App\Models\Topic::observe(\App\Observers\TopicObserver::class);
\App\Models\Link::observe(\App\Observers\LinkObserver::class);

//
JsonResource::withoutWrapping();
}
}
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
// 短信验证码
Route::post('verificationCodes', 'VerificationCodesController@store')
->name('verificationCodes.store');
// 用户注册
Route::post('users', 'UsersController@store')
->name('users.store');
});

0 comments on commit 30a9d51

Please sign in to comment.