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

26 lines
817 B
PHP
Raw Permalink Normal View History

2018-06-03 10:58:09 +00:00
<?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);
}
}