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

'use strict';

const _ = require('lodash');
const library = '../../../library';
const sessionService = require('../models/session-service');
const captcha = require('../models/captcha-service')(sessionService);
const helpers = require(library + '/helpers');

exports.checkAPI = (req, res, next) => {
    let captchaToken = req.param('verifyCode', '');

    captcha.findByContentAsync(captchaToken)
        .then(result => {
            if (_.isEmpty(result)) {
                res.json({
                    code: 400,
                    message: '您输入的验证码不正确!'
                });
            } else {
                next();
            }
        });
};

exports.checkPage = (req, res, next) => {
    let captchaToken = req.param('verifyCode', '');

    captcha.findByContentAsync(captchaToken)
        .then(result => {
            if (_.isEmpty(result)) {
                res.redirect(helpers.urlFormat('/passport/back/index.html'));
            } else {
                next();
            }
        });
};