captcha.js 1.31 KB
'use strict';

let imgCheckAPI = require('../models/imgcheck');
const request = require('request');
const uuid = require('uuid');

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

    return imgCheckAPI.gen().then(result => {
        if (result.code === 200 && result.data) {
            let codeStr = result.data.degrees.reduce((str, rotate) => {
                return str.concat((4 - rotate / 90 % 4) % 4);
            }, '');

            req.session.captcha = codeStr;
            req.session.captchaSrc = result.data.verifiedGraphicCode;

            data.src = `/passport/img-check.jpg?t=${Date.now()}`;

            return res.json(data);
        }

        next();
    }).catch(next);
};


exports.imgCheck = (req, res, next) => {
    return imgCheckAPI.gen().then(result => {
        if (result.code === 200 && result.data) {
            let codeStr = result.data.degrees.reduce((str, rotate) => {
                return str.concat((4 - rotate / 90 % 4) % 4);
            }, '');

            req.session.captcha = codeStr;
            req.session.captchaSrc = result.data.verifiedGraphicCode;

            return request(`${result.data.verifiedGraphicCode}?imageView2/0/format/jpg/q/70|watermark/2/text/${uuid.v4()}/fontsize/120/dissolve/10`).pipe(res);
        }
        next();
    }).catch(next);
};