define('views/login', ['view'], function (Dep) { // 添加全局调试日志 console.log('登录视图模块被加载'); var LoginView = Dep.extend({ template: 'login', data: function () { return { logoSrc: this.getLogoSrc(), hasSignIn: false, showForgotPassword: true, logInText: this.translate('Log in', 'labels', 'User') }; }, events: { 'submit #login-form': function (e) { e.preventDefault(); this.login(); }, 'click a[data-action="passwordChangeRequest"]': function () { this.getRouter().navigate('#PasswordChangeRequest', {trigger: true}); }, 'click .toggle-password': function (e) { var $target = $(e.currentTarget); var $input = $target.closest('.input-group').find('input'); var $icon = $target.find('i'); if ($input.attr('type') === 'password') { $input.attr('type', 'text'); $icon.removeClass('ri-eye-off-line').addClass('ri-eye-line'); } else { $input.attr('type', 'password'); $icon.removeClass('ri-eye-line').addClass('ri-eye-off-line'); } }, 'click .social-btn[data-provider="google"]': function (e) { e.preventDefault(); e.stopPropagation(); console.log('Google登录按钮点击(事件委托)'); this.handleSocialLogin('google'); return false; }, 'click .social-btn[data-provider="wechat"]': function (e) { e.preventDefault(); e.stopPropagation(); console.log('微信登录按钮点击(事件委托)'); this.handleSocialLogin('wechat'); return false; } }, setup: function () { this.getRouter().confirmLeaveOut = false; console.log('登录视图设置开始'); // 检查EspoCRM的视图系统 if (this.getRouter) { console.log('Router可用'); } if (this.getConfig) { console.log('Config可用'); } // 打印当前模板名称 console.log('当前模板:', this.template); // 监听模板渲染事件 this.on('render', function() { console.log('视图开始渲染'); }); this.once('after:render', function () { console.log('视图渲染完成'); console.log('DOM元素:', this.$el.length ? '已找到' : '未找到'); // 检查按钮是否存在 var $googleBtn = this.$el.find('.social-btn[data-provider="google"]'); var $wechatBtn = this.$el.find('.social-btn[data-provider="wechat"]'); console.log('Google按钮:', $googleBtn.length); console.log('微信按钮:', $wechatBtn.length); // 确保事件处理程序已绑定 this.bindSocialLoginEvents(); }.bind(this)); // 尝试使用EspoCRM的API if (Espo && Espo.Events) { console.log('使用Espo.Events添加事件处理程序'); Espo.Events.on('login:render', function() { console.log('登录视图渲染事件触发'); $('[data-action="googleLogin"]').on('click', function() { console.log('Google登录按钮点击 (通过Espo.Events)'); alert('将重定向到Google登录页面(示例)'); }); }); } }, bindSocialLoginEvents: function () { console.log('绑定社交登录事件'); // 使用jQuery的on方法绑定事件 this.$el.on('click', '.social-btn[data-provider="google"]', function (e) { e.preventDefault(); e.stopPropagation(); console.log('Google登录按钮点击(手动绑定)'); this.handleSocialLogin('google'); return false; }.bind(this)); this.$el.on('click', '.social-btn[data-provider="wechat"]', function (e) { e.preventDefault(); e.stopPropagation(); console.log('微信登录按钮点击(手动绑定)'); this.handleSocialLogin('wechat'); return false; }.bind(this)); }, handleSocialLogin: function (provider) { console.log('处理社交登录:', provider); // 显示toast this.showToast('正在尝试使用' + provider + '登录...'); // 处理不同的登录提供商 if (provider === 'google') { alert('将重定向到Google登录页面(示例)'); // 实际实现中,这里应该重定向到Google OAuth页面 } else if (provider === 'wechat') { alert('微信登录尚未实现'); // 实际实现中,这里应该显示微信二维码 } }, getLogoSrc: function () { var companyLogoId = this.getConfig().get('companyLogoId'); if (!companyLogoId) { return this.getBasePath() + (this.getThemeManager().getParam('logo') || 'client/img/logo.png'); } return this.getBasePath() + '?entryPoint=LogoImage&id=' + companyLogoId; }, login: function () { var username = this.$el.find('input[name="username"]').val(); var password = this.$el.find('input[name="password"]').val(); if (username === '') { this.showErrorMessage(this.translate('userCantBeEmpty', 'messages', 'User')); return; } this.disableForm(); var xhr = new XMLHttpRequest(); xhr.open('POST', this.getBasePath() + '?entryPoint=login', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function () { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status === 200) { this.onLoginSuccess(xhr.responseText); } else { this.onLoginFail(xhr.responseText); } } }.bind(this); xhr.send('username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password)); }, disableForm: function () { this.$el.find('button[type="submit"]').addClass('disabled').attr('disabled', 'disabled'); }, enableForm: function () { this.$el.find('button[type="submit"]').removeClass('disabled').removeAttr('disabled'); }, onLoginSuccess: function (responseText) { try { var data = JSON.parse(responseText); if (data.status === 'success') { this.trigger('login', data.user); this.getRouter().navigate('', {trigger: true}); } else if (data.status === 'second-step') { this.onSecondStep(data); } else { this.onLoginFail(responseText); } } catch (e) { this.onLoginFail(responseText); } }, onSecondStep: function (data) { // 处理两步验证 this.getRouter().navigate('Login/secondStep', {trigger: true}); }, onLoginFail: function (responseText) { this.enableForm(); try { var data = JSON.parse(responseText); if (data.status === 'error') { this.showErrorMessage(data.message); } else { this.showErrorMessage(this.translate('Error occurred')); } } catch (e) { this.showErrorMessage(this.translate('Error occurred')); } }, showErrorMessage: function (message) { this.showToast(message, 'error'); }, showToast: function (message, type) { type = type || 'info'; var $toast = $('#toast'); if (!$toast.length) { $toast = $('
').css({ position: 'fixed', top: '16px', right: '16px', backgroundColor: '#333', color: 'white', padding: '12px 24px', borderRadius: '8px', transform: 'translateX(100%)', transition: 'transform 0.3s, opacity 0.3s', opacity: '0', zIndex: '9999', boxShadow: '0 4px 6px rgba(0, 0, 0, 0.1)' }).appendTo('body'); } $toast.text(message); if (type === 'error') { $toast.css('backgroundColor', '#e74c3c'); } else if (type === 'warning') { $toast.css('backgroundColor', '#f39c12'); } else if (type === 'success') { $toast.css('backgroundColor', '#2ecc71'); } else { $toast.css('backgroundColor', '#333'); } $toast.css({ transform: 'translateX(0)', opacity: '1' }); setTimeout(function() { $toast.css({ transform: 'translateX(100%)', opacity: '0' }); }, 3000); }, afterRender: function () { if (typeof Dep.prototype.afterRender === 'function') { Dep.prototype.afterRender.call(this); } console.log('afterRender方法被调用'); // 直接修改DOM var $loginForm = this.$el.find('#login-form'); if ($loginForm.length) { console.log('找到登录表单'); // 检查社交登录按钮是否已存在 var $socialBtns = $loginForm.find('.social-btn'); console.log('现有社交登录按钮数量:', $socialBtns.length); // 为按钮添加事件处理程序 $socialBtns.each(function() { var $btn = $(this); var provider = $btn.data('provider'); $btn.off('click').on('click', function(e) { e.preventDefault(); e.stopPropagation(); console.log('社交登录按钮点击(afterRender):', provider); if (provider === 'google') { alert('将重定向到Google登录页面(示例)'); } else if (provider === 'wechat') { alert('微信登录尚未实现'); } return false; }); }); } } }); // 添加更多调试信息 console.log('登录视图定义完成'); return LoginView; });