Blame view

apps/passport/controllers/captcha.js 1.1 KB
htoooth authored
1 2 3 4 5
/**
 * Created by TaoHuang on 2016/6/18.
 */

'use strict';
htoooth authored
6
const _ = require('lodash');
htoooth authored
7 8
const gee = require('./gee-captcha');
const img = require('./img-captcha');
htoooth authored
9
htoooth authored
10 11 12 13 14 15 16 17
const isGeetest = (req) => {
    if (_.get(req.app.locals.pc, 'geetest.validation', false)) {
        return true;
    }

    return req.session.captchaType === 'geetest';
};
htoooth authored
18
const requiredAPI = (req, res, next) => {
htoooth authored
19
    return (isGeetest(req) ? gee.requiredAPI : img.requiredAPI)(req, res, next);
htoooth authored
20
};
htoooth authored
21
htoooth authored
22
const generate = (req, res) => {
htoooth authored
23
    return (isGeetest(req) ? gee.generate : img.generate)(req, res);
htoooth authored
24 25
};
htoooth authored
26
// 端到端检查
htoooth authored
27
const checkAPI = (req, res) => {
htoooth authored
28
    return (isGeetest(req) ? gee.checkAPI : img.checkAPI)(req, res);
htoooth authored
29 30
};
htoooth authored
31 32 33 34 35 36 37 38
const passwordRequired = (req, res, next) => {
    if (req.body.loginType === 'password') {
        return requiredAPI(req, res, next);
    } else {
        return next();
    }
};
htoooth authored
39 40 41 42 43 44 45
const captchaSwitcher = (req, res, next) => {
    if (isGeetest(req)) {
        req.app.locals.geetest = true;
    }
    next();
};
htoooth authored
46
module.exports = {
htoooth authored
47
    requiredAPI,
htoooth authored
48
    generate,
htoooth authored
49
    checkAPI,
htoooth authored
50 51
    passwordRequired,
    captchaSwitcher
htoooth authored
52
};