geetest.js
1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
const Geetest = require('geetest');
const logger = global.yoho.logger;
const captcha = new Geetest({
geetest_id: 'bce95d796bc3058615fdf2ec2c0aef29',
geetest_key: '124c41d3a22aa48f36ae3cd609c51db5'
});
const geetest = {
register(req, res, next) {
captcha.register().then(function(data) {
// data 为一个对象,包含 gt, challenge, success, new_captcha 字段
// success 为 1 表示正常模式,为 0 表示宕机模式(failback, fallback)
// var body = {
// gt: data.geetest_id,
// challenge: data.challenge,
// success: data.success
// };
if (!data.success) {
req.session.useYohoCaptcha = true;
}
res.send(data);
}).catch(next);
},
validate(req, res, next) {
let challenge = req.body.geetest_challenge,
validate = req.body.geetest_validate,
seccode = req.body.geetest_seccode;
let errRes = {
code: 400,
message: '验证码错误',
captchaShow: true,
changeCaptcha: true
};
if (!challenge || !validate || !seccode) {
return res.json(errRes);
}
return captcha.validate({
challenge,
validate,
seccode
}).then(function(success) {
if (success) {
logger.info('geetest success');
return next();
}
logger.info('geetest faild');
return res.json(errRes);
});
}
};
module.exports = geetest;