Blame view

apps/passport/models/captcha-img-service.js 1.73 KB
htoooth authored
1 2
const CaptchaApi = require('./captcha-api');
const logger = global.yoho.logger;
htoooth authored
3
const config = global.yoho.config;
htoooth authored
4
htoooth authored
5 6
const _ = require('lodash');
htoooth authored
7 8 9
module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
htoooth authored
10
htoooth authored
11 12 13
        this.api = new CaptchaApi(ctx);
    }
htoooth authored
14 15
    generateCaptcha(id) {
        return this.api.gen(id)
htoooth authored
16 17 18 19
            .then(result => {
                logger.info('get captcha from ', result.data.url);
                return result;
            });
htoooth authored
20
    }
htoooth authored
21
htoooth authored
22 23 24 25 26 27
    _checkUniverse(captcha) {
        return captcha === config.UNIVERSAL_CAPTCHA ?
            Promise.resolve({code: 200}) :
            Promise.reject();
    }
htoooth authored
28
    check(id, captcha) {
29
        return this.api.check(id, captcha)
htoooth authored
30
            .then((result) => {
htoooth authored
31
                logger.info(`app.verified.graphic [${captcha}] result: `, JSON.stringify(result));
htoooth authored
32 33 34
                return result;
            })
            .then((result) => {
htoooth authored
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
                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
                        }
                    };
                }
            });
htoooth authored
55
    }
htoooth authored
56 57

    async try() {
htoooth authored
58
        const result = await this.api.try();
htoooth authored
59 60 61

        return _.get(result, 'data', true);
    }
htoooth authored
62
};