laravel-learn-bbs/app/Http/Controllers/Api/CaptchasController.php
2018-06-03 18:58:09 +08:00

26 lines
817 B
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\CaptchaRequest;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Cache;
class CaptchasController extends Controller
{
public function store(CaptchaRequest $request, CaptchaBuilder $captchaBuilder)
{
$key = 'captcha' . str_random(15);
$phone = $request->get('phone');
$capcha = $captchaBuilder->build();
$expiredAt = now()->addMinutes(2);
Cache::put($key, ['phone' => $phone, 'code' => $capcha->getPhrase()], $expiredAt);
$result = [
'captcha_key' => $key,
'expired_at' => $expiredAt->toDateTimeString(),
'captcha_image_content' => $capcha->inline()
];
return $this->response->array($result)->setStatusCode(201);
}
}