var sms, smsWrap; function Passport(el) { this.$F = el; this.F = el.get(0); return this.init(); } function Register(el) { this.$F = el; this.F = el.get(0); sms = $(this.F.sms); smsWrap = sms.parent().parent(); return this.init(); } function freshCode(el) { el = $(el); el.attr('src', el.attr('url') + '?' + new Date().getTime()); } function errorMessage(elem, messageStr) { var message = elem.children('.has-error'); $('html').scrollTop(0); message.removeClass('hide'); message.text(messageStr || '登录失败'); } function checkUserName() { var that = $(this), username = $.trim(this.value); if (!username) return false; if (!(isMobile(username) || isEmail(username))) { that.next().removeClass('hide').text(Register.prototype.messages[0]); sms.attr('disabled', true); smsWrap.addClass('hide'); return false; } $.ajax({ url: BASE_URL + 'register/checkuser', type: 'post', dataType: 'json', data: {username: username}, success: function (rs) { if (rs.state == 1) { that.next().removeClass('hide').text('该账号已注册,请更换'); sms.attr('disabled', true); smsWrap.addClass('hide'); } if (rs.state == 0) { that.next().addClass('hide'); if (isMobile(username)) { sms.attr('disabled', false); smsWrap.removeClass('hide'); } else { sms.attr('disabled', true); smsWrap.addClass('hide'); } } }, error: function () { that.next().removeClass('hide').text('系统错误,请重新输入'); } }); } function delayedGet(time, el, beTxt) { time--; var ele = $(el); if (time < 1) { ele.attr('disabled', false); ele.text(beTxt); } else { ele.attr('disabled', true); ele.text(time + '秒后重新获取'); setTimeout('delayedGet(' + time + ',"' + el + '","' + beTxt + '")', 1000); } } function sendEmail(el) { el = $(el); el.attr('disabled', true); $.ajax({ url: BASE_URL + 'register/againmail', type: 'post', dataType: 'json', success: function (rs) { if (rs.state == 1) { $('#senderror').text(''); delayedGet(60, '#checkSms', el.text()); } else { $('#senderror').text('(' + rs.message + ')'); el.attr('disabled', false); } }, error: function () { $('#senderror').text('(系统错误,请重试)'); el.attr('disabled', false); } }); } !function () { function initError() { this.$error = $('.has-error', this.$F); if (!this.$error.is(':hidden')) this.$error.addClass('hide'); } function hideMessage() { $(this).find('.has-error').addClass('hide'); } function showMessage(e, s, ipt) { if (e.$error.is(':hidden')) e.$error.removeClass('hide'); e.$error.text(s); if (ipt) ipt.focus(); }; function checkUsername(v) { if (!isMobile(v) && !isEmail(v) && !isUsername(v))return false; return true; }; function checkPassword(v) { if (v.length < 6 || v.length > 18)return false; return true; } var $box = $("#box"); //TODO 登录验证原型 Passport.prototype.init = function () { initError.call(this); this.initEvent(); return this; }; Passport.prototype.initEvent = function () { var _this = this; this.$F.on('submit', function () { var v = $.trim(this.username.value); if (!v || !checkUsername(v)) { YZ.Valid(this.username, _this.messages[0]); return false; } YZ.Valid(this.username); v = $.trim(this.password.value); if (!v || !checkPassword(v)) { YZ.Valid(this.password, _this.messages[1]); return false; } YZ.Valid(this.password); if (this.vcode.disabled == false) { v = this.vcode.value; if (!v) { YZ.Valid(this.vcode, '请输入验证码', true); return false; } YZ.Valid(this.vcode); } hideMessage.call(this); $.ajax({ type: 'post', dataType: 'json', data: $(this).serialize(), success: function (rs) { if (rs.state == 1) { location.href = rs.content && rs.content.forward ? rs.content.forward : BASE_URL; } else { errorMessage(_this.$F, rs.message); if (rs.content || rs.content.is_code) { $('#loginVcode').removeClass('hide'); $('#loginVcode input').removeAttr('disabled'); freshCode('#loginImg'); } } }, error: function () { freshCode('#loginImg'); errorMessage(_this.$F, '系统错误,请重试'); } }); return false; }); return false; }; Passport.prototype.messages = [ '请输入正确的账号、手机号码或邮箱地址', '请输入正确的密码' ]; //TODO 注册验证原型 Register.prototype.init = function () { initError.call(this); this.initEvent(); return this; }; Register.prototype.messages = [ '请输入正确的手机号码或邮箱地址', '请输入正确的密码', '两次密码不相同,请重新输入', '昵称不能为空', '请输入短信验证码', '请输入正确的短信验证码' ]; Register.prototype.initEvent = function () { var that = this, btnSms; $(this.F.username).on('blur', checkUserName); btnSms = $('.btn-sms').on('click', showBox); $("#sendSms").click(function () { var mobile = that.F.username.value, mobileVcode = $('#mobileVcode'), vcode = mobileVcode.val(); if (!isMobile(mobile)) { alert('手机号码格式不正确'); return false; } if (!vcode) { mobileVcode.focus(); return false; } $.post(BASE_URL + 'register/sendsms', { mobile: mobile, vcode: vcode }, function (rs) { freshCode('#mobileImgVcode'); if (rs.state == 1) { hideBox(); delayedGet(60, '#checkSms', '获取验证码'); $('#smsPrompt').text(''); } else { mobileVcode.focus(); $('#smsPrompt').text(rs.message); } }, 'json') .error(function () { alert('系统错误,请重试'); }); return false; }) this.$F.on('submit', function () { var el = this.username, v = el.value.trim(); if (!v || !(isMobile(v) || isEmail(v))) { YZ.Valid(el, that.messages[0], 1); return false; } YZ.Valid(el); el = this.nickname; if (el) { v = el.value.trim(); if (!v) { YZ.Valid(this.nickname, that.messages[3], 1); return false; } } YZ.Valid(el); el = this.password, v = el.value.trim(); if (!checkPassword(v)) { YZ.Valid(this.password, that.messages[1], 1); return false; } YZ.Valid(el); el = this.repassword; if (v != el.value.trim()) { YZ.Valid(el, that.messages[2], 1); return false; } YZ.Valid(el); el = this.sms; if (el) { v = el.value.trim(); if (!v.length) { YZ.Valid(el, that.messages[4], 1); return false; } else if (v.length != 4) { YZ.Valid(el, that.messages[5], 1); return false; } YZ.Valid(el); } $.ajax({ url: BASE_URL + 'register/index', type: 'post', dataType: 'json', data: that.$F.serialize(), success: function (rs) { if (rs.state == 1) { window.location.href = BASE_URL + 'register/success/' + that.F.username.value.trim(); } else { errorMessage(that.$F, rs.message); } }, error: function () { freshCode('#loginImg'); errorMessage(that.$F, '系统错误,请重试'); } }); return false; }); }; var $box = $("#box").find('.mask').click(function () { hideBox(); }).end(); function showBox() { $('#smsPrompt').text(''); freshCode('#mobileImgVcode'); $box.removeClass('hide'); } function hideBox() { $box.addClass('hide'); } }();