用户注册
This commit is contained in:
parent
32c0b14902
commit
b32dc237f5
26
app/Http/Controllers/Api/UsersController.php
Normal file
26
app/Http/Controllers/Api/UsersController.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Requests\UserRequest;
|
||||
use App\Models\User;
|
||||
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);
|
||||
|
||||
return $this->response->created();
|
||||
}
|
||||
}
|
41
app/Http/Requests/UserRequest.php
Normal file
41
app/Http/Requests/UserRequest.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UserRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|max:255',
|
||||
'password' => 'required|string|min:6',
|
||||
'verification_key' => 'required|string',
|
||||
'verification_code' => 'required|string',
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes()
|
||||
{
|
||||
return [
|
||||
'verification_key' => '短信验证码 key',
|
||||
'verification_code' => '短信验证码',
|
||||
];
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ class User extends Authenticatable
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name', 'email', 'password', 'introduction', 'avatar '
|
||||
'name', 'phone', 'email', 'password', 'introduction', 'avatar '
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -229,4 +229,20 @@ return [
|
||||
|
||||
],
|
||||
|
||||
|
||||
/*
|
||||
* 接口频率限制
|
||||
*/
|
||||
'rate_limits' => [
|
||||
// 访问频率限制,次数/分钟
|
||||
'access' => [
|
||||
'expires' => env('RATE_LIMITS_EXPIRES', 1),
|
||||
'limit' => env('RATE_LIMITS', 60),
|
||||
],
|
||||
// 登录相关,次数/分钟
|
||||
'sign' => [
|
||||
'expires' => env('SIGN_RATE_LIMITS_EXPIRES', 1),
|
||||
'limit' => env('SIGN_RATE_LIMITS', 10),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
@ -13,8 +13,21 @@
|
||||
$api = app('Dingo\Api\Routing\Router');
|
||||
|
||||
$api->version('v1', ['namespace' => 'App\Http\Controllers\Api'], function ($api) {
|
||||
$api->group([
|
||||
'middleware' => 'api.throttle',
|
||||
'limit' => config('api.rate_limits.sign.limit'),
|
||||
'expires' => config('api.rate_limits.sign.expires'),
|
||||
], function ($api) {
|
||||
// 短信验证码
|
||||
$api->post('verificationCodes', 'VerificationCodesController@store')
|
||||
->name('api.verificationCodes.store');
|
||||
// 用户注册
|
||||
$api->post('users', 'UsersController@store')
|
||||
->name('api.users.store');
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
/*$api->version('v2', function ($api) {
|
||||
|
Loading…
Reference in New Issue
Block a user