gee-captcha-controller.js
2.16 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
70
71
72
73
74
75
76
77
78
79
80
/**
* 用户controller
* @author: shuiling.wang<shuiling.wang@yoho.cn>
* @date: 2017/10/17
*/
const Context = require('../framework/context');
const Geetest = require('gt3-sdk');
const logger = global.yoho.logger;
let pcGeetest = new Geetest({
geetest_id: '4bf7f911856073c1347247667790e7c4',
geetest_key: '8d41283971d071216f1cef3d190d6b44'
});
class GeeCaptchaController extends Context {
constructor() {
super();
}
captcha(req, res) {
return pcGeetest.register({
client_type: 'web'
}, function(err, data) {
if (err) {
logger.error(err);
return res.json({code: 500});
}
if (!data.success) {
req.session.fallback = true;
return res.json({
code: 501,
data: data
});
} else {
// 正常模式
req.session.fallback = false;
return res.send({
code: 200,
data: data
});
}
});
}
check(req, res, next) {
let verifyCode = req.body.captcha || '';
if (!verifyCode) {
return res.send({
code: 405,
message: '请将图形验证码拖动至正确位置'
});
}
pcGeetest.validate(req.session.fallback, {
geetest_challenge: verifyCode[0],
geetest_validate: verifyCode[1],
geetest_seccode: verifyCode[2]
}, function(err, success) {
// logger.info(`geetest captcha auth [${err ? 'fail' : 'success'}]`,
// `CLIENT [${req.body.verifyCode}] SERVER [${err}]`);
if (err) {
return res.send({
message: '网络错误',
code: 405
});
} else if (!success) {
return res.send({
message: '图形验证失败',
code: 405
});
} else {
return next();
}
});
}
}
module.exports = GeeCaptchaController;