Skip to content

Commit

Permalink
调整
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Apr 17, 2019
1 parent 03bf97d commit 66bde16
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
5 changes: 4 additions & 1 deletion README.md
@@ -1,5 +1,6 @@
# think-captcha
thinkphp5.2 验证码类库

thinkphp6 验证码类库

## 安装
> composer require topthink/think-captcha
Expand All @@ -20,6 +21,7 @@ public function captcha($id = '')
~~~
然后注册对应的路由来输出验证码


### 模板里输出验证码

首先要在你应用的路由定义文件中,注册一个验证码路由规则。
Expand All @@ -38,6 +40,7 @@ public function captcha($id = '')
~~~
> 上面两种的最终效果是一样的

### 控制器里验证

使用TP的内置验证功能即可
Expand Down
14 changes: 7 additions & 7 deletions src/Captcha.php
Expand Up @@ -76,7 +76,7 @@ public function __construct(Config $config, Session $session)
* 配置验证码
* @param string|null $config
*/
protected function configure(string $config = null)
protected function configure(string $config = null): void
{
if (is_null($config)) {
$config = $this->config->get('captcha', []);
Expand All @@ -96,7 +96,7 @@ protected function configure(string $config = null)
* @return array
* @throws Exception
*/
protected function generate()
protected function generate(): array
{
$bag = '';

Expand Down Expand Up @@ -140,7 +140,7 @@ protected function generate()
* @param string $code 用户验证码
* @return bool 用户验证码是否正确
*/
public function check(string $code)
public function check(string $code): bool
{
if (!$this->session->has('captcha')) {
return false;
Expand All @@ -166,7 +166,7 @@ public function check(string $code)
* @param bool $api
* @return Response
*/
public function create(string $config = null, $api = false)
public function create(string $config = null, bool $api = false): Response
{
$this->configure($config);

Expand Down Expand Up @@ -247,7 +247,7 @@ public function create(string $config = null, $api = false)
* ω:决定周期(最小正周期T=2π/∣ω∣)
*
*/
protected function writeCurve()
protected function writeCurve(): void
{
$px = $py = 0;

Expand Down Expand Up @@ -297,7 +297,7 @@ protected function writeCurve()
* 画杂点
* 往图片上写不同颜色的字母或数字
*/
protected function writeNoise()
protected function writeNoise(): void
{
$codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
for ($i = 0; $i < 10; $i++) {
Expand All @@ -314,7 +314,7 @@ protected function writeNoise()
* 绘制背景图片
* 注:如果验证码输出图片比较大,将占用比较多的系统资源
*/
protected function background()
protected function background(): void
{
$path = __DIR__ . '/../assets/bgs/';
$dir = dir($path);
Expand Down
4 changes: 1 addition & 3 deletions src/CaptchaService.php
Expand Up @@ -14,8 +14,6 @@ public function boot(Route $route, Validate $validate)

$validate->extend('captcha', function ($value) {
return captcha_check($value);
});

$validate->setTypeMsg('captcha', ':attribute错误!');
}, ':attribute错误!');
}
}
8 changes: 4 additions & 4 deletions src/helper.php
Expand Up @@ -11,12 +11,13 @@

use think\captcha\facade\Captcha;
use think\facade\Route;
use think\Response;

/**
* @param string $config
* @return \think\Response
*/
function captcha($config = null)
function captcha($config = null): Response
{
return Captcha::create($config);
}
Expand All @@ -25,7 +26,7 @@ function captcha($config = null)
* @param $config
* @return string
*/
function captcha_src($config = null)
function captcha_src($config = null): string
{
return Route::buildUrl('/captcha' . ($config ? "/{$config}" : ''));
}
Expand All @@ -34,7 +35,7 @@ function captcha_src($config = null)
* @param $id
* @return string
*/
function captcha_img($id = '')
function captcha_img($id = ''): string
{
$src = captcha_src($id);

Expand All @@ -49,4 +50,3 @@ function captcha_check($value)
{
return Captcha::check($value);
}

0 comments on commit 66bde16

Please sign in to comment.