robot-check.js 1.96 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) => {
    return res.render('robot-check', {
        module: '3party',
        page: 'robot-check',
        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
};