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