/* 登录页主逻辑:视图切换 / 验证码冷却 / 三种登录方式 */ (function () { /* ===== Toast ===== */ let toastTimer; function toast(msg, ms = 2600) { const el = document.getElementById('toast'); el.textContent = msg; el.classList.add('show'); clearTimeout(toastTimer); toastTimer = setTimeout(() => el.classList.remove('show'), ms); } function toastByKey(key, ms) { toast(t(key), ms); } function toastRaw(msg, ms) { toast(msg, ms); } /* ===== 语言切换 ===== */ document.getElementById('langSwitcher').addEventListener('click', () => toggleLang()); document.addEventListener('click', (e) => { if (!e.target.closest('#langSwitcher')) toggleLang(false); }); applyLang(); /* ===== 视图切换 ===== */ const qrArea = document.getElementById('qrArea'); document.querySelectorAll('.tab, [data-goto]').forEach(el => { el.addEventListener('click', () => switchView(el.dataset.view || el.dataset.goto)); }); function switchView(name) { document.querySelectorAll('.tab').forEach(x => x.classList.toggle('active', x.dataset.view === name)); document.querySelectorAll('.view').forEach(x => x.classList.toggle('active', x.id === 'view-' + name)); qrArea.style.display = name === 'account' ? 'none' : 'flex'; clearErrors(); } function clearErrors() { document.querySelectorAll('.form-error').forEach(e => e.textContent = ''); } function showErr(id, msg) { document.getElementById(id).textContent = msg || ''; } /* ===== 协议勾选 ===== */ const agreeCheck = document.getElementById('agreeCheck'); function requireAgree() { if (agreeCheck.checked) return true; toastByKey('toastAgree'); const box = document.getElementById('agreementBox'); box.classList.remove('shake'); void box.offsetWidth; box.classList.add('shake'); return false; } /* ===== 验证码按钮冷却(60s) ===== */ function cooldown(btn) { let s = 60; btn.disabled = true; const tick = () => { btn.textContent = t('resend').replace('{s}', s); if (--s > 0) setTimeout(tick, 1000); else { btn.disabled = false; btn.textContent = t('getCode'); } }; tick(); } /* ===== 登录成功 ===== */ function loginOk(user, isNew) { document.getElementById('successDetail').textContent = user + (isNew ? ' · ' + t('toastSilentReg') : ''); document.getElementById('successMask').classList.add('show'); } function handleApiError(err, errElId) { if (err.code === CODE.IP_FROZEN) { showErr(errElId, t('toastFrozen')); return; } if (err.code === CODE.CODE_INVALID) { toastByKey('toastCodeErr'); return; } if (err.code === CODE.INVALID_CREDENTIALS) { const left = err.data && err.data.remainAttempts; showErr(errElId, t('toastAccErr') + (left != null ? `(${left})` : '')); return; } if (err.code === CODE.SEND_TOO_FREQUENT) { toastRaw((err.data && err.data.retryAfter ? err.data.retryAfter + 's ' : '') + t('toastCodeErr')); return; } toastByKey('toastNetErr'); } /* ===== 手机号验证码登录 ===== */ const phoneInput = document.getElementById('phoneInput'); const phoneCodeInput = document.getElementById('phoneCodeInput'); const phoneSendBtn = document.getElementById('phoneSendBtn'); const isPhone = v => /^1\d{10}$|^\d{8,11}$/.test(v); phoneSendBtn.addEventListener('click', async () => { if (phoneSendBtn.disabled) return; if (!isPhone(phoneInput.value)) { showErr('phoneError', t('phoneErr')); return; } if (!requireAgree()) return; try { const data = await Api.sendCode('phone', phoneInput.value); cooldown(phoneSendBtn); clearErrors(); // 演示环境后端会回传演示验证码,便于联调;生产环境 devCode 为空 toastRaw(t('toastCodeSent').replace('{code}', data.devCode || '****'), 4200); } catch (err) { handleApiError(err, 'phoneError'); } }); document.getElementById('phoneLoginBtn').addEventListener('click', async () => { if (!isPhone(phoneInput.value)) { showErr('phoneError', t('phoneErr')); return; } if (!phoneCodeInput.value) { toastByKey('toastCodeErr'); return; } try { const data = await Api.loginByCode('phone', phoneInput.value, phoneCodeInput.value); clearErrors(); loginOk(data.user.displayId, data.isNewUser); toastByKey(data.isNewUser ? 'toastSilentReg' : 'toastLoginOk'); } catch (err) { handleApiError(err, 'phoneError'); } }); /* ===== 邮箱验证码登录 ===== */ const emailInput = document.getElementById('emailInput'); const emailCodeInput = document.getElementById('emailCodeInput'); const emailSendBtn = document.getElementById('emailSendBtn'); const isEmail = v => /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v); emailSendBtn.addEventListener('click', async () => { if (emailSendBtn.disabled) return; if (!isEmail(emailInput.value)) { showErr('emailError', t('emailErr')); return; } if (!requireAgree()) return; try { const data = await Api.sendCode('email', emailInput.value); cooldown(emailSendBtn); clearErrors(); toastRaw(t('toastCodeSent').replace('{code}', data.devCode || '****'), 4200); } catch (err) { handleApiError(err, 'emailError'); } }); document.getElementById('emailLoginBtn').addEventListener('click', async () => { if (!isEmail(emailInput.value)) { showErr('emailError', t('emailErr')); return; } if (!emailCodeInput.value) { toastByKey('toastCodeErr'); return; } try { const data = await Api.loginByCode('email', emailInput.value, emailCodeInput.value); clearErrors(); loginOk(data.user.displayId, data.isNewUser); toastByKey(data.isNewUser ? 'toastSilentReg' : 'toastLoginOk'); } catch (err) { handleApiError(err, 'emailError'); } }); /* ===== 账号密码登录 ===== */ document.getElementById('accLoginBtn').addEventListener('click', async () => { const account = document.getElementById('accInput').value.trim(); const password = document.getElementById('pwdInput').value; if (!account || !password) { showErr('accError', t('toastAccErr')); return; } try { const data = await Api.loginByPassword(account, password); clearErrors(); loginOk(data.user.displayId, false); toastByKey('toastLoginOk'); } catch (err) { handleApiError(err, 'accError'); } }); /* ===== 忘记密码 ===== */ document.getElementById('forgotBtn').addEventListener('click', async () => { const account = document.getElementById('accInput').value.trim(); try { await Api.forgotPassword(account); toastByKey('toastForgot'); } catch (err) { handleApiError(err, 'accError'); } }); /* ===== 微信扫码(MVP 预留) ===== */ qrArea.addEventListener('click', () => { qrArea.classList.add('show-tip'); setTimeout(() => qrArea.classList.remove('show-tip'), 2200); toastByKey('toastWx'); }); })();