captcha.js 1.14 KB
/**
 * Created by TaoHuang on 2016/6/18.
 */

'use strict';
const _ = require('lodash');
const gee = require('./gee-captcha');
const img = require('./img-captcha');

const isGeetest = (req) => {
    if (_.get(req.app.locals.pc, 'geetest.validation', false)) {
        return req.session.captchaType !== 'img';
    }

    return false;
};

const requiredAPI = (req, res, next) => {
    return (isGeetest(req) ? gee.requiredAPI : img.requiredAPI)(req, res, next);
};

const generate = (req, res) => {
    return (isGeetest(req) ? gee.generate : img.generate)(req, res);
};

// 端到端检查
const checkAPI = (req, res) => {
    return (isGeetest(req) ? gee.checkAPI : img.checkAPI)(req, res);
};

const passwordRequired = (req, res, next) => {
    if (req.body.loginType === 'password') {
        return requiredAPI(req, res, next);
    } else {
        return next();
    }
};

const tryGeetest = (req, res, next) => {
    if (isGeetest(req)) {
        req.app.locals.geetest = true;
    } else {
        req.app.locals.geetest = false;
    }

    next();
};

module.exports = {
    requiredAPI,
    generate,
    checkAPI,
    passwordRequired,
    tryGeetest
};