gee-captcha.js 3.17 KB
'use strict';

/**
 * 极验证
 * Created by Tao.Huang on 2017/3/17.
 */

const Geetest = require('../models/captcha-gee-service');

const config = global.yoho.config;
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;

const CAPTCHA = config.UNIVERSAL_CAPTCHA;

var pcGeetest = new Geetest({
    geetest_id: config.gee_captcha.id,
    geetest_key: config.gee_captcha.key
});

const generate = (req, res) => {
    pcGeetest.register(function(err, data) {
        if (err) {
            return;
        }

        if (!data.success) {
            res.send(data);
        } else {
            // 正常模式
            res.send(data);
        }
    });
};

/**
 * 中间件检查
 */
const requiredAPI = (req, res, next) => {
    let verifyCode = req.body.verifyCode || '';
    let captcha = verifyCode.split(',');

    if (!verifyCode) {
        return res.send({
            code: 401,
            message: '参数失效'
        });
    }

    if (verifyCode === CAPTCHA) {
        return next();
    }

    pcGeetest.validate({
        challenge: captcha[0],
        validate: captcha[1],
        seccode: captcha[2]
    }, function(err, success) {
        logger.info(`img captcha auth [${err ? 'fail' : 'success'}]`,
            `CLIENT [${req.body.verifyCode}] SERVER [${err}]`);

        if (err) {
            res.send({
                message: '网络失败',
                code: 401
            });
        } else if (!success) {
            res.send({
                message: '图形验证失败',
                code: 401
            });
        } else {
            return next();
        }
    });
};

// 端到端检查
const checkAPI = (req, res) => {
    let verifyCode = req.body.verifyCode || '';
    let captcha = verifyCode.split(',');

    if (!verifyCode) {
        return res.send({
            code: 401,
            message: '参数失效'
        });
    }

    pcGeetest.validate({
        challenge: captcha[0],
        validate: captcha[1],
        seccode: captcha[2]
    }, function(err, success) {
        if (err) {
            return res.send({
                message: '网络失败',
                code: 401
            });
        } else if (!success) {
            return res.send({
                message: '图形验证失败',
                code: 401
            });
        } else {
            return res.send({
                message: '验证成功',
                code: 200
            });
        }
    });
};

// 重定向调用
const requiredPage = (req, res, next) => {
    let verifyCode = req.body.verifyCode || '';
    let captcha = verifyCode.split(',');

    if (!verifyCode) {
        return res.redirect(helpers.urlFormat('/passport/back/index'));
    }

    pcGeetest.validate({
        challenge: captcha[0],
        validate: captcha[1],
        seccode: captcha[2]
    }, function(err, success) {
        if (err) {
            return res.redirect(helpers.urlFormat('/passport/back/index'));
        } else if (!success) {
            return res.redirect(helpers.urlFormat('/passport/back/index'));
        } else {
            return next();
        }
    });
};

module.exports = {
    generate,
    requiredAPI,
    checkAPI,
    requiredPage
};