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 cfde6a7 commit 98e8b29
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 178 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ BAIDU_TRANSLATE_KEY=
# aliyun 短信
SMS_ALIYUN_ACCESS_KEY_ID=
SMS_ALIYUN_ACCESS_KEY_SECRET=
SMS_ALIYUN_TEMPLATE_REGISTER=
11 changes: 11 additions & 0 deletions app/Http/Controllers/Api/Controller.php
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
{
//
}
48 changes: 48 additions & 0 deletions app/Http/Controllers/Api/VerificationCodesController.php
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);
}
}
13 changes: 13 additions & 0 deletions app/Http/Requests/Api/FormRequest.php
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;
}
}
22 changes: 22 additions & 0 deletions app/Http/Requests/Api/VerificationCodeRequest.php
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'
]
];
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"require": {
"php": "^7.2.5",
"doctrine/dbal": "^2.10",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^1.0",
"guzzlehttp/guzzle": "~6.3",
Expand Down
Loading

0 comments on commit 98e8b29

Please sign in to comment.