ts
This commit is contained in:
+42
-30
@@ -144,6 +144,9 @@ function init() {
|
||||
// 加载主题
|
||||
loadTheme();
|
||||
|
||||
// 加载登录状态
|
||||
loadAccountStatus();
|
||||
|
||||
// 加载初始数据
|
||||
loadInitialData();
|
||||
|
||||
@@ -692,42 +695,36 @@ function closeAccountModal() {
|
||||
// 打开登录二维码模态框
|
||||
async function openLoginQrModal() {
|
||||
elements.loginQrModal.classList.add('show');
|
||||
elements.qrLoginStatus.textContent = '正在生成二维码...';
|
||||
elements.qrLoginStatus.textContent = '正在模拟登录...';
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/login/qrcode');
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
// 显示二维码
|
||||
if (data.qr_url) {
|
||||
// 使用简单的二维码显示(实际项目中可以使用QRCode.js等库)
|
||||
elements.qrcodeContainer.innerHTML = `
|
||||
<div style="text-align: center;">
|
||||
<img src="https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(data.qr_url)}" alt="登录二维码" style="max-width: 100%;">
|
||||
</div>
|
||||
`;
|
||||
elements.qrLoginStatus.textContent = '请扫码登录';
|
||||
// 显示登录成功信息
|
||||
elements.qrcodeContainer.innerHTML = `
|
||||
<div style="text-align: center; padding: 40px;">
|
||||
<div style="font-size: 48px; margin-bottom: 20px;">✅</div>
|
||||
<h3 style="color: var(--success-color); margin-bottom: 10px;">${data.message}</h3>
|
||||
<p style="color: var(--text-secondary);">登录状态已保存到本地</p>
|
||||
${data.expire_in_hours ? `<p style="color: var(--text-muted); font-size: 14px;">有效期:${data.expire_in_hours}小时</p>` : ''}
|
||||
</div>
|
||||
`;
|
||||
elements.qrLoginStatus.textContent = '登录成功!';
|
||||
|
||||
// 开始检查登录状态
|
||||
checkLoginStatus();
|
||||
// 开始检查登录状态
|
||||
checkLoginStatus();
|
||||
|
||||
// 设置二维码过期倒计时
|
||||
let countdown = 120;
|
||||
const countdownInterval = setInterval(() => {
|
||||
countdown--;
|
||||
elements.qrExpireTime.textContent = `${countdown}秒`;
|
||||
|
||||
if (countdown <= 0) {
|
||||
clearInterval(countdownInterval);
|
||||
elements.qrLoginStatus.textContent = '二维码已过期,请重新生成';
|
||||
elements.qrcodeContainer.innerHTML = '<p style="color: var(--error-color);">二维码已过期</p>';
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
// 3秒后自动关闭
|
||||
setTimeout(() => {
|
||||
closeLoginQrModal();
|
||||
closeAccountModal();
|
||||
showToast('登录成功', 'success');
|
||||
}, 3000);
|
||||
} else {
|
||||
elements.qrcodeContainer.innerHTML = '<p style="color: var(--error-color);">生成二维码失败</p>';
|
||||
elements.qrLoginStatus.textContent = '生成失败';
|
||||
elements.qrcodeContainer.innerHTML = '<p style="color: var(--error-color);">登录失败</p>';
|
||||
elements.qrLoginStatus.textContent = '登录失败';
|
||||
}
|
||||
} catch (error) {
|
||||
elements.qrcodeContainer.innerHTML = '<p style="color: var(--error-color);">网络错误</p>';
|
||||
@@ -751,7 +748,7 @@ async function checkLoginStatus() {
|
||||
setTimeout(() => {
|
||||
closeLoginQrModal();
|
||||
closeAccountModal();
|
||||
updateLoginStatus(true, data.login_time);
|
||||
updateLoginStatus(true, data.login_time, data.expire_time);
|
||||
showToast('登录成功', 'success');
|
||||
}, 1000);
|
||||
} else {
|
||||
@@ -783,7 +780,7 @@ async function logout() {
|
||||
}
|
||||
|
||||
// 更新登录状态显示
|
||||
function updateLoginStatus(isLoggedIn, loginTime = null) {
|
||||
function updateLoginStatus(isLoggedIn, loginTime = null, expireTime = null) {
|
||||
const indicator = elements.loginStatusIndicator;
|
||||
const statusText = elements.loginStatusText;
|
||||
const modalStatus = elements.modalLoginStatus;
|
||||
@@ -797,6 +794,21 @@ function updateLoginStatus(isLoggedIn, loginTime = null) {
|
||||
if (loginTime) {
|
||||
elements.loginTime.textContent = new Date(loginTime).toLocaleString('zh-CN');
|
||||
}
|
||||
|
||||
// 如果有到期时间,显示剩余时间
|
||||
if (expireTime) {
|
||||
const expireDate = new Date(expireTime);
|
||||
const now = new Date();
|
||||
const timeLeft = expireDate - now;
|
||||
|
||||
if (timeLeft > 0) {
|
||||
const hours = Math.floor(timeLeft / (1000 * 60 * 60));
|
||||
const minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
|
||||
|
||||
elements.loginTime.textContent = new Date(loginTime).toLocaleString('zh-CN');
|
||||
elements.loginTime.innerHTML += `<br><span style="color: var(--warning-color); font-size: 12px;">(${hours}小时${minutes}分钟后过期)</span>`;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
indicator.className = 'status-indicator offline';
|
||||
statusText.textContent = '未登录';
|
||||
@@ -811,7 +823,7 @@ async function loadAccountStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/login/status');
|
||||
const data = await response.json();
|
||||
updateLoginStatus(data.logged_in, data.login_time);
|
||||
updateLoginStatus(data.logged_in, data.login_time, data.expire_time);
|
||||
} catch (error) {
|
||||
updateLoginStatus(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user