Authored by htoooth

change

... ... @@ -146,6 +146,14 @@ app.use((req, res, next) => {
req.session = {};
}
req.app.locals = _.merge(req.app.locals, {
pc: {
geetest: {
validation: true
}
}
});
next();
});
... ...
... ... @@ -5,3 +5,5 @@
<a class="btn confirm">确定</a>
</div>
</div>
{{> gee-captcha}}
... ...
... ... @@ -45,3 +45,5 @@
</div>
{{/ gift}}
</div>
{{> gee-captcha}}
... ...
... ... @@ -87,3 +87,5 @@
<p>1. 为保障您的账户信息安全,在变更账户中的重要信息时需要进行身份验证,感谢您的理解和支持。</p>
<p>2. 验证身份遇到问题?请拨打客服电话<span class="blue2">400-889-9646</span>,客服将尽快联系您。</p>
</div>
{{> gee-captcha}}
... ...
... ... @@ -4,146 +4,42 @@
'use strict';
const _ = require('lodash');
const gee = require('./gee-captcha');
const img = require('./img-captcha');
const captchaService = require('../models/captcha-img-service');
const gtestCaptcha = require('./gee-captcha');
const helpers = global.yoho.helpers;
const CAPTCHA = global.yoho.config.UNIVERSAL_CAPTCHA;
const request = require('request');
const logger = global.yoho.logger;
// 对比函数
const _mustEqual = (req) => {
let result = req.session.captcha && (req.body.verifyCode === req.session.captcha ||
req.body.verifyCode === CAPTCHA);
logger.info(`img captcha auth [${result ? 'success' : 'fail'}]`,
`CLIENT [${req.body.verifyCode}] SERVER [${req.session.captcha}]`);
return result;
};
// 中间件
const requiredAPI = (req, res, next) => {
let count = req.session.captchaCount;
if (count >= 4) {
req.session.captcha = CAPTCHA;
let captchaAPI = _.get(req.app.locals.pc, 'geetest.validation', false) ? gee.requiredAPI : img.requiredAPI;
return res.json({
code: 403,
message: '该验证码已失效'
});
}
req.session.captchaCount = count + 1;
if (_mustEqual(req)) {
return next();
if (req.body.loginType === 'password') {
return captchaAPI(req, res, next);
} else {
return res.json({
code: 400,
message: '请将图形验证码翻转至正确方向'
});
}
};
// 重定向调用
const requiredPage = (req, res, next) => {
let count = req.session.captchaCount;
if (count >= 4) {
req.session.captcha = CAPTCHA;
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
req.session.captchaCount = count + 1;
if (_mustEqual(req)) {
return next();
} else {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
};
// 生成验证码
const generate = (req, res, next) => {
captchaService.generateCaptcha().then((result) => {
req.session.captcha = result.data.text;
req.session.captchaCount = 0;
if (result.code === 200) {
return res.json({
code: result.code,
data: {
images: result.data.images
}
});
} else {
return res.json({
code: result.code,
message: result.message
});
}
}).catch(next);
};
// 七牛验证码
const generateQiniu = (req, res, next) => {
captchaService.generateCaptcha().then((result) => {
req.session.captcha = result.data.text;
req.session.captchaCount = 0;
res.type('png');
if (result.code === 200) {
request(result.data.images).pipe(res);
}
}).catch(next);
const generate = (req, res) => {
return (_.get(req.app.locals.pc, 'geetest.validation', false) ?
gee.generate :
img.generate)(req, res);
};
// 端到端检查
const checkAPI = (req, res) => {
let count = req.session.captchaCount;
if (count >= 4) {
req.session.captcha = CAPTCHA;
return res.json({
code: 403,
message: '该验证码已失效'
});
}
req.session.captchaCount = count + 1;
if (_mustEqual(req)) {
return res.json({
code: 200,
message: '验证成功'
});
} else {
return res.json({
code: 400,
message: '请将图形验证码翻转至正确方向'
});
}
return (_.get(req.app.locals.pc, 'geetest.validation', false) ?
gee.checkAPI :
img.checkAPI)(req, res);
};
const required = (req, res, next) => {
let captchaAPI = _.get(req.app.locals.pc, 'geetest.validation', false) ? gtestCaptcha.requiredAPI : requiredAPI;
if (req.body.loginType === 'password') {
return captchaAPI(req, res, next);
} else {
return next();
}
// 重定向调用
const requiredPage = (req, res, next) => {
return (_.get(req.app.locals.pc, 'geetest.validation', false) ?
gee.requiredPage :
img.requiredPage)(req, res, next);
};
module.exports = {
requiredAPI,
requiredPage,
generate,
checkAPI,
generateQiniu,
required
checkAPI
};
... ...
... ... @@ -9,6 +9,8 @@ const Geetest = require('../models/captcha-gee-service');
const config = global.yoho.config;
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const CAPTCHA = config.UNIVERSAL_CAPTCHA;
var pcGeetest = new Geetest({
... ... @@ -16,7 +18,7 @@ var pcGeetest = new Geetest({
geetest_key: config.gee_captcha.key
});
const getCode = (req, res) => {
const generate = (req, res) => {
pcGeetest.register(function(err, data) {
if (err) {
return;
... ... @@ -109,8 +111,33 @@ const checkAPI = (req, res) => {
});
};
// 重定向调用
const requiredPage = (req, res, next) => {
let verifyCode = req.body.verifyCode || '';
let captcha = verifyCode.split(',');
if (!verifyCode) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
pcGeetest.validate({
challenge: captcha[0],
validate: captcha[1],
seccode: captcha[2]
}, function(err, success) {
if (err) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
} else if (!success) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
} else {
return next();
}
});
};
module.exports = {
getCode,
generate,
requiredAPI,
checkAPI
checkAPI,
requiredPage
};
... ...
/**
* Created by TaoHuang on 2016/6/18.
*/
'use strict';
const _ = require('lodash');
const captchaService = require('../models/captcha-img-service');
const helpers = global.yoho.helpers;
const CAPTCHA = global.yoho.config.UNIVERSAL_CAPTCHA;
const request = require('request');
const logger = global.yoho.logger;
// 对比函数
const _mustEqual = (req) => {
let result = req.session.captcha && (req.body.verifyCode === req.session.captcha ||
req.body.verifyCode === CAPTCHA);
logger.info(`img captcha auth [${result ? 'success' : 'fail'}]`,
`CLIENT [${req.body.verifyCode}] SERVER [${req.session.captcha}]`);
return result;
};
// 中间件
const requiredAPI = (req, res, next) => {
let count = req.session.captchaCount;
if (count >= 4) {
req.session.captcha = CAPTCHA;
return res.json({
code: 403,
message: '该验证码已失效'
});
}
req.session.captchaCount = count + 1;
if (_mustEqual(req)) {
return next();
} else {
return res.json({
code: 400,
message: '请将图形验证码翻转至正确方向'
});
}
};
// 重定向调用
const requiredPage = (req, res, next) => {
let count = req.session.captchaCount;
if (count >= 4) {
req.session.captcha = CAPTCHA;
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
req.session.captchaCount = count + 1;
if (_mustEqual(req)) {
return next();
} else {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
};
// 七牛验证码
const generate = (req, res, next) => {
captchaService.generateCaptcha().then((result) => {
req.session.captcha = result.data.text;
req.session.captchaCount = 0;
res.type('png');
if (result.code === 200) {
request(result.data.images).pipe(res);
}
}).catch(next);
};
// 端到端检查
const checkAPI = (req, res) => {
let count = req.session.captchaCount;
if (count >= 4) {
req.session.captcha = CAPTCHA;
return res.json({
code: 403,
message: '该验证码已失效'
});
}
req.session.captchaCount = count + 1;
if (_mustEqual(req)) {
return res.json({
code: 200,
message: '验证成功'
});
} else {
return res.json({
code: 400,
message: '请将图形验证码翻转至正确方向'
});
}
};
module.exports = {
requiredAPI,
requiredPage,
generate,
checkAPI
};
... ...
... ... @@ -15,7 +15,6 @@ const back = require(cRoot + '/back');
const reg = require(cRoot + '/reg');
const bind = require(cRoot + '/bind');
const cert = require(cRoot + '/cert');
const gee = require(cRoot + '/gee-captcha');
const auth = require(`${global.middleware}/auth`);
const router = express.Router(); // eslint-disable-line
... ... @@ -36,7 +35,7 @@ router.post('/passport/login/qrcode/refresh', login.local.qrcode.refresh);
router.post('/passport/login/qrcode/check', login.local.qrcode.check); // 验证二维码的状态
router.get('/passport/login', login.common.beforeLogin, login.local.loginPage);
router.post('/passport/login/auth', captcha.required, login.local.login);
router.post('/passport/login/auth', captcha.requiredAPI, login.local.login);
router.get('/passport/logout', login.local.logout);
// 微信登录
... ... @@ -191,12 +190,7 @@ router.get('/passport/back/resetSuccess',
back.validateSuccessStatusPage,
back.resetPwdSuccessPage);
router.get('/passport/imagesNode', captcha.generate);
router.get('/passport/images.png', captcha.generate);
router.post('/passport/captcha/img', captcha.checkAPI);
router.get('/passport/images.png', captcha.generateQiniu);
// 极验证
router.get('/passport/geetest/register', gee.getCode);
router.post('/passport/geetest/validate', gee.checkAPI);
module.exports = router;
... ...
... ... @@ -38,3 +38,5 @@
</div>
{{/ back}}
</div>
{{> gee-captcha}}
... ...
... ... @@ -55,7 +55,7 @@
</span>
</li>
<li class="clearfix captcha-wrap">
<li class="clearfix password-login captcha-wrap">
</li>
<li class="relative clearfix sms-login hide">
... ... @@ -162,12 +162,8 @@
</div>
</form>
{{#if @root.pc.geetest.validation}}
<input class="gee-captcha" type="hidden">
{{/if}}
{{/ passport}}
</div>
<script src="http://static.geetest.com/static/tools/gt.js"></script>
{{> gee-captcha}}
... ...
... ... @@ -82,3 +82,5 @@
<input name="" type="hidden" id="open-id" value="{{openId}}"/>
<input name="" type="hidden" id="source-type" value="{{sourceType}}"/>
</div>
{{> gee-captcha}}
... ...
{{#if @root.pc.geetest.validation}}
<script>window._geeCaptcha = true;</script>
<script src="http://static.geetest.com/static/tools/gt.js"></script>
{{/if}}
... ...
... ... @@ -8,10 +8,6 @@ var captcha = new Captcha('.captcha-wrap', {
require('../common');
require('../common/promise');
$.sleep(500).then(function() {
captcha.refresh();
});
$('.confirm').on('click', function() {
captcha.check().then(function() {
window.jumpUrl(window.queryString().refer || '//www.yohobuy.com');
... ...
... ... @@ -72,8 +72,6 @@ var Gift = {
var captcha = new Captcha('.captcha-img').init();
captcha.refresh();
require('../common');
// 更换验证码
... ...
... ... @@ -23,8 +23,6 @@ var sInt,
var captcha = new Captcha('.captcha-safe-form-r').init();
captcha.refresh();
require('../common');
$.ajaxSetup({
async: false
... ...
... ... @@ -154,10 +154,6 @@ $(document).click(function() {
}
});
$.sleep(1000).then(function() {
captcha.refresh();
});
$phoneNum.keyup(function() {
vaPn($.trim($(this).val()));
}).focus(function() {
... ...
... ... @@ -33,11 +33,8 @@
var $ = require('yoho-jquery'),
Captcha = require('../../plugins/captcha'),
GeeCaptcha = require('../../plugins/gee-captcha'),
QRCode = require('../../plugins/qrcode');
var needGeeCaptcha = $('.gee-captcha').length;
// 密码输入帐号
var $accountInput1 = $('#account1'),
getAccountVal1 = function() {
... ... @@ -57,11 +54,9 @@ var $accountInput1 = $('#account1'),
},
// 图像验证码
$captchaImgWrapper = $('.captcha-wrap'),
captchaImg = needGeeCaptcha ? new GeeCaptcha('.captcha-wrap').init() : new Captcha('.captcha-wrap').init(),
$showCaptchaImg = true,
getCaptchaImgVal = function() {
return captchaImg.getResults();
passwordCaptchaImg = new Captcha('.captcha-wrap').init(),
getPasswordCaptchaImgVal = function() {
return passwordCaptchaImg.getResults();
},
// 短信验证码
... ... @@ -273,7 +268,6 @@ accountChangeEvent.add(function(type) {
accountChangeEvent.add(function(type) {
hideAccountTip1(); // eslint-disable-line
hideAccountTip2(); // eslint-disable-line
hideCaptchaImgTip(); // eslint-disable-line
hideCaptchaSmsTip(); // eslint-disable-line
hidePasswordTip(); // eslint-disable-line
hideSmsCaptchaImgTip(); // eslint-disable-line
... ... @@ -288,21 +282,12 @@ accountChangeEvent.add(function(type) {
initQrCode(); // eslint-disable-line
}
// 密码登录的图形验证码
if (type === AccountLoginData.PasswordLogin.name) {
if ($showCaptchaImg) {
$captchaImgWrapper.removeClass('hide');
$.sleep(500).then(function() {
captchaImg.refresh();
});
}
passwordCaptchaImg.refresh();
}
if (type === AccountLoginData.SMSLogin.name) {
if ($showCaptchaImg) {
$captchaImgWrapper.addClass('hide');
}
smsCaptchaImg.refresh();
}
});
... ... @@ -310,7 +295,6 @@ accountChangeEvent.add(function(type) {
accountChangeEvent.add(function(type) {
if (type === AccountLoginData.SMSLogin.name) {
$accountInput2.val($accountInput1.val());
refreshSmsCaptchaImg(); // eslint-disable-line
} else {
$accountInput1.val($accountInput2.val());
}
... ... @@ -387,12 +371,6 @@ function hidePasswordTip() {
/** ************************************************************************/
function hideCaptchaImgTip() {
return captchaImg.hide();
}
/** ************************************************************************/
function showCaptchaSmsTip(msg) {
refreshSmsCaptchaEvent.fire();
return errTipShow($captchaSmsTip, $captchaSmsInput, msg);
... ... @@ -539,22 +517,9 @@ function validatePasswordLocal() {
/** ************************************************************************/
/* 图形验证码 */
/** ************************************************************************/
function validateCaptchaImgAsync() {
return captchaImg.check();
}
function validateCaptchaImg() {
// 验证码不可见的时候验证通过
if ($captchaImgWrapper.is(':hidden')) {
return $.Deferred().resolve().promise(); //eslint-disable-line
}
return validateCaptchaImgAsync()
.then(hideCaptchaSmsTip);
}
function refreshPasswordCaptchaImg() {
return captchaImg.refresh();
return passwordCaptchaImg.refresh();
}
/** ************************************************************************/
... ... @@ -629,6 +594,8 @@ function validateCaptchaSmsAsync() {
}
return defer.promise();
}).always(function() {
refreshSmsCaptchaEvent.fire();
});
}
... ... @@ -674,9 +641,7 @@ function validateSmsCaptchaImg() {
}
function refreshSmsCaptchaImg() {
$.sleep(500).then(function() {
smsCaptchaImg.refresh();
});
return smsCaptchaImg.refresh();
}
/** ************************************************************************/
... ... @@ -768,8 +733,7 @@ function getDesktopLoginType() {
// 密码验证过程
function validateWithPasswordMode() {
return validateAccount()
.then(validatePasswordLocal)
.then(validateCaptchaImg);
.then(validatePasswordLocal);
}
// 短信验证过程
... ... @@ -872,7 +836,7 @@ function loginAsync() {
areaCode: getAreaCodeVal(),
account: currentLogin.getAccountVal(),
password: currentLogin.creditableToken(),
verifyCode: currentLogin.type() === 'password' ? getCaptchaImgVal() : '',
verifyCode: currentLogin.type() === 'password' ? getPasswordCaptchaImgVal() : '',
isRemember: getRememberMeVal(),
loginType: currentLogin.type()
}
... ... @@ -887,9 +851,6 @@ function loginAsync() {
showPasswordTip(res.message);
$passwordInput.addClass('error').val('');
if (res.data && res.data.needCaptcha) {
showCaptchaImgPic(); // eslint-disable-line
}
} else if (currentLogin.type() === 'sms') {
showCaptchaSmsTip('短信验证码错误');
$captchaSmsInput.addClass('error').val('');
... ... @@ -900,14 +861,6 @@ function loginAsync() {
});
}
function showCaptchaImgPic() {
captchaImg.refresh().then(function() {
$captchaImgWrapper.removeClass('hide');
});
$showCaptchaImg = true;
}
function getReferForLogin() {
var vars = {},
hash,
... ... @@ -1023,7 +976,7 @@ $captchaSmsBtn.on('click', function() {
.then(function() {
disable60sSendSmsBtn();
return sendCaptchaSmsAsync();
});
}).fail(refreshSmsCaptchaImg);
});
// 记住登录状态
... ...
... ... @@ -426,10 +426,6 @@ $(window).resize(function() {
// Tips: 不可以在获得焦点的时候验证,获得焦点和失去焦点的间隔太小,如果中间存在ajax校验的话会出现问题
// ( ▼-▼ )注册页和信息完善页面接口不同
exports.init = function(page) {
$.sleep(1000).then(function() {
captchaImage.refresh();
});
$('#agree-terms').click(function() {
var $this = $(this),
... ...
... ... @@ -7,7 +7,9 @@ var $ = require('yoho-jquery');
var GeeCaptcha = function(container, options) {
var optionDefault = {
template: require('hbs/common/gee-captcha.hbs')
template: require('hbs/common/gee-captcha.hbs'),
initURI: '//www.yohobuy.com/passport/images.png',
checkURI: '//www.yohobuy.com/passport/captcha/img'
};
$.extend(this, optionDefault, options);
... ... @@ -28,7 +30,7 @@ GeeCaptcha.prototype = {
init: function() {
var _this = this;
$.get('/passport/geetest/register?t=' + $.now()).then(function(data) {
$.get(_this.initURI + '?t=' + $.now()).then(function(data) {
initGeetest && initGeetest({ // eslint-disable-line
gt: data.gt,
challenge: data.challenge,
... ... @@ -91,22 +93,22 @@ GeeCaptcha.prototype = {
return $.Deferred().reject().promise(); // eslint-disable-line
}
//return $.post('/passport/geetest/validate', {
// verifyCode: _this.getResults()
//}).then(function(result) {
// if (result.code === 200) {
// _this.hideTip();
// return $.Deferred().resolve().promise(); // eslint-disable-line
// } else if (result.code === 403) {
// _this.refresh();
// return $.Deferred().reject().promise(); //eslint-disable-line
// } else {
// _this.showTip(result.message);
// return $.Deferred().reject().promise(); //eslint-disable-line
// }
//});
return $.Deferred().resolve().promise(); // eslint-disable-line
return $.post(this.checkURI, {
verifyCode: _this.getResults()
}).then(function(result) {
if (result.code === 200) {
_this.hideTip();
return $.Deferred().resolve().promise(); // eslint-disable-line
} else if (result.code === 403) {
_this.refresh();
return $.Deferred().reject().promise(); //eslint-disable-line
} else {
_this.showTip(result.message);
return $.Deferred().reject().promise(); //eslint-disable-line
}
}).fail(function() {
_this.refresh();
});
},
getResults: function() {
... ... @@ -149,6 +151,6 @@ GeeCaptcha.prototype = {
GeeCaptcha.prototype.construct = GeeCaptcha;
require('../common/promise');
require('../../common/promise');
module.exports = GeeCaptcha;
... ...
/**
* Created by TaoHuang on 2016/12/12.
*/
var Captcha = function(container, options) {
var optionDefault = {
template: require('hbs/common/captcha.hbs'),
refreshURI: '//www.yohobuy.com/passport/images.png',
checkURI: '//www.yohobuy.com/passport/captcha/img'
};
$.extend(this, optionDefault, options);
this.$container = $(container);
this.$imgPics = null;
this.$tip = null;
this.picWidth = null;
this.refreshCb = null;
this.running = false;
// NODE: 这个是专门给自动化测试做的后门
this.$_____trojanYohobuy = null;
return this;
};
Captcha.prototype = {
init: function() {
this.bindEvents();
this.refresh();
return this;
},
/**
* method: 绑定事件
*/
bindEvents: function() {
this.$container
.on('click.refresh', '.img-check-refresh', $.proxy(this.refresh, this))
.on('click.rotate', '.img-check-pic', $.proxy(this.rotateImg, this));
return this;
},
/**
* method: 渲染 dom
* @param obj data 渲染数据
* {
* imgSrc: 'src' //图片src
* }
*/
render: function(data) {
var self = this;
this.$container.html(this.template(data));
this.$imgPics = this.$container.find('.img-check-pic');
this.$tip = this.$container.find('.img-check-tip');
this.$_____trojanYohobuy = this.$container.find('#yohobuy');
this.picWidth = this.$imgPics.width();
this.$imgPics.each(function(index, elem) {
var $elem = $(elem);
var position = self.calcPosition($elem);
$elem.css('backgroundPosition', position);
});
return this;
},
/**
* method: 计算 background-position
* @param num index img-check-pic 的index
* @param num count img-check-pic
*/
calcPosition: function($elem) {
var positionX, positionY;
var index = $elem.index();
var count = parseInt($elem.attr('data-val'), 10);
var unit = 'px';
count = count % 4;
positionX = -index * this.picWidth + unit;
positionY = -count * this.picWidth + unit;
return [positionX, positionY].join(' ');
},
/**
* Handler method: 刷新验证码
*/
refresh: function() {
var self = this;
var url = this.refreshURI + '?t=' + $.now();
return $.sleep(500).then(function() {
if (self.running) {
return $.Deferred().reject().promise(); // eslint-disable-line
}
self.running = true;
self.render({
images: url
});
self.hideTip();
self.refreshCb && self.refreshCb();
self.running = false;
return $.Deferred().resolve().promise(); // eslint-disable-line
});
},
onRefresh: function(cb) {
this.refreshCb = cb;
return this;
},
/**
* 检查是否正确
*/
check: function() {
var self = this;
var uri = this.checkURI;
if (self.getResults() === '0000') {
self.showTip();
return $.Deferred().reject().promise(); // eslint-disable-line
}
return $.post(uri, {
verifyCode: self.getResults()
}).then(function(result) {
if (result.code === 200) {
self.hideTip();
return $.Deferred().resolve().promise(); // eslint-disable-line
} else if (result.code === 403) {
self.refresh();
return $.Deferred().reject().promise(); //eslint-disable-line
} else {
self.showTip(result.message);
return $.Deferred().reject().promise(); //eslint-disable-line
}
});
},
/**
* Handler method: 旋转图片
*/
rotateImg: function(event) {
var $pic = $(event.target);
var prevRotateCount = parseInt($pic.attr('data-val'), 10);
$pic.attr('data-val', prevRotateCount + 1);
$pic.css({
backgroundPosition: this.calcPosition($pic)
});
},
/**
* method: 获取结果
* @return string '0123'
*/
getResults: function() {
var result = [];
this.$container.find('.img-check-pic')
.each(function() {
var $elem = $(this);
var val = $elem.attr('data-val');
val = parseInt(val, 10);
result.push(val % 4);
});
if (this.$_____trojanYohobuy && this.$_____trojanYohobuy.val()) {
result = [];
result.push(this.$_____trojanYohobuy.val());
}
return result.join('');
},
/**
* 显示错误
*/
showTip: function(msg) {
if (msg) {
this.$tip.find('em').html(msg);
} else {
this.$tip.find('em').html('请将图形验证码翻转至正确方向');
}
if (this.$tip) {
this.$tip.removeClass('hide');
}
return this;
},
/**
* 隐藏错误
*/
hideTip: function() {
if (this.$tip) {
this.$tip.addClass('hide');
}
return this;
},
show: function() {
this.$container.removeClass('hide');
},
hide: function() {
this.$container.addClass('hide');
}
};
Captcha.prototype.construct = Captcha;
require('../../common/promise');
module.exports = Captcha;
... ...
/**
* Created by TaoHuang on 2016/12/12.
*/
var Captcha = function(container, options) {
var optionDefault = {
template: require('hbs/common/captcha.hbs'),
refreshURI: '/passport/images.png',
checkURI: '/passport/captcha/img'
};
$.extend(this, optionDefault, options);
this.$container = $(container);
this.$imgPics = null;
this.$tip = null;
this.picWidth = null;
this.refreshCb = null;
this.running = false;
// NODE: 这个是专门给自动化测试做的后门
this.$_____trojanYohobuy = null;
return this;
};
Captcha.prototype = {
init: function() {
this.bindEvents();
return this;
},
/**
* method: 绑定事件
*/
bindEvents: function() {
this.$container
.on('click.refresh', '.img-check-refresh', $.proxy(this.refresh, this))
.on('click.rotate', '.img-check-pic', $.proxy(this.rotateImg, this));
return this;
},
/**
* method: 渲染 dom
* @param obj data 渲染数据
* {
* imgSrc: 'src' //图片src
* }
*/
render: function(data) {
var self = this;
this.$container.html(this.template(data));
this.$imgPics = this.$container.find('.img-check-pic');
this.$tip = this.$container.find('.img-check-tip');
this.$_____trojanYohobuy = this.$container.find('#yohobuy');
this.picWidth = this.$imgPics.width();
this.$imgPics.each(function(index, elem) {
var $elem = $(elem);
var position = self.calcPosition($elem);
$elem.css('backgroundPosition', position);
});
return this;
},
/**
* method: 计算 background-position
* @param num index img-check-pic 的index
* @param num count img-check-pic
*/
calcPosition: function($elem) {
var positionX, positionY;
var index = $elem.index();
var count = parseInt($elem.attr('data-val'), 10);
var unit = 'px';
count = count % 4;
positionX = -index * this.picWidth + unit;
positionY = -count * this.picWidth + unit;
return [positionX, positionY].join(' ');
},
/**
* Handler method: 刷新验证码
*/
refresh: function() {
var self = this;
var uri = this.refreshURI + '?t=' + $.now();
if (self.running) {
return $.Deferred().reject().promise(); // eslint-disable-line
}
self.running = true;
self.render({
images: uri
});
self.hideTip();
self.refreshCb && self.refreshCb();
self.running = false;
return $.Deferred().resolve().promise(); // eslint-disable-line
},
onRefresh: function(cb) {
this.refreshCb = cb;
return this;
},
/**
* 检查是否正确
*/
check: function() {
var self = this;
var uri = this.checkURI;
if (self.getResults() === '0000') {
self.showTip();
return $.Deferred().reject().promise(); // eslint-disable-line
}
return $.post(uri, {
verifyCode: self.getResults()
}).then(function(result) {
if (result.code === 200) {
self.hideTip();
return $.Deferred().resolve().promise(); // eslint-disable-line
} else if (result.code === 403) {
self.refresh();
return $.Deferred().reject().promise(); //eslint-disable-line
} else {
self.showTip(result.message);
return $.Deferred().reject().promise(); //eslint-disable-line
}
});
},
/**
* Handler method: 旋转图片
*/
rotateImg: function(event) {
var $pic = $(event.target);
var prevRotateCount = parseInt($pic.attr('data-val'), 10);
$pic.attr('data-val', prevRotateCount + 1);
$pic.css({
backgroundPosition: this.calcPosition($pic)
});
},
/**
* method: 获取结果
* @return string '0123'
*/
getResults: function() {
var result = [];
this.$container.find('.img-check-pic')
.each(function() {
var $elem = $(this);
var val = $elem.attr('data-val');
val = parseInt(val, 10);
result.push(val % 4);
});
if (this.$_____trojanYohobuy && this.$_____trojanYohobuy.val()) {
result = [];
result.push(this.$_____trojanYohobuy.val());
}
return result.join('');
},
/**
* 显示错误
*/
showTip: function(msg) {
if (msg) {
this.$tip.find('em').html(msg);
} else {
this.$tip.find('em').html('请将图形验证码翻转至正确方向');
}
if (this.$tip) {
this.$tip.removeClass('hide');
}
return this;
},
/**
* 隐藏错误
*/
hideTip: function() {
if (this.$tip) {
this.$tip.addClass('hide');
}
return this;
},
show: function() {
this.$container.removeClass('hide');
},
hide: function() {
this.$container.addClass('hide');
}
};
Captcha.prototype.construct = Captcha;
var ImgCaptcha = require('./captcha-strategy/img-captcha'),
GeeCaptcha = require('./captcha-strategy/gee-captcha');
var Captcha = window._geeCaptcha ? GeeCaptcha : ImgCaptcha;
module.exports = Captcha;
... ...
... ... @@ -725,8 +725,8 @@
.check-safe {
padding: 10px 11px 11px;
height: auto;
overflow: hidden;
overflow: visible;
min-height: 250px;
.form-success {
background: resolve(home/onCorrect.png) no-repeat 5px 9px;
... ...