imgcheck.js 2.46 KB
'use strict';
const PAGE = 'H5';
const logger = global.yoho.logger;
const serviceAPI = global.yoho.ServiceAPI.ApiUrl;
const ApiUrl = global.yoho.API.ApiUrl;
const config = global.yoho.config;
const sign = global.yoho.sign;
const querystring = require('querystring');

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * 获取图片
     */
    gen(udid) {
        if (udid) {
            let params = {
                udid: udid,
                fromPage: PAGE,
                client_type: config.app,
                app_version: config.appVersion
            };
            const headers = {};

            if (!global.isProduction && config.yohoVerifyUdid) {
                params.udid = global.yoho.verify.udid;
                params = global.yoho.sign.apiSign(params);
                const verifySign = global.yoho.verify ? global.yoho.verify.sign(params) : '';

                headers['x-yoho-verify'] = verifySign;
            }
            const result = {
                code: 200,
                data: {
                    url: `${serviceAPI}passport/img-check?${querystring.stringify(params)}`
                },
                headers
            };

            return Promise.resolve(result);
        } else {
            return Promise.reject({
                code: 400,
                message: '生成二维码失败,请重新刷新!'
            });
        }
    }

    /**
     * 校验
     */
    check(udid, captcha) {
        return this.get({
            data: {
                method: 'app.verified.graphic',
                udid: udid,
                fromPage: PAGE,
                degrees: captcha
            }
        }).then(result => {
            logger.info(`app.verified.graphic result: ${JSON.stringify(result)}`);
            return result;
        });
    }

    /**
     * 是否需要验证码
     */
    isNeedImgCheck(udid) {
        return this.get({
            api: global.yoho.ServiceAPI,
            url: 'smart/way',
            data: {
                udid: udid,
                fromPage: PAGE
            }
        }).then(result => {
            logger.info(`smart/way ${udid} result: ${JSON.stringify(result)}`);
            return result;
        });
    }

    getRiskCheckImg(udid) {
        return Promise.resolve(`${ApiUrl}?${querystring.stringify(sign.apiSign({
            method: 'app.graphic.img',
            udid,
            fromPage: PAGE
        }))}`);
    }
};