+
+
+
-
-
-
-
-
-
-
+ // 忘记密码弹窗
+ if (linkForgot && modalForgot && btnModalClose) {
+ linkForgot.addEventListener('click', function () {
+ modalForgot.classList.add('active');
+ });
+ btnModalClose.addEventListener('click', function () {
+ modalForgot.classList.remove('active');
+ });
+ modalForgot.addEventListener('click', function (e) {
+ if (e.target === modalForgot) {
+ modalForgot.classList.remove('active');
+ }
+ });
+ }
+
+ // 轻量 Toast 提示
+ function showToast(message, type) {
+ type = type || 'info';
+ var container = document.getElementById('toast-container');
+ if (!container) return;
+
+ var item = document.createElement('div');
+ item.className = 'toast-item toast-' + type;
+ item.textContent = message;
+ container.appendChild(item);
+
+ // 渐入
+ requestAnimationFrame(function () {
+ item.classList.add('show');
+ });
+
+ // 3秒后移除
+ setTimeout(function () {
+ item.classList.remove('show');
+ setTimeout(function () {
+ if (item.parentNode) {
+ item.parentNode.removeChild(item);
+ }
+ }, 300);
+ }, 2800);
+ }
+
+ // 设置 Loading 状态
+ function setLoading(loading) {
+ if (loading) {
+ btnSubmit.disabled = true;
+ btnSpinner.style.display = 'inline-block';
+ btnText.textContent = '登录验证中...';
+ } else {
+ btnSubmit.disabled = false;
+ btnSpinner.style.display = 'none';
+ btnText.textContent = '登 录 系 统';
+ }
+ }
+
+ // 表单提交
+ function handleSubmit() {
+ var username = usernameInput ? usernameInput.value.trim() : '';
+ var password = passwordInput ? passwordInput.value : '';
+ var captcha = captchaInput ? captchaInput.value.trim() : '';
+ var keeplogin = (keeploginInput && keeploginInput.checked) ? 1 : 0;
+ var tokenInput = document.querySelector('input[name="__token__"]');
+ var token = tokenInput ? tokenInput.value : '';
+
+ if (!username) {
+ showToast('请您输入管理员账号', 'warning');
+ if (usernameInput) usernameInput.focus();
+ return;
+ }
+ if (!password) {
+ showToast('请您输入登录密码', 'warning');
+ if (passwordInput) passwordInput.focus();
+ return;
+ }
+ if (captchaInput && !captcha) {
+ showToast('请您输入安全验证码', 'warning');
+ captchaInput.focus();
+ return;
+ }
+
+ setLoading(true);
+
+ // 组装 POST 数据
+ var postData = new URLSearchParams();
+ postData.append('username', username);
+ postData.append('password', password);
+ postData.append('keeplogin', keeplogin);
+ postData.append('__token__', token);
+ if (captchaInput) {
+ postData.append('captcha', captcha);
+ }
+
+ fetch('{:url("login")}', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
+ 'X-Requested-With': 'XMLHttpRequest'
+ },
+ body: postData.toString()
+ })
+ .then(function (response) {
+ return response.json();
+ })
+ .then(function (res) {
+ setLoading(false);
+ // 如果后端返回了新 token,更新页面隐藏域
+ if (res.data && res.data.token && tokenInput) {
+ tokenInput.value = res.data.token;
+ }
+
+ if (res.code === 1) {
+ showToast(res.msg || '登录成功,正在进入系统...', 'success');
+ setTimeout(function () {
+ window.location.href = res.url || 'index/index';
+ }, 800);
+ } else {
+ // 登录失败:刷新验证码并更新 token
+ refreshCaptcha();
+ showToast(res.msg || '用户名或密码错误', 'error');
+ }
+ })
+ .catch(function (err) {
+ setLoading(false);
+ refreshCaptcha();
+ showToast('网络通信异常,请重试或检查服务状态', 'error');
+ });
+ }
+
+ // 按钮点击
+ btnSubmit.addEventListener('click', handleSubmit);
+
+ // 回车键快捷提交
+ var inputs = [usernameInput, passwordInput, captchaInput];
+ inputs.forEach(function (inp) {
+ if (inp) {
+ inp.addEventListener('keydown', function (e) {
+ if (e.key === 'Enter' || e.keyCode === 13) {
+ handleSubmit();
+ }
+ });
+ }
+ });
+
+ })();
+
+
\ No newline at end of file