validata.js
3.56 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* 验证码插件
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2017/03/13
*/
let $ = require('yoho-jquery'),
ImgCheck = require('plugin/img-check'),
tip = require('plugin/tip');
const validType = {
IMG_CHECK: 1,
GEETEST: 2
};
// 解决加载w3t统计代码同时使用geetest报错问题
(() => {
(typeof SVGAnimatedString !== 'undefined') && (SVGAnimatedString.prototype.indexOf = function() {
return false;
});
})();
class Validate {
constructor(container, options, type) {
this.render(container, options, type);
}
render(container, options, type) {
this.$container = container instanceof $ ? container : $(container);
this.options = options;
this.type = type || (this.$container.data('geetest') ? validType.GEETEST : validType.IMG_CHECK);
if ($('#yohobuy').length < 1) {
this.$container.after('<input id="yohobuy" type="text" style="display:none;">');
}
if (this.type === validType.IMG_CHECK) {
this.imgCheck = new ImgCheck(this.$container, this.options);
if (type) {
this.init();
}
} else {
$.ajax({
url: '/passport/geetest/register?t=' + (new Date()).getTime(), // 加随机数防止缓存(IE下的问题)
type: 'get',
dataType: 'json',
success: data => {
if (data.success) {
window.initGeetest && window.initGeetest({
gt: data.gt,
challenge: data.challenge,
offline: !data.success
}, (captchaObj) => {
this.captchaObj = captchaObj;
captchaObj.appendTo(this.$container);
this._atWorking = true;
this.$container.addClass('hide').addClass('popup');
});
} else {
this.render(container, options, validType.IMG_CHECK);
}
}
});
$('.yoho-page').on('touchstart', '#js-img-check', (e) => {
if ($(e.target).attr('id') !== 'js-img-check') {
return;
}
this.$container.addClass('hide');
});
}
}
get atWorking() {
return this._atWorking;
}
refresh() {
if (this.type === validType.IMG_CHECK) {
this.imgCheck.refresh();
} else {
this.captchaObj.refresh();
}
}
init() {
if (this.type === validType.IMG_CHECK) {
this.imgCheck.init();
this._atWorking = this.imgCheck.atWorking;
}
}
getResults() {
let yohobuy = $('#yohobuy').val();
if (yohobuy) {
return Promise.resolve({yohobuy});
} else if (this.type === validType.IMG_CHECK) {
let captcha = this.imgCheck.getResults();
if (captcha === '0000') {
tip.show(' 请将图片旋转到正确方向');
return Promise.reject();
}
return Promise.resolve({captcha});
} else {
return new Promise((resolve) => {
this.$container.removeClass('hide');
this.captchaObj.onSuccess(() => {
resolve(this.captchaObj.getValidate());
this.$container.addClass('hide');
});
});
}
}
}
module.exports = Validate;