From 66bde1635a5b4f45554a88857bc355aa17eb2180 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 17 Apr 2019 17:17:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- src/Captcha.php | 14 +++++++------- src/CaptchaService.php | 4 +--- src/helper.php | 8 ++++---- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7a825bd..cf1a06a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # think-captcha -thinkphp5.2 验证码类库 + +thinkphp6 验证码类库 ## 安装 > composer require topthink/think-captcha @@ -20,6 +21,7 @@ public function captcha($id = '') ~~~ 然后注册对应的路由来输出验证码 + ### 模板里输出验证码 首先要在你应用的路由定义文件中,注册一个验证码路由规则。 @@ -38,6 +40,7 @@ public function captcha($id = '') ~~~ > 上面两种的最终效果是一样的 + ### 控制器里验证 使用TP的内置验证功能即可 diff --git a/src/Captcha.php b/src/Captcha.php index 7adf051..ea396ad 100644 --- a/src/Captcha.php +++ b/src/Captcha.php @@ -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', []); @@ -96,7 +96,7 @@ protected function configure(string $config = null) * @return array * @throws Exception */ - protected function generate() + protected function generate(): array { $bag = ''; @@ -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; @@ -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); @@ -247,7 +247,7 @@ public function create(string $config = null, $api = false) * ω:决定周期(最小正周期T=2π/∣ω∣) * */ - protected function writeCurve() + protected function writeCurve(): void { $px = $py = 0; @@ -297,7 +297,7 @@ protected function writeCurve() * 画杂点 * 往图片上写不同颜色的字母或数字 */ - protected function writeNoise() + protected function writeNoise(): void { $codeSet = '2345678abcdefhijkmnpqrstuvwxyz'; for ($i = 0; $i < 10; $i++) { @@ -314,7 +314,7 @@ protected function writeNoise() * 绘制背景图片 * 注:如果验证码输出图片比较大,将占用比较多的系统资源 */ - protected function background() + protected function background(): void { $path = __DIR__ . '/../assets/bgs/'; $dir = dir($path); diff --git a/src/CaptchaService.php b/src/CaptchaService.php index a44a17d..f0cd358 100644 --- a/src/CaptchaService.php +++ b/src/CaptchaService.php @@ -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错误!'); } } diff --git a/src/helper.php b/src/helper.php index b2f9a3f..e3c3ec2 100644 --- a/src/helper.php +++ b/src/helper.php @@ -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); } @@ -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}" : '')); } @@ -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); @@ -49,4 +50,3 @@ function captcha_check($value) { return Captcha::check($value); } -