captcha.js
1.64 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
/* eslint-disable newline-after-var */
/* eslint-disable quotes */
"use strict";
const _ = require('lodash');
const tencentcloud = require('tencentcloud-sdk-nodejs');
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) {
const isOpenCaptcha = _.get(req.app.locals.wap, 'ufo.coupon', false);
if (!isOpenCaptcha) {
return next();
}
if (req.query.coupontype === 'ufo') {
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) {
if (errMsg) {
return res.jsonp({code: 400, message: errMsg.Message});
}
next();
});
} else {
// eslint-disable-next-line callback-return
next();
}
};