使用thinphp官网的验证码组件
https://www.thinkphp.cn/ext/8
composer安装以后,直接引入一个控制器
public function getCode(): thinkResponse
{
$uniqid = uniqid(rand(00000,99999));
$res=Captcha::create();
$base64_image = "data:image/png;base64," . base64_encode($res->getData());
$key = session('captcha.key');
cache('ADMIN_LOGIN_VERIFY_'.$uniqid,$key);
return json(['data'=>['uniqid'=>$uniqid,'image'=>$base64_image]],200);
}看似简单,不过对于初次接触的人,还是需要花点心思。
主要是前后端分离,不能使用session,所以官方的案例就失效了
以上代码参照另一篇文章,文章地址:
https://blog.csdn.net/gzmyh/article/details/130866047