captcha.js 1.52 KB
'use strict';

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

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

    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.cookies.udid;

    return req.ctx(imgCheckServiceModel).gen(udid).then(result => {
        if (result.code === 200 && result.data) {
            return request({
                url: result.data.verifiedGraphicCode,
                headers: {
                    'X-request-ID': req.reqID || '',
                    'X-YOHO-IP': req.yoho.clientIp || '',
                    'X-Forwarded-For': req.yoho.clientIp || '',
                    'User-Agent': 'yoho/nodejs'
                }
            }).pipe(res); // eslint-disable-line
        }
        next();
    }).catch(next);
};

/**
 * 验证img-check验证码
 */
exports.validate = (req, res, next) => {
    // let udid = req.sessionID;
    // 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);
    //     }
    // });

    return next();
};