(function($){ var BBCGetCode = function(element, options = {}) { this.$el = $(element) this.options = Object.assign(this.$el.data(),options) this.$phone = $(this.options.getcodePhone) this.timer = this.options.timer || 60 this.init() } BBCGetCode.prototype.init = function() { this.initCaptcha() this.initEvent() } BBCGetCode.prototype.initCaptcha = function() { var type = this.options.captchaType switch(type) { case 'rotatecaptcha': this.$el.BBCCaptchaRotate() break; case 'imagecaptcha': break; } } BBCGetCode.prototype.initEvent = function() { var self = this this.$phone.on('input keyup', function(){ var phone = $(this).val() console.log(phone) if($.bbc.helper.isChinaMobile(phone)) { self.$el.prop('disabled',false) }else{ self.$el.prop('disabled',true) } }) self.$el.on('success.check',function(e,res){ var phone = self.$phone.val() $.request(self.options.getcodeRequest,{ flash: true, data: {phone: phone, captcha_token:res.captcha_token}, success: function(a) { this.success(a) if(a.code == 1) { console.log(a) self.setTimer() $.oc.flashMsg({ text: a.msg, class: 'success' }) }else{ $.oc.flashMsg({ text: a.msg, class: 'error' }) } } }) }) } BBCGetCode.prototype.setTimer = function() { var self = this var text = self.$el.text() self.$el.prop('disabled',true) var i = self.timer var timer = setInterval(() => { if(i < 0) { clearInterval(timer) self.$el.text(text) self.$el.prop('disabled',false) }else{ self.$el.text(i) } i-- }, 1000); } var old = $.fn.BBCGetCode $.fn.BBCGetCode = function (option) { var args = Array.prototype.slice.call(arguments, 1), result this.each(function () { var $this = $(this) var data = $this.data('bbc.getcode') var options = $.extend({}, BBCGetCode.DEFAULTS, $this.data(), typeof option == 'object' && option) if (!data) $this.data('bbc.getcode', (data = new BBCGetCode(this, options))) if (typeof option == 'string') result = data[option].apply(data, args) if (typeof result != 'undefined') return false }) return result ? result : this } $.fn.BBCGetCode.Constructor = BBCGetCode // BBCGetCode NO CONFLICT // ================= $.fn.BBCGetCode.noConflict = function () { $.fn.BBCCaptchaRotate = old return this } $(document).ready(function(){ $('[data-control="getcode"]').BBCGetCode() }) })(window.jQuery);