robot-check.js 2.18 KB
'use strict';

const robotCheckService = require('../models/robot-check-service');
const captchaService = require('../../passport/controllers/captcha');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');

const logger = global.yoho.logger;

const index = (req, res) => {
    let remoteIp = req.yoho.clientIp || '';

    logger.info(`limit remote ip: ${remoteIp}; ua: ${req.header('User-Agent')}; uid: ${req.user.uid}; reqId: ${req.reqID}`); // eslint-disable-line

    return res.render('robot-check', {
        module: '3party',
        page: 'robot-check',
        reqId: req.reqID,
        simpleHeader: simpleHeaderModel.setSimpleHeaderData()
    });
};


const check = (req, res, next) => {
    if (req.session.apiRiskValidate && req.body.apiRiskValidate) {
        return req.ctx(robotCheckService).verifyImgCheckRisk(req.yoho.udid, req.body.verifyCode).then(result => {
            if (result.code === 200) {
                return next();
            } else {
                logger.info('api risk img verify faild');
                return res.json(result);
            }
        });
    }

    return captchaService.geeCheck(req, res, () => {
        // 图形验证码关闭时通过极验证后解锁接口风控
        if (req.session.apiRiskClear) {
            delete req.session.apiRiskClear;
            req.ctx(robotCheckService).verifyImgCheckRisk(req.yoho.udid, '1,2,3,4');
        }

        return next();
    });
};

const img = captchaService.geeGenerate;

const isHuman = (req, res) => {
    let remoteIp = req.yoho.clientIp;

    if (remoteIp.indexOf(',') > 0) {
        let arr = remoteIp.split(',');

        remoteIp = arr[0];
    }
    const apiLimitValidate = req.session.apiLimitValidate || req.session.apiRiskValidate;

    delete req.session.apiLimitValidate;
    delete req.session.apiRiskValidate;

    logger.warn('isHuman', remoteIp);
    return req.ctx(robotCheckService).removeBlack(remoteIp, apiLimitValidate, req.headers.referer).then(() => {
        return res.json({
            code: 200
        });
    }).catch(() => {
        return res.json({
            code: 400
        });
    });
};

module.exports = {
    index,
    check,
    isHuman,
    img
};