说起验证码,尽管用户反感但还是挺需要的,以前的项目用自己写的基于GeeTest(极验)的 Laravel + Vue 组件,这次也考虑过reCaptcha的国内版,不过实在担心改版、收费及其它原因,所以自己用Laravel做一个吧。
这个包默认是用于SPA这种无状态下的响应,所以没有依赖Session,使用Session可以依照IStorage进行扩展,记得middleware使用web。
内置两个路由,一个获取验证码图像,返回一个base64的代码和一个身份标识;另一个进行验证。 提供简单的字符展示和数学计算两种,每种都支持Ascii(阿拉伯数字)和非Ascii(如中文),样子如下:
用法
axios
.get('your_domain/api/v1/captcha/image')
.then(response => (this.captcha = response.data))
返回
{
"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAAyCAIAAAB6RmagAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAF/0lEQVR4nO2bW1BbRRjH9yQhgQQC4X4pVtAUaCktUAoq1o5aLx3qjIoy2DJtpx37gPaho06dPthxHF9wrA/4oMPYB2WkM9hR2+lY2hGL6Bhkyq1TkFDCRW5pIFxyg9x8ODSenJMTkpOzmxO6v6ec3c1+m7P/832bb/cQq6YxgMH4QhTuAWCECxYHhhUsDgwrWBwYVrA4MKxgcWBYweLAsILFgWEFiwPDChYHhhUsDgwrEnhdO1zugQWLdsk2b3OY7M7kaElJimJXkgKeRXgMGq0X+mfap5dmLfbUmKhns5RnijILVDHhHhdcCN433iwO57fDhsu6hS69adXpptXmKmXn92ypzk3i1yhULg7pT/8xRvspYgI0VuYczUsJ06BQwL84Flcd6u97zQ6XnzaH1Ulf7csVEQS/pmHw5Z3Z9/+aYKttqHikvjAd5XhQwv+aI0EmqduW7L9Ns3b+zJ/jvJvmHc2c6ayGVRkAgLOaCc2cCdl4EANlQfpuYfqGPuHrQf3vM8swrPNIfaeOERi9cLpBfacO1XBQA0UcOcroqq2qDZt91jfDoXN5U5e8qYvDF4Pl8ujCXaOVWnKuJMtwrPRcSRa18K7R2jo6j2A86OF/zeGBnEKZmMhWyBxu99jKKq2BmABTdaVKqZhz5ySWk3tDGSc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M1un6dSaBMv5D02n2BxsBIbJf60PPtUh9cpQGYM+KQsm+28kp/nO0ShBBUiORM2cQgtmvikbluKRm/6Zug+W4NjeSnH87nkeUPZMUYGCnH4j7VCuyM0GitzEmUSn8dd39uV8XFZqHleGCeV+ALFMUH/PlAId2FDegzmxjuzGr1pyryWIZc+kRb7TmF6cTLEt/eEIBR0Z0gxnAmXl8XiiCQQuxMsjkgFgVCwODYDkOIOFsemgl93gsWxaQldKFgcDwXchILF8dAR+KshWBwYVoS48RYRu5fU9/Y4f13gYM+BYeU/JAxuGwa14rYAAAAASUVORK5CYII=",
"validation": "f9cc27ee094dbb5024da2e9a103b3642"
}
axios
.post('your_domain/api/v1/captcha/image', post_data)
.then(response => (this.validation = response.data))
响应如下
{
"message": "The captcha code input valid, please try again",
"status": 403
}
使用配置文件可以方便的修改路由
'router_api' => '/api/v1/captcha/image',
配置
源码中有比较细致的注释
php artisan vendor:publish --tag=captcha
<?php
return [
'router' => '/api/v1/captcha/image',
'generator' => \Tao\Captcha\Core\Generator\SimpleGenerator::class,
'code' => \Tao\Captcha\Core\Code\CalculatorCode::class,
'storage' => \Tao\Captcha\Core\Storage\CacheStorage::class,
'codeType' => [
'chars' => '1234567890',
'isAscii' => true,
'operations' => '+-*'
],
'font' => base_path('vendor/tao/captcha/src/resources/fonts/ximai.ttf'),
'fontSize' => 26,
'letterSpacing' => 15,
'length' => [4, 5],
'width' => 180,
'height' => 50,
'background' => ['f9f4e0', 'd3dee0', 'f6f7f9'],
'colors' => ['b8364f', '0390aa', '1b6aa8'],
'scratches' => [3, 3],
];
文件结构
├── src
│ ├── CaptchaServiceProvider.php
│ ├── Controllers
│ │ └── CaptchaController.php
│ ├── Core
│ │ ├── Captcha.php
│ │ ├── Code
│ │ │ ├── AbstractCode.php
│ │ │ ├── CalculatorCode.php
│ │ │ └── SimpleCode.php
│ │ ├── Generator
│ │ │ ├── AbstractGenerator.php
│ │ │ ├── IGenerator.php
│ │ │ └── SimpleGenerator.php
│ │ ├── Helper.php
│ │ └── Storage
│ │ ├── CacheStorage.php
│ │ └── IStorage.php
│ ├── Facades
│ │ └── Captcha.php
│ ├── resources
│ │ ├── fonts
│ │ │ └── ximai.ttf
│ │ └── lang
│ │ ├── en
│ │ │ └── message.php
│ │ └── zh-CN
│ │ └── message.php
│ └── routes
│ └── api.php
├── tests
│ └── CaptchaTest.php
Github
欢迎到这里无情鼓励
|