geetest.js
1.88 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
60
61
62
63
64
65
66
67
68
69
'use strict';
const _ = require('lodash');
const Geetest = require('geetest');
const logger = global.yoho.logger;
const config = global.yoho.config;
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
// };
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,
testCode = req.body.yohobuy;
let errRes = {
code: 400,
message: '验证码错误',
captchaShow: true,
changeCaptcha: true
};
if (testCode === config.testCode) {
return next();
}
// 使用极验证
let useGeetest = !_.get(req.app.locals.wap, 'geetest.validation', false);
if (!useGeetest) {
return next();
}
if (!challenge || !validate || !seccode) {
return res.send(errRes);
}
captcha.validate({
challenge,
validate,
seccode
}).then(function(success) {
if (success) {
logger.info('geetest success');
return next();
}
logger.info('geetest faild');
return res.send(errRes);
});
}
};
module.exports = geetest;