captcha.js
1.63 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
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Created by TaoHuang on 2016/6/18.
*/
'use strict';
const _ = require('lodash');
const gee = require('./gee-captcha');
const img = require('./img-captcha');
const isGeetest = (req) => {
if (_.get(req.app.locals.pc, 'geetest.validation', false)) {
return req.session.captchaType !== 'img';
}
return false;
};
const requiredAPI = (req, res, next) => {
return (isGeetest(req) ? gee.requiredAPI : img.requiredAPI)(req, res, next);
};
const generate = (req, res) => {
return (isGeetest(req) ? gee.generate : img.generate)(req, res);
};
// 端到端检查
const checkAPI = (req, res) => {
return (isGeetest(req) ? gee.checkAPI : img.checkAPI)(req, res);
};
const passwordRequired = (req, res, next) => {
// 默认账户密码登录需要验证码
if (_.get(req.app.locals.pc, 'password.alwaysNeedCaptcha', false)) {
if (req.body.loginType === 'password') {
return requiredAPI(req, res, next);
}
} else {
if (req.session.type === 'needCaptcha' && req.body.loginType === 'password') {
return requiredAPI(req, res, next);
}
}
return next();
};
const tryGeetest = (req, res, next) => {
if (isGeetest(req)) {
req.app.locals.geetest = true;
} else {
req.app.locals.geetest = false;
}
next();
};
const geetestOnly = (req, res, next) => {
req.app.locals.geetest = true;
next();
};
const geetestCheck = gee.requiredAPI;
const geetestGenerate = gee.generate;
module.exports = {
requiredAPI,
generate,
checkAPI,
passwordRequired,
tryGeetest,
geetestOnly,
geetestCheck,
geetestGenerate
};