dysix/tp6-captcha
最新稳定版本:1.0
Composer 安装命令:
composer require dysix/tp6-captcha
包简介
tp6 前后端分离模式验证码机制
README 文档
README
thinkphp6 验证码类库 ,适用于前后端分离模式,api接口验证码使用场景
安装
composer require dysix/tp6-captcha
使用
注册路由
首先在应用的路由定义文件中,注册验证码路由规则
//展示验证码图形 Route::get('captcha/:id', function(){ return \Dysix\Tp6Captcha\facade\Captcha::create($id); //return \Dysix\Tp6Captcha\facade\Captcha::instance(['length'=>8])->create($id);//自定义更改配置项 }); //验证码接口 Route::get('captcha', function (){ //验证码唯一标识 $uniqid = uniqid((string)mt_rand(100000, 999999)); $src = (string)\think\facade\Route::buildUrl('/captcha/' . $uniqid)->domain(true); $data = [ 'src' => $src, 'uniqid' => $uniqid, ]; return json($data); });
获取验证码
首先请求验证码接口获取信息,获取到的验证码数据如图示
{
"src": "http://domain/captcha/720807640afff8834bd",
"uniqid": "720807640afff8834bd"
}
前端图片使用返回的链接展示验证码图片
<img src="http://domain/captcha/720807640afff8834bd">
验证
需将验证码与uniqid一起提交验证
//登录 public function index() { $input = $this->request->post('', null, ['trim']); if(!\Dysix\Tp6Captcha\facade\Captcha::check($input['captcha'], $input['uniqid'])){ return json(['code' => 400, 'msg' => '验证码错误'); } }
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2023-05-08