70 lines
1.9 KiB
PHP
70 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Captcha\Http\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Exceptions\NotAllowedException;
|
|
use App\Domains\Captcha\Services\CaptchaService;
|
|
|
|
class CaptchaController extends Controller
|
|
{
|
|
protected $request;
|
|
protected $captchaService;
|
|
|
|
/**
|
|
* 构造函数,自动注入.
|
|
*/
|
|
public function __construct(Request $request, CaptchaService $captchaService)
|
|
{
|
|
$this->request = $request;
|
|
$this->captchaService = $captchaService;
|
|
}
|
|
|
|
/**
|
|
* get CAPTCHA
|
|
*
|
|
* @param string $config
|
|
* @return \Intervention\Image\ImageManager->response
|
|
*/
|
|
public function getCaptcha($config = 'default')
|
|
{
|
|
if ($this->request->isMethod('post')) {
|
|
$captchaKey = $this->request->get('captcha_key', '');
|
|
$captcha = $this->request->get('captcha', '');
|
|
if (!$this->captchaService->check($captcha, $captchaKey)) {
|
|
throw new NotAllowedException('验证码不正确');
|
|
}
|
|
|
|
return res(true, '验证码正确');
|
|
}
|
|
|
|
if (ob_get_contents()) {
|
|
ob_clean();
|
|
}
|
|
|
|
return $this->captchaService->create($config);
|
|
}
|
|
|
|
/**
|
|
* get CAPTCHA api
|
|
*
|
|
* @param string $config
|
|
* @return \Intervention\Image\ImageManager->response
|
|
*/
|
|
public function getCaptchaApi($config = 'default')
|
|
{
|
|
if ($this->request->isMethod('post')) {
|
|
$captchaKey = $this->request->get('captcha_key', '');
|
|
$captcha = $this->request->get('captcha', '');
|
|
if (!$this->captchaService->check($captcha, $captchaKey)) {
|
|
throw new NotAllowedException('验证码不正确');
|
|
}
|
|
|
|
return res(true, '验证码正确', 201);
|
|
}
|
|
|
|
return res($this->captchaService->create($config, true), '获取验证码');
|
|
}
|
|
}
|