From e0aeea15f212c8fd39c805cda2d8616e4f4a6a29 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Mon, 8 Dec 2025 14:10:41 +0800 Subject: [PATCH] =?UTF-8?q?revert(platform):=20=E6=98=AFThinkPHP-Captcha?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC=E9=97=AE=E9=A2=98=EF=BC=8C=E9=80=A0?= =?UTF-8?q?=E6=88=90=E8=8E=B7=E5=8F=96=E9=AA=8C=E8=AF=81=E7=A0=81=E4=B8=8D?= =?UTF-8?q?=E7=A8=B3=E5=AE=9A=E7=9A=84=E5=9B=A0=E7=B4=A0=EF=BC=8C=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E8=BF=98=E5=8E=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/platform/controller/Login.php | 45 ++------------------------- 1 file changed, 3 insertions(+), 42 deletions(-) diff --git a/src/app/platform/controller/Login.php b/src/app/platform/controller/Login.php index 7f51586f3..73432faf7 100644 --- a/src/app/platform/controller/Login.php +++ b/src/app/platform/controller/Login.php @@ -99,50 +99,11 @@ class Login extends Controller */ public function captcha() { - // 生成验证码ID + $captcha_data = ThinkCaptcha::create(null, true); $captcha_id = md5(uniqid(null, true)); // 验证码10分钟有效 - $expire = 600; - - try { - // 直接使用门面生成验证码图片 - $captchaResponse = ThinkCaptcha::create(); - - // 获取图片内容 - ob_start(); - $captchaResponse->send(); - $imgContent = ob_get_clean(); - // 生成base64图片 - $imgBase64 = 'data:image/png;base64,' . base64_encode($imgContent); - - // 获取当前session中的验证码 - $session = app()->make('session'); - $captchaInfo = $session->get('captcha'); - - // 由于无法直接获取验证码文本,我们需要重新生成一个验证码实例 - // 并使用相同的配置来生成验证码文本 - $captchaInstance = new \think\captcha\Captcha(app()->make('config'), $session); - // 反射获取受保护的generate方法 - $reflection = new \ReflectionClass($captchaInstance); - $generateMethod = $reflection->getMethod('generate'); - $generateMethod->setAccessible(true); - $generator = $generateMethod->invoke($captchaInstance); - $code = $generator['value']; - - } catch (\Exception $e) { - try { - // 如果失败,尝试使用版本2.x的方式 - $captchaData = ThinkCaptcha::create(null, true); - $code = $captchaData['code']; - $imgBase64 = $captchaData['img']; - } catch (\Exception $e2) { - // 如果都失败,返回错误信息 - return error(-1, '验证码生成失败: ' . $e2->getMessage()); - } - } - - Cache::set($captcha_id, $code, $expire); - return success(0, '', [ 'id' => $captcha_id, 'img' => $imgBase64 ]); + Cache::set($captcha_id, $captcha_data[ 'code' ], 600); + return success(0, '', [ 'id' => $captcha_id, 'img' => $captcha_data[ 'img' ] ]); } /**