Authored by 郭成尧

3party-check

'use strict';
const _ = require('lodash');
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
... ... @@ -9,28 +32,58 @@ exports.index = (req, res) => {
};
exports.submit = (req, res) => {
let remoteIp = req.get('X-Forwarded-For') || req.ip;
co(function * () {
let challenge = req.body.geetest_challenge,
validate = req.body.geetest_validate,
seccode = req.body.geetest_seccode;
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
let errRes = {
code: 400,
message: '验证码错误',
captchaShow: true,
changeCaptcha: true
};
remoteIp = arr[0];
}
let key = `pc:limiter:${remoteIp}`;
let key10m = `pc:limiter:10m:${remoteIp}`;
if (!challenge || !validate || !seccode) {
return res.json(errRes);
}
Promise.all([
cache.delAsync(key),
cache.delAsync(key10m)
]).then(() => {
return res.json({
code: 200
let geetestRes = yield captcha.validate({
challenge,
validate,
seccode
});
}).catch(() => {
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}`;
yield Promise.all([
cache.delAsync(key),
cache.delAsync(key10m)
]);
return res.json({
code: 200
});
} else {
logger.info('geetest faild');
return res.json(errRes);
}
})().catch(() => {
return res.json({
code: 400
});
});
};
... ...
... ... @@ -11,7 +11,6 @@ const cRoot = './controllers';
const ads = require(`${cRoot}/ads`);
const check = require(`${cRoot}/check`);
const question = require(`${cRoot}/question`);
const validateCode = require('../passport/controllers/validateCode');
const auth = require('../../doraemon/middleware/auth');
// const material = require(`${cRoot}/material`);
... ... @@ -20,8 +19,8 @@ const materialNew = require(`${cRoot}/material-new`);
// routers
router.get('/ads', ads.index);
router.get('/check', validateCode.load, check.index);
router.post('/check/submit', validateCode.check, check.submit);
router.get('/check', check.index);
router.post('/check/submit', check.submit);
router.get('/questionnaire', auth, question.list);
... ...