fix(platform): 修复由于使用的Think-Captcha版本不同造成的验证码的问题

This commit is contained in:
2025-12-08 12:01:24 +08:00
parent 99ba1d812c
commit ea322c9727

View File

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