define(['jquery', 'utils', 'fmodal', 'fselect', 'fscroll', 'ajaxRequest', 'validate', 'handlebars'], function($, utils, fmodal, fselect, fscroll, ajaxRequest, validate, handlebars) { return { // 初始化淘金客公用方法 initCustomer: function() { var self = this; _hmt.push(['_trackEvent', 'REGISTER','finished', 'customer', 'pc']); $('#customer-login').on('click','.js-refresh',function(){ var $this = $(this); self.refreshCustomerLoginQr($this); }); $('.js-login-type').on('click', '.js-login-status', function() { var $this = $(this); self.loginSwitch($this); }); $('.js-forgetPassword').on('click', function() { var $this = $(this); self.forgetPasswordCustomer($this); }); $('.js-forget-close').on('click', function() { var $this = $(this); self.forgetPasswordCustomerClose($this); }); $('.shareto_mask').on('click', function() { var $this = $(this); self.hideModal($this); }); $('.js-register-form').on('click', '.getCode', function() { var $this = $(this); self.phoneVerification($(this)); }).on('click', 'input[type="submit"]', function() { var $this = $(this); self.registerCustomer($(this)); }).on('focus', 'input', function() { var $this = $(this); self.removeError($this); }); $('.js-login-form').on('click', 'input[type="submit"]', function() { var $this = $(this); self.loginCustomer($(this)); }).on('focus', 'input', function() { var $this = $(this); self.removeError($this); }).each(function() { var $this = $(this); self.setLoginNumberCustomer($this); }); $(document).on('click', '.js-logout', function() { var $this = $(this); self.logout($this); }); self.getLoginStatus(); $('.js-forget-form').on('click', 'input[type="submit"]', function() { var $this = $(this); if ($this.data('pending')) return false; else $this.data('pending', true); self.identityVerify($this); }).on('click', '.getCode', function() { var $this = $(this); self.phoneVerification($(this)); }).on('focus', 'input', function() { var $this = $(this); self.removeError($this); }); }, showModal: function($this) { var $target = $('#' + $this.data('modal')), $maskLayer = $('.shareto_mask'), $modal = $('.pop-modal'); $modal.hide().addClass('none'); $maskLayer.show().removeClass('none'); $target.removeClass('none').show().fadeIn('fast'); }, showModalCustomer: function($this) { var self = this, $status = $this.data('status'), $modal = $('#customer-login'), $regModal = $('#customer-reg'); $modal.show().removeClass('none'); $regModal.addClass('none'); self.getLoginQRUuid($this); }, getLoginQRUuid: function($this){ var self = this, $qrBox = $('.qr-login'), url = $qrBox.data('id'), $img = $qrBox.find('[qr]'), $qrBox = $('.qr-login'), qrUrl = $qrBox.data('qr'); if ($this.data('pending')) return false; else $this.data('pending', true); $img.prop('src',qrUrl + '?' + Math.random()); setTimeout(function(){ $.get(url).done(function(result) { if(result.status){ $qrBox.data('uuid',result.data); self.getUuidStatus($qrBox); } else { $.alert(result.msg); } $this.data('pending', false); }).fail(function() { $.alert('请求服务异常'); $this.data('pending', false); }); },1000); }, getUuidStatus: function($qrBox){ var self = this, lx = $qrBox.data('lx'), id = $qrBox.data('uuid'), temp = null; temp = setInterval(function(){ $qrBox.data('temp',temp); $.ajax({ type: 'GET', url: lx + '?key=' + id, cache: false, success: function(result, textStatus, jqXHR) { switch (result.data) { case '104': //已经过期 要刷新 self.customerQrLoginTimeOut(temp); break; case '200': //扫描未登录 self.customerQrLoginScanNotLogin(); break; case '204': self.customerQrLoginScanLogin(); break; default: break; } }, error: function(jqXHR, textStatus, errorThrown) { console.info('查询状态出错或者超时'); } }); },2000); }, customerQrLoginTimeOut:function(temp){ var $timeOut = $('.ql-time-out'), $scan = $('.ql-scan'); $scan.addClass('none'); $timeOut.removeClass('none'); clearInterval(temp); }, customerQrLoginScanNotLogin:function(){ var $timeOut = $('.ql-time-out'), $scan = $('.ql-scan'); $timeOut.addClass('none'); $scan.removeClass('none'); }, customerQrLoginScanLogin: function(){ window.location.href = '/customer/action/user/index'; }, refreshCustomerLoginQr: function($this){ var self = this, $modal = $('#customer-login'), $img = $modal.find('[qr]'), $qrBox = $('.qr-login'), temp = $qrBox.data(temp), $timeOut = $modal.find('.ql-time-out'), src = $qrBox.data('qr'); $img.prop('src',src + '?' + Math.random()); $timeOut.addClass('none'); clearInterval(temp); self.getLoginQRUuid($this); }, forgetPasswordCustomer: function($this) { $('#customer-login').hide().addClass('none'); $('.forget-modal').show().removeClass('none'); $('.shareto_mask').unbind(); }, forgetPasswordCustomerClose: function($this) { $('.forget-modal').hide().addClass('none'); $('.shareto_mask').bind('click', function() { var $maskLayer = $('.shareto_mask'), $modal = $('.pop-modal'); $maskLayer.hide().addClass('none'); $modal.hide().addClass('none'); }); }, identityVerify: function($this) { var self = this, $form = $this.closest('form'), url = $form.data('action'), user = $this.data('user'), data = $form.serializeArray(), $maskLayer = $('.shareto_mask'); $form.data('cansubmit', true); $form.find('[name="phone"]').validate({'type': 'none'}, 'local'); $form.find('[name="verifyCode"]').validate({'type': 'none'}, 'local'); $form.find('.getCode').validate({'type': 'isActive'}, 'local'); $form.find('[name="code"]').validate({'exact': 6}, 'local'); $form.find('[name="newPassword"]').validate({'leng': [6, 20], 'norepeat': 'login_pwd'}, 'local'); $form.find('[name="repeatPassWord"]').validate({'repeat': 'newPassword'}, 'local'); if(!$form.data('cansubmit')) { $this.data('pending', false); return false; }; $this.data('ajax', false).ajaxRequest({ "url": url, "data": data, "callBack": function(result) { if (result.status) { $.alert(result.data.message); //$modal.hide().addClass('none'); $.alert(result.message); setTimeout(function(){ window.location.href = "/customer/action/login/index"; },2000); } else { if (result.data) { $form.data('validate', false).validate(result, 'overall'); $this.data('pending', false); } else { $.alert(result.message); } } $this.data('pending', false); } }) }, loginSwitch: function($this) { var self = this, $obj = $('.js-login-form').find('input'), $objR = $('.js-register-form').find('input'), $status = $this.data('status'), $qrBox = $('.qr-login'), temp = $qrBox.data('temp'); if($this.hasClass('active')) return false; if($status == '1') { $this.addClass('active').siblings().removeClass('active'); $('.qr-login').show().removeClass('none'); $('.account-login').hide().addClass('none'); self.removeError($obj); self.getLoginQRUuid($this); }else { $this.addClass('active').siblings().removeClass('active'); $('.qr-login').hide().addClass('none'); $('.account-login').show().removeClass('none'); self.removeError($objR); if(temp != null){ clearInterval(temp); } } }, hideModal: function($this) { var $maskLayer = $('.shareto_mask'), $modal = $('.pop-modal'); $maskLayer.hide().addClass('none'); $modal.hide().addClass('none'); }, phoneVerification: function($obj){ var self = this, $this = $obj, $form = $this.closest('form'), $phoneGroup = $this.closest('dl'), $message = $phoneGroup.find('.error-msg'), $phone = $this.closest('form').find('input[name="phone"]'), url = $obj.data('url'), data = { "phone" : $form.find('input[name="phone"]').val(), "type" : $this.data('type'), "verifyCode":$form.find('input[name="verifyCode"]').val() }; $form.data('cansubmit', true); $form.find('[name="verifyCode"]').validate({'type': 'none'}, 'local'); $form.find('[name="phone"]').validate({'type': 'phone'}, 'local'); if(!$form.data('cansubmit')) { $this.data('pending', false); return false; } if ($form.find('[name="phone"]').data('used')) { $.alert('该手机号已被占用,请核对后重新输入!'); return false; } if (!$phone.data('phonearr')) { $phone.data('phonearr', []); } for(i in $phone.data('phonearr')) { if($phone.data('phonearr')[i] == $phone.val()) { $phone.data('acquired', true); } } if (!$phone.data('acquired')) { $phone.data('phonearr').push($phone.val()); } if($this.data('phone') != data.phone) { self.countdownStatus = false; $this.data('pending', false); $this.data('phone', data.phone); } else { $.alert('短信发送操作过于频繁,稍后再试'); return false; } if($this.data('pending')) return false; $this.data('pending', true); var countdown = function($tips) { setTimeout(function(){ var count = $tips.text() - 1; $tips.html(count); if($tips.html() <= "0") { self.countdownStatus = false; clearTimeout(countdown); $this.html('重新获取'); $this.data('pending', false); $this.data('phone', ""); return false; } else { countdown($tips); } },1000); }; $this.data('ajax', false).ajaxRequest({ "url": url, "data": data, "callBack": function(result) { if(result.status) { $message.addClass('none'); $this.html('60秒后重试'); var $tips = $this.find('i'); if (!self.countdownStatus) { self.countdownStatus = true; countdown($tips); } } else { var $error = $form.find('dd > .error-msg'); $error.remove(); $this.html('重新获取'); $this.data('phone', ""); for(var i = 0; i < result.fieldError.length; i++){ $form.find('[name="' + result.fieldError[i].name + '"]').parent().append(''+ result.fieldError[i].errorMessage +''); } /*if ($phoneGroup.find('.error').length) { $message.removeClass('none').html(result.message); } else { $(''+ result.message +'').appendTo($phoneGroup.children('dd')); } $this.data('pending', false);*/ } } }) }, loginCustomer: function($this) { var $form = $this.closest('form'), url = $form.data('action'), data = $form.serializeArray(); if($this.data('pending')) return false; $this.data('pending', true); $form.data('cansubmit', true); $form.find('[name="login_name"]').validate({'type': 'none'}, 'local'); $form.find('[name="login_pwd"]').validate({'leng': [6, 20]}, 'local'); if(!$form.data('cansubmit')) { $this.data('pending', false); return false; }; $this.data('ajax', false).ajaxRequest({ "url": url, "data": data, "callBack": function(result) { if (result.status) { var checkboxStatus = $form.find('.fcheck'), login_name = $form.find('[name="login_name"]').val(), login_pwd = $form.find('[name="login_pwd"]').val(); if (checkboxStatus.hasClass('checked')) { utils.setCookie('customer_login_name', login_name); utils.setCookie('customer_login_pwd', login_pwd); }else { utils.delCookie('customer_login_name'); utils.delCookie('customer_login_pwd'); } window.location.href = result.location; } else { $form.data('validate', false).validate(result, 'overall'); $this.data('pending', false); } } }) }, /** * @Description 页面初始化时,从Cookie中读取电话号码和密码,渲染页面 * @param $obj 需要渲染的元素所在区域 */ setLoginNumberCustomer: function($obj) { var phoneNumber = utils.getCookie('customer_login_name'), password = utils.getCookie('customer_login_pwd'), $phoneInput = $obj.find('[name="login_name"]'), $passwordInput = $obj.find('[name="login_pwd"]'), $checkbox = $obj.find('.fcheck'); $phoneInput.val(phoneNumber); $passwordInput.val(password); phoneNumber && $checkbox.addClass('checked'); }, getParam: function(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return r[2]; return null; }, registerCustomer: function($obj) { var self = this, $this = $obj, $form = $this.closest('form'), url = $form.data('action'); $('.urlParam').val(self.getParam('registerCode')); var data = $form.serializeArray(); if($this.data('pending')) return false; $this.data('pending', true); $form.data('cansubmit', true); $form.find('[name="phone"]').validate({'type': 'phone'}, 'local'); $form.find('[name="verifyCode"]').validate({'exact': 4}, 'local'); $form.find('.getCode').validate({'type': 'isActive'}, 'local'); $form.find('[name="code"]').validate({'exact': 6}, 'local'); $form.find('[name="login_pwd"]').validate({'leng': [6, 20]}, 'local'); if(!$form.data('cansubmit')) { $this.data('pending', false); return false; }; $this.data('ajax', false).ajaxRequest({ "url": url, "data": data, "callBack": function(result) { if (result.status) { window._hmt.push(['_trackEvent', 'REGISTER','finished', 'customer', 'pc']); /*var countdownRegister = function($tips) { setTimeout(function(){ var count = $tips.text() - 1; $tips.html(count); if($tips.html() <= "0") { self.countdownStatusRegister = false; clearTimeout(countdownRegister); window.location.href = result.location; $this.data('pending', false); return false; } else { countdownRegister($tips); } },1000); }; if($('.js-registerAgreement').length){ $('.js-registerAgreement').after('
您已注册成功,3秒后自动登录...
'); var $tips = $('.successMessage').find('i'); if (!self.countdownStatusRegister) { self.countdownStatusRegister = true; $this.addClass('btn-register-gray'); countdownRegister($tips); } }else{ window.location.href = result.location; }*/ self.showWeixinCodeImgModal(result.location); } else { $form.data('validate', false).validate(result, 'overall'); $this.data('pending', false); } } }) }, // 显示二维码弹窗。(注册成功后,显示一个扫描二维码的弹窗。点击确定后跳转到淘金客系统首页) showWeixinCodeImgModal: function(url) { var self = this, $tmp = $('#showWeixinCodeImgModal'), $modal = $(handlebars.compile($tmp.html())()); $modal.appendTo($('body')); $modal.fmodal({ show: true, // backdrop: 'static', afterShow: function() { $modal.find('.submit-btn').on('click', function(){ window.location.href = url; }); } }) }, login: function($this) { var $form = $this.closest('form'), url = $form.data('action'), data = $form.serializeArray(); if($this.data('pending')) return false; $this.data('pending', true); $form.data('cansubmit', true); $form.find('[name="phone"]').validate({'type': 'none'}, 'local'); $form.find('[name="password"]').validate({'leng': [6, 20]}, 'local'); if(!$form.data('cansubmit')) { $this.data('pending', false); return false; }; $this.data('ajax', false).ajaxRequest({ "url": url, "data": data, "callBack": function(result) { if (result.status) { window.location.href = result.location; } else { $form.data('validate', false).validate(result, 'overall'); $this.data('pending', false); } } }) }, register: function($this) { var self = this, $form = $this.closest('form'), url = $form.data('action'), data = $form.serializeArray(); if($this.data('pending')) return false; $this.data('pending', true); $form.data('cansubmit', true); $form.find('[name="phone"]').validate({'type': 'phone'}, 'local'); $form.find('.getCode').validate({'type': 'isActive'}, 'local'); $form.find('[name="code"]').validate({'exact': 6}, 'local'); $form.find('[name="password"]').validate({'leng': [6, 20]}, 'local'); $form.find('[name="checkbox"]').validate({'type': 'checked'}, 'local'); if(!$form.data('cansubmit')) { $this.data('pending', false); return false; }; $this.data('ajax', false).ajaxRequest({ "url": url, "data": data, "callBack": function(result) { if (result.status) { var countdownRegister = function($tips) { setTimeout(function(){ var count = $tips.text() - 1; $tips.html(count); if($tips.html() <= "0") { self.countdownStatusRegister = false; clearTimeout(countdownRegister); window.location.href = result.location; $this.data('pending', false); return false; } else { countdownRegister($tips); } },1000); }; $('.btn').after('您已注册成功,3秒后自动登录...
'); var $tips = $('.successMessage').find('i'); if (!self.countdownStatusRegister) { self.countdownStatusRegister = true; $this.addClass('btn-register-gray'); countdownRegister($tips); } } else { $form.data('validate', false).validate(result, 'overall'); $this.data('pending', false); } } }) }, removeError: function($this) { var $dl = $this.closest('dl'), $error = $dl.find('.error'); $error.remove(); }, logout: function($this) { var url = $this.data('url'); $.post(url).done(function(result) { if (result.status) { window.location.reload(); } else { $.alert(result.message); } }) }, getLoginStatus: function() { var self = this, $nonea = $('.m-header').length ? $('.m-header .nonea.reg') : $('.header .nonea.reg'), $btns = $('.js-anonymous-status'), $recruit = $('.js-recruit'), url = $nonea.data('url'); $.post(url).done(function(result) { if (result.status) { var thumb = result.data.headimgurl ? result.data.headimgurl : '/static/image/merchantThumb.png', title = result.data.companyName ? result.data.companyName : result.data.title ? result.data.title : result.data.loginName; var $str = $(''+ title +'\ \ '); $nonea.html($str); $btns.off('click').html('进入我的淘金').removeClass('js-register').attr('href', $nonea.data('home')); $recruit.off('click').removeClass('js-register').attr('href', '/merchant/action/recruit/index'); } }) }, /** * 右侧导航-了解更多-留言 */ knowmoreFun: function($this) { var $form = $this.closest('form'), url = $form.data('action'), data = $form.serializeArray(); $form.data('cansubmit', true); $form.find('[name="phone"]').validate({'exact': 11}, 'local'); $form.find('[name="name"]').validate({'type': 'none'}, 'local'); if(!$form.data('cansubmit')) { $this.data('pending', false); return false; }; if($this.data('pending')) return false; $this.data('pending', true); $this.data('ajax', false).ajaxRequest({ "url": url, "data": data, "callBack": function(result) { if (result.status) { $('.pop-modal').addClass('none'); $('#merchant-knowmore').after('亲,我们将在一个工作日内联系到您,请保持手机通畅!