验证码

This commit is contained in:
邓皓元 2018-12-11 10:48:27 +08:00
parent b5e5702b17
commit 881c48398d
4 changed files with 42 additions and 11 deletions

View File

@ -4,35 +4,66 @@ 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 Captcha $captcha
* @param string $config
* @return \Intervention\Image\ImageManager->response
*/
public function getCaptcha(CaptchaService $captcha, $config = 'default')
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 $captcha->create($config);
return $this->captchaService->create($config);
}
/**
* get CAPTCHA api
*
* @param Captcha $captcha
* @param string $config
* @return \Intervention\Image\ImageManager->response
*/
public function getCaptchaApi(CaptchaService $captcha, $config = 'default')
public function getCaptchaApi($config = 'default')
{
return res($captcha->create($config, true), '获取验证码');
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), '获取验证码');
}
}

View File

@ -1,10 +1,10 @@
<?php
// Prefix: /api/captcha
$router->group(['prefix' => 'captcha', 'as' => 'captcha'], function($router) {
$router->group(['prefix' => 'captcha', 'as' => 'captcha'], function ($router) {
// The controllers live in Domains/Captcha/Http/Controllers
$router->get('/{config}', ['as' => 'index', 'uses' => 'CaptchaController@getCaptchaApi']);
$router->addRoute(['GET', 'POST'], '/{config}', ['as' => 'index', 'uses' => 'CaptchaController@getCaptchaApi']);
/**
* 需要认证的接口
@ -12,4 +12,4 @@ $router->group(['prefix' => 'captcha', 'as' => 'captcha'], function($router) {
// $router->group(['middleware' => ['adminAuth']], function($router) {
// // $router->post('delete', ['as' => 'delete', 'uses' => 'CaptchaController@delete']);
// });
});
});

View File

@ -3,7 +3,7 @@
$router->group(['prefix' => 'captcha', 'as' => 'captcha'], function ($router) {
// The controllers live in Domains/Captcha/Http/Controllers
$router->get('/{config}', ['as' => 'index', 'uses' => 'CaptchaController@getCaptcha']);
$router->addRoute(['GET', 'POST'], '/{config}', ['as' => 'index', 'uses' => 'CaptchaController@getCaptcha']);
/**
* 需要认证的接口

View File

@ -42,4 +42,4 @@ return [
'contrast' => -5,
]
];
];