-
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
cfde6a7
commit 98e8b29
Showing
10 changed files
with
405 additions
and
178 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
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,11 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller as BaseController; | ||
|
||
class Controller extends BaseController | ||
{ | ||
// | ||
} |
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,48 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Http\Request; | ||
use Overtrue\EasySms\EasySms; | ||
use App\Http\Requests\Api\VerificationCodeRequest; | ||
|
||
class VerificationCodesController extends Controller | ||
{ | ||
public function store(VerificationCodeRequest $request, EasySms $easySms) | ||
{ | ||
$phone = $request->phone; | ||
|
||
// 生成4位随机数,左侧补0 | ||
$code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT); | ||
|
||
if (!app()->environment('production')) { | ||
$code = '1234'; | ||
} else { | ||
// 生成4位随机数,左侧补0 | ||
$code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT); | ||
|
||
try { | ||
$result = $easySms->send($phone, [ | ||
'template' => config('easysms.gateways.aliyun.templates.register'), | ||
'data' => [ | ||
'code' => $code | ||
], | ||
]); | ||
} catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) { | ||
$message = $exception->getException('aliyun')->getMessage(); | ||
abort(500, $message ?: '短信发送异常'); | ||
} | ||
} | ||
|
||
$key = 'verificationCode_'.Str::random(15); | ||
$expiredAt = now()->addMinutes(5); | ||
// 缓存验证码 5 分钟过期。 | ||
\Cache::put($key, ['phone' => $phone, 'code' => $code], $expiredAt); | ||
|
||
return response()->json([ | ||
'key' => $key, | ||
'expired_at' => $expiredAt->toDateTimeString(), | ||
])->setStatusCode(201); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest; | ||
|
||
class FormRequest extends BaseFormRequest | ||
{ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
class VerificationCodeRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'phone' => [ | ||
'required', | ||
'regex:/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199)\d{8}$/', | ||
'unique:users' | ||
] | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.