diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php new file mode 100644 index 0000000..93f91d0 --- /dev/null +++ b/app/Http/Controllers/Api/UsersController.php @@ -0,0 +1,26 @@ +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(); + } +} diff --git a/app/Http/Requests/UserRequest.php b/app/Http/Requests/UserRequest.php new file mode 100644 index 0000000..d87d013 --- /dev/null +++ b/app/Http/Requests/UserRequest.php @@ -0,0 +1,41 @@ + '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' => '短信验证码', + ]; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 60d45ba..7f4eb8e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -29,7 +29,7 @@ class User extends Authenticatable * @var array */ protected $fillable = [ - 'name', 'email', 'password', 'introduction', 'avatar ' + 'name', 'phone', 'email', 'password', 'introduction', 'avatar ' ]; /** diff --git a/config/api.php b/config/api.php index d6e8836..ae7fdda 100644 --- a/config/api.php +++ b/config/api.php @@ -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), + ], + ], ]; diff --git a/routes/api.php b/routes/api.php index 05ec45f..ec34650 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,8 +13,21 @@ $api = app('Dingo\Api\Routing\Router'); $api->version('v1', ['namespace' => 'App\Http\Controllers\Api'], function ($api) { - $api->post('verificationCodes', 'VerificationCodesController@store') - ->name('api.verificationCodes.store'); + $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) {