check.js 3.12 KB
'use strict';

const _ = require('lodash');
const decodeURIComponent = require('../../../utils/string-process').decodeURIComponent;
const logger = global.yoho.logger;
const Geetest = require('geetest');
const config = global.yoho.config;
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.loadJsBefore')) {
        res.locals.loadJsBefore.push({
            src: global.yoho.config.geetestJs
        });
    } else {
        res.locals.loadJsBefore = [
            {
                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.yoho.clientIp;

            if (remoteIp.indexOf(',') > 0) {
                let arr = remoteIp.split(',');

                remoteIp = arr[0];
            }

            // pc:limiter:IP 和PC端共用
            let operations = [cache.delAsync(`pc:limiter:${remoteIp}`)];

            // 验证码之后一小时之内不再限制qps
            if (req.session.apiLimitValidate) {
                operations.push(cache.setAsync(
                    `${config.app}:limiter:api:ishuman:${remoteIp}`,
                    1,
                    config.LIMITER_IP_TIME
                ));
            } else {
                operations.push(cache.setAsync(
                    `${config.app}:limiter:ishuman:${remoteIp}`,
                    1,
                    config.LIMITER_IP_TIME
                ));
            }

            delete req.session.apiLimitValidate;

            if (req.body.pid) {
                let riskPid = decodeURIComponent(req.body.pid) + ':' + _.get(req.yoho, 'clientIp', '');

                operations.push(cache.delAsync(riskPid));
            }

            _.forEach(config.REQUEST_LIMIT, (val, key) => {
                operations.push(cache.delAsync(`${config.app}:limiter:${key}:max:${remoteIp}`));
            });

            yield Promise.all(operations);

            return res.json({
                code: 200
            });
        } else {
            logger.info('geetest faild');
            return res.json(errRes);
        }

    })().catch(() => {
        return res.json({
            code: 400
        });
    });
};