简单用法
<img src="{:captcha_src()}" alt="captcha" />
参数配置
参数 | 描述 | 默认 |
---|
codeSet | 验证码字符集合 | 略 |
expire | 验证码过期时间(s) | 1800 |
useZh | 使用中文验证码 | false |
zhSet | 中文验证码字符串 | 略 |
useImgBg | 使用背景图片 | false |
fontSize | 验证码字体大小(px) | 25 |
useCurve | 是否画混淆曲线 | true |
useNoise | 是否添加杂点 | true |
imageH | 验证码图片高度,设置为0为自动计算 | 0 |
imageW | 验证码图片宽度,设置为0为自动计算 | 0 |
length | 验证码位数 | 5 |
fontttf | 验证码字体, | 不设置是随机获取 |
bg | 背景颜色 | [243, 251, 254] |
reset | 验证成功后是否重置 | true |
'captcha' => [
'fontSize' => 18,
'imageH' => 30,
'imageW' =>100,
'length' => 5,
'reset' => true
]
点击切换验证码
<img onclick="this.src='{:captcha_src()}?'+Math.random()" src="{:captcha_src()}" alt="captcha" />
controller层
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Admin;
class Login extends Controller
{
public function index()
{
if(request()->isPost()){
$admin = new Admin();
$data=input('post.');
if($admin->login($data)==3){
$this->success("信息正确,正在为您跳转",'index/index');
}else if($admin->login($data)==4){
$this->error('验证码错误');
}
else{
$this->error("用户名或密码错误");
}
}
return $this->fetch();
}
}
model层
namespace app\admin\model;
use think\Model;
use think\Db;
class Admin extends Model{
public function login($data){
$captcha = new \think\captcha\Captcha();
if(!$captcha->check($data['code'])){
return 4;
}
$user = Db::name('admin')->where('username','=',$data['username'])->find();
if($user){
if($user['password'] == md5($data['password'])){
session('username', $user['username']);
session('uid', $user['id']);
return 3;
}else{
return 2;
}
}else{
return 1;
}
}
}