captcha.js 1.17 KB
'use strict';

const imgCheckServiceModel = require('../models/imgcheck');
const request = require('request');

exports.get = (req, res, next) => {
    let data = {
        src: ''
    };
    let udid = req.session.id;

    return req.ctx(imgCheckServiceModel).gen(udid).then(result => {
        if (result.code === 200 && result.data) {
            data.src = `/passport/img-check.jpg?t=${Date.now()}`;

            return res.json(data);
        }
        next();
    }).catch(next);
};

exports.imgCheck = (req, res, next) => {
    let udid = req.session.id;

    return req.ctx(imgCheckServiceModel).gen(udid).then(result => {
        if (result.code === 200 && result.data) {
            return request(result.data.verifiedGraphicCode).pipe(res); // eslint-disable-line
        }
        next();
    }).catch(next);
};

/**
 * 验证img-check验证码
 */
exports.validate = (req, res, next) => {
    let udid = req.session.id;
    let captchaInput = req.body.captcha;

    return req.ctx(imgCheckServiceModel).check(udid, captchaInput).then(result => {
        if (result.code === 200) {
            return next();
        } else {
            return res.json(result);
        }
    });
};