Authored by htoooth

fix captcha

... ... @@ -7,6 +7,8 @@
const captchaService = require('../models/captcha-service');
const helpers = global.yoho.helpers;
const CAPTCHA = 'yoho4946abcdef#$%&!@';
const request = require('request');
const uuid = require('uuid');
// 对比函数
const _mustEqual = (req) => {
... ... @@ -79,6 +81,22 @@ const generate = (req, res, next) => {
}).catch(next);
};
// 七牛验证码
const generateQiniu = (req, res, next) => {
captchaService.generateCaptcha().then((result) => {
req.session.captcha = result.data.text;
req.session.captchaCount = 0;
let imageUrl = result.data.images + `/watermark/2/text/${uuid.v4()}/fontsize/120/dissolve/10`;
res.type('png');
if (result.code === 200) {
request(imageUrl).pipe(res);
}
}).catch(next);
};
// 端到端检查
const checkAPI = (req, res) => {
let count = req.session.captchaCount;
... ... @@ -110,5 +128,6 @@ module.exports = {
requiredAPI,
requiredPage,
generate,
checkAPI
checkAPI,
generateQiniu
};
... ...
... ... @@ -201,5 +201,6 @@ router.get('/passport/back/resetSuccess',
router.get('/passport/imagesNode', captcha.generate);
router.post('/passport/captcha/img', captcha.checkAPI);
router.get('/passport/images', captcha.generateQiniu);
module.exports = router;
... ...
... ... @@ -5,7 +5,7 @@
var Captcha = function(container, options) {
var optionDefault = {
template: require('hbs/common/captcha.hbs'),
refreshURI: '/passport/imagesNode',
refreshURI: '/passport/images',
checkURI: '/passport/captcha/img'
};
... ... @@ -92,7 +92,7 @@ Captcha.prototype = {
*/
refresh: function() {
var self = this;
var uri = this.refreshURI;
var uri = this.refreshURI + '?t=' + $.now();
if (self.running) {
return $.Deferred().reject().promise(); // eslint-disable-line
... ... @@ -100,25 +100,15 @@ Captcha.prototype = {
self.running = true;
return $.get(uri)
.done(function(result) {
if (result.code !== 200) {
return self.showTip();
}
var src = result.data.images; // eslint-disable-line
self.render({
images: uri
});
self.render({
images: src
});
self.hideTip();
self.refreshCb && self.refreshCb();
self.running = false;
self.hideTip();
self.refreshCb && self.refreshCb();
})
.always(function() {
self.running = false;
});
return $.Deferred().resolve().promise(); // eslint-disable-line
},
onRefresh: function(cb) {
... ...