From ea322c9727de93af253e8a44b8ec0182938aa2ff Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Mon, 8 Dec 2025 12:01:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(platform):=20=E4=BF=AE=E5=A4=8D=E7=94=B1?= =?UTF-8?q?=E4=BA=8E=E4=BD=BF=E7=94=A8=E7=9A=84Think-Captcha=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=B8=8D=E5=90=8C=E9=80=A0=E6=88=90=E7=9A=84=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/platform/controller/Login.php | 29 ++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/app/platform/controller/Login.php b/src/app/platform/controller/Login.php index 73432faf7..b33f07fdd 100644 --- a/src/app/platform/controller/Login.php +++ b/src/app/platform/controller/Login.php @@ -99,11 +99,34 @@ class Login extends Controller */ public function captcha() { - $captcha_data = ThinkCaptcha::create(null, true); + // 生成验证码ID $captcha_id = md5(uniqid(null, true)); // 验证码10分钟有效 - Cache::set($captcha_id, $captcha_data[ 'code' ], 600); - return success(0, '', [ 'id' => $captcha_id, 'img' => $captcha_data[ 'img' ] ]); + $expire = 600; + + try { + // 尝试使用版本3.x的方式 + $captchaInstance = new \think\captcha\Captcha(app()->make('config'), app()->make('session')); + $captchaInstance->configure(); + $generator = $captchaInstance->generate(); + $code = $generator['value']; + $captchaResponse = ThinkCaptcha::create(); + + // 获取图片内容 + ob_start(); + $captchaResponse->send(); + $imgContent = ob_get_clean(); + // 生成base64图片 + $imgBase64 = 'data:image/png;base64,' . base64_encode($imgContent); + } catch (\Exception $e) { + // 如果失败,尝试使用版本2.x的方式 + $captchaData = ThinkCaptcha::create(null, true); + $code = $captchaData['code']; + $imgBase64 = $captchaData['img']; + } + + Cache::set($captcha_id, $code, $expire); + return success(0, '', [ 'id' => $captcha_id, 'img' => $imgBase64 ]); } /**