gee-captcha.js 3.4 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 CAPTCHA = config.UNIVERSAL_CAPTCHA;

// 对比函数
const _mustEqual = () => {
    return false;
};

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.json({
                code: 501,
                data: data
            });
        } else {
            // 正常模式
            res.send({
                code: 200,
                data: data
            });
        }
    });
};

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

    if (!verifyCode) {
        return res.send({
            code: 5011,
            message: '未输入图形验证码,请输入图形验证码',
            data: {
                needCaptcha: true
            }
        });
    }

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

            if (err) {
                res.send({
                    message: '网络失败,请刷新图形验证码',
                    code: 5011,
                    data: {
                        needCaptcha: true
                    }
                });
            } else if (!success) {
                res.send({
                    message: '图形验证失败, 请刷新图形验证码',
                    code: 5011,
                    data: {
                        needCaptcha: true
                    }
                });
            } else {
                req.body.verifyCode = null;
                return next();
            }
        });
    }
};

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

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

    if (_mustEqual(req)) {
        return res.json({
            message: '验证成功',
            code: 200
        });
    }

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

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

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