captcha.js
1.82 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
/* eslint-disable newline-after-var */
/* eslint-disable quotes */
'use strict';
const tencentcloud = require('tencentcloud-sdk-nodejs');
const logger = global.yoho.logger;
const CaptchaClient = tencentcloud.captcha.v20190722.Client;
const models = tencentcloud.captcha.v20190722.Models;
const Credential = tencentcloud.common.Credential;
const ClientProfile = tencentcloud.common.ClientProfile;
const HttpProfile = tencentcloud.common.HttpProfile;
const { secretId, secretKey, AppSecretKey, appId } = global.yoho.config.captcha;
let cred = new Credential(secretId, secretKey);
let httpProfile = new HttpProfile();
httpProfile.endpoint = 'captcha.tencentcloudapi.com';
let clientProfile = new ClientProfile();
clientProfile.httpProfile = httpProfile;
let client = new CaptchaClient(cred, 'ap-shanghai', clientProfile);
let captchaReq = new models.DescribeCaptchaResultRequest();
/**
* 优惠券防刷验证码
*/
module.exports = function(req, res, next) {
if (!req.couponCheckRequired) {
return next();
}
if (!(req.query.ticket && req.query.randstr)) {
return res.jsonp({ code: 400, message: '没有验证信息' });
}
const params = {
AppSecretKey,
CaptchaAppId: appId,
CaptchaType: 9,
UserIp: req.yoho.clientIp,
Ticket: req.query.ticket,
Randstr: req.query.randstr
};
captchaReq.from_json_string(JSON.stringify(params));
// eslint-disable-next-line new-cap
client.DescribeCaptchaResult(captchaReq, function(errMsg, response) {
if (errMsg) {
logger.error('tencent captcha fail ', req.user.uid);
return res.jsonp({ code: 400, message: errMsg.Message });
}
if (response.CaptchaCode !== 1) {
return res.jsonp({ code: 400, message: '验证码错误'});
}
next();
});
};