check.js
2.42 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
const _ = require('lodash');
const decodeURIComponent = require('../../../utils/string-process').decodeURIComponent;
const logger = global.yoho.logger;
const Geetest = require('geetest');
const co = Promise.coroutine;
const cache = global.yoho.cache.master;
const captcha = new Geetest({
geetest_id: 'bce95d796bc3058615fdf2ec2c0aef29',
geetest_key: '124c41d3a22aa48f36ae3cd609c51db5'
});
exports.index = (req, res) => {
req.yoho.captchaShow = false;
res.locals.useGeetest = true;
if (_.has(res, 'locals.loadJs')) {
res.locals.loadJs.push({
src: global.yoho.config.geetestJs
});
} else {
res.locals.loadJs = [
{
src: global.yoho.config.geetestJs
}
];
}
res.render('check', {
width750: true,
localCss: true
});
};
exports.submit = (req, res) => {
co(function * () {
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);
}
let geetestRes = yield captcha.validate({
challenge,
validate,
seccode
});
if (geetestRes) {
logger.info('geetest success');
let remoteIp = req.get('X-Forwarded-For') || req.ip;
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = arr[0];
}
let key = `pc:limiter:${remoteIp}`;
let key10m = `pc:limiter:10m:${remoteIp}`;
let dels = [
cache.delAsync(key),
cache.delAsync(key10m)
];
if (req.body.pid) {
dels.push(cache.delAsync(decodeURIComponent(req.body.pid)));
}
yield Promise.all(dels).catch(e => {
console.log(`check.js err: ${e.message}`);
});
return res.json({
code: 200
});
} else {
logger.info('geetest faild');
return res.json(errRes);
}
})().catch(() => {
return res.json({
code: 400
});
});
};