captcha-img-service.js 1.16 KB
const CaptchaApi = require('./captcha-api');
const logger = global.yoho.logger;

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

        this.api = new CaptchaApi(ctx);
    }

    generateCaptcha(id, from) {
        return this.api.gen(id, from)
            .then(result => {
                logger.info('get captcha from ', result.data.url);
                return result;
            });
    };

    check(id, page, captcha) {
        return this.api.check(id, page, captcha).then((result) => {
            if (result.code === 200) {
                return {
                    code: 200,
                    message: '验证成功'
                };
            } else if (result.code === 503 || result.code === 504 || result.code === 501) {
                return {
                    code: 403,
                    message: result.message
                };
            } else {
                return {
                    code: 405,
                    message: result.message,
                    data: {
                        needCaptcha: true
                    }
                };
            }
        });
    };


};