-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98e8b29
commit 30a9d51
Showing
6 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => '短信验证码', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters