laravel-learn-bbs/app/Http/Controllers/Api/VerificationCodesController.php

51 lines
1.8 KiB
PHP
Raw Normal View History

2018-06-03 08:11:51 +00:00
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\VerificationCodeRequest;
use Overtrue\EasySms\EasySms;
class VerificationCodesController extends Controller
{
public function store(VerificationCodeRequest $request, EasySms $easySms)
{
2018-06-03 10:58:09 +00:00
$captchaData = \Cache::get($request->get('captcha_key'));
2018-06-03 08:11:51 +00:00
2018-06-03 10:58:09 +00:00
if (!$captchaData) {
return $this->response->error('图片验证码已失效', 422);
}
2018-06-03 08:11:51 +00:00
2018-06-03 10:58:09 +00:00
if (!hash_equals($captchaData['code'], $request->get('captcha_code'))) {
// 验证错误就清除缓存
\Cache::forget($request->get('captcha_key'));
return $this->response->errorUnauthorized('验证码错误');
}
$phone = $captchaData['phone'];
// 生成4位随机数左侧补0
2018-06-03 08:11:51 +00:00
if (!app()->environment('production')) {
$code = '1234';
} else {
try {
$code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
$result = $easySms->send($phone, [
'content' => "【Lbbs社区】您的验证码是{$code}。如非本人操作,请忽略本短信"
]);
} catch (\GuzzleHttp\Exception\ClientException $exception) {
$response = $exception->getResponse();
$result = json_decode($response->getBody()->getContents(), true);
return $this->response->errorInternal($result['msg'] ?? '短信发送异常');
}
}
$key = 'verificationCode_' . str_random(15);
$expiredAt = now()->addMinutes(10);
// 缓存验证码 10分钟过期。
\Cache::put($key, ['phone' => $phone, 'code' => $code], $expiredAt);
return $this->response->array([
'key' => $key,
'expired_at' => $expiredAt->toDateTimeString(),
])->setStatusCode(201);
}
}