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 ]); } /**