将sdk里面的bodyParser函数去掉,简化了各接口的内部实现,接口使用不变。 添加了示例,并将带有failback功能的脚本统一放到了gt.js文件中,提供一个initGeetest的接口,用法参考示例
Use recommended error handler method: callback(err, result).
规范了错误处理,所有的回调函数现在均遵循node规范,以callback(err, result)的形式,因此您需要按照新的方法修改您的代码以正常运行。
You can modify the api server address
你现在可以修改APIServer的地址了
Use register api to check the server status, if it return false, switch to local captcha manually
现在可以用register接口作为检查服务器是否正常的方法,如果register返回false则切换为本地验证码
Pass public key to use register
API, optional right now, if you don't use it now, the captcha might broke some time in the future
将你的Public Key作为第二个参数传入,此参数现在为可选参数,只有传入才能够使用register
接口。Register接口可能在将来成为必须的接口
npm install geetest
var geetest = require('geetest)('Private key', 'Public Key')
在每次用户请求验证码时使用register接口获取challenge
geetest.register(function(err, challenge) {
if (err) {
//network error
return;
}
if(challenge) {
//deal with it
res.json({challenge: challenge})
}
})
See Web api for more detail
具体见Web api
Put the challenge into the src 将challenge作为参数传入src
$http.get('/request/to/your/register/api').success(function(e) {
var s = document.createElement('script');
s.src = 'http://api.geetest.com/get.php?gt=yourPublicKey&challenge=' + challenge;
s.async = true;
document.body.append(s);//append the script where ever you want
})
See Validate API or Express Middleware below
见以下Validate接口或Express中间件
geetest.validate({
challenge: //form's [geetest_challenge],
validate: //form's [geetest_validate],
seccode: //form's [geetest_seccode],
}, function(err, result) {
if (err) {
//network error
return;
}
if(result) {
//validate pass
}
else {
//validate fail
}
})
geetest.register(function(err, challenge) {
if (err) {
//network error
return;
}
if(challenge) {
//put this challenge into the request of get in your website
//将challenge作为参数传入前端的get请求
}
else {
//Fail to reach the server, use failback method
//无法链接无服务器,使用本地验证码
}
})
Note: express.bodyParser()
is required for geetest-sdk's bodyParser.
app.post('/someForm', geetest.bodyParser, yourHandler);
module.exports.yourHandler = function(err, req, res, next) {
if(err) {
//network error
return next(err)
}
if(req.geetest) {
console.log('GeeTest captcha validation pass');
//DO WHAT EVER NEXT
res.json({success: true})
}
else {
res.json({err: 'GeeTest captcha validation fail'});
}
}