1.TP安装依赖
composer require topthink/think-captcha=2.0.*
2.生成验证码返回给前端
// 生成验证码
public function getCaptcha()
{
$config = [
// 验证码字符集合
'codeSet' => '0123456789',
// 验证码字体大小
'fontSize' => 30,
//字体
'fontttf' => '5.ttf',
// 验证码位数
'length' => 4,
// 背景颜色
'bg' => [119, 136, 153]
];
$captcha = new Captcha($config);
return $captcha->entry();
}
3.前端接收
前端调接口获取验证码时请求方法的responseType要设置成'arraybuffer'
export function getCaptcha (data, responseType = 'arraybuffer') {
return fetch({
url: 'login/getCaptcha',
method: 'get',
data,