revert(platform): 是ThinkPHP-Captcha 版本问题,造成获取验证码不稳定的因素,暂时还原

This commit is contained in:
2025-12-08 14:10:41 +08:00
parent 951836083e
commit e0aeea15f2

View File

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