fix: 彻底重写 syncLoginStatus 用 jwt 直接判断
All checks were successful
continuous-integration/drone/push Build is passing

根因: MutationObserver + CSS display检测 形成死循环,
先 common.js AJAX 隐藏按钮, 然后 observer 触发
syncLoginStatus 又错误显示登录按钮.

修复: syncLoginStatus 不再检测 CSS display 状态,
直接用 localStorage.jwt 判断:
- 有 jwt → 隐藏登录/注册, 显示已登录
- 无 jwt → 显示登录/注册, 隐藏已登录

同时 common.js certification 接口失败不再
影响登录按钮显示
This commit is contained in:
yiqiu
2026-03-20 20:14:28 +08:00
parent adf0e8a58f
commit cd8f7b87a2

View File

@@ -45,53 +45,33 @@ $(function () {
}); });
}); });
// 同步桌面端和移动端的登录状态 // 同步桌面端和移动端的登录状态(直接用 jwt 判断)
function syncLoginStatus() { function syncLoginStatus() {
const desktopNoLogin = document.querySelector('.nav-desktop-link.no-login'); const desktopNoLogin = document.querySelector('.nav-desktop-link.no-login');
const desktopLoginIn = document.querySelector('.nav-desktop-link.login-in'); const desktopLoginIn = document.querySelector('.nav-desktop-link.login-in');
const mobileNoLogin = document.querySelector('.mobile-no-login'); const mobileNoLogin = document.querySelector('.mobile-no-login');
const mobileLoginIn = document.querySelector('.mobile-login-in'); const mobileLoginIn = document.querySelector('.mobile-login-in');
// 使用计算后的样式而不是 inline style if (localStorage.jwt) {
const noLoginComputedDisplay = desktopNoLogin ? window.getComputedStyle(desktopNoLogin).display : 'none'; // 有 jwt: 隐藏登录按钮, 显示已登录区域
const loginInComputedDisplay = desktopLoginIn ? window.getComputedStyle(desktopLoginIn).display : 'none';
// 检查桌面端登录状态
if (desktopNoLogin && noLoginComputedDisplay !== 'none') {
// 未登录
desktopNoLogin.style.display = 'flex';
if (desktopLoginIn) desktopLoginIn.style.display = 'none';
if (mobileNoLogin) mobileNoLogin.style.display = 'block';
if (mobileLoginIn) mobileLoginIn.style.display = 'none';
} else if (desktopLoginIn && loginInComputedDisplay !== 'none') {
// 已登录
if (desktopNoLogin) desktopNoLogin.style.display = 'none'; if (desktopNoLogin) desktopNoLogin.style.display = 'none';
desktopLoginIn.style.display = 'flex'; if (desktopLoginIn) desktopLoginIn.style.display = 'flex';
if (mobileNoLogin) mobileNoLogin.style.display = 'none'; if (mobileNoLogin) mobileNoLogin.style.display = 'none';
if (mobileLoginIn) mobileLoginIn.style.display = 'block'; if (mobileLoginIn) mobileLoginIn.style.display = 'block';
// 同步用户信息 // 同步用户信息到移动端
const desktopUsername = document.getElementById('username'); var du = document.getElementById('username');
const desktopHeadImg = document.getElementById('headImg'); var dh = document.getElementById('headImg');
const mobileUsername = document.getElementById('mobileUsername'); var mu = document.getElementById('mobileUsername');
const mobileHeadImg = document.getElementById('mobileHeadImg'); var mh = document.getElementById('mobileHeadImg');
if (du && mu) mu.textContent = du.textContent;
if (desktopUsername && mobileUsername) { if (dh && mh) mh.textContent = dh.textContent;
mobileUsername.textContent = desktopUsername.textContent;
}
if (desktopHeadImg && mobileHeadImg) {
mobileHeadImg.textContent = desktopHeadImg.textContent;
}
} else { } else {
// 两者都隐藏 — 可能是 AJAX 尚未返回 // 无 jwt: 显示登录按钮
// 若有 jwt 则不做操作, 等 common.js AJAX 回调处理 if (desktopNoLogin) desktopNoLogin.style.display = 'flex';
if (!localStorage.jwt) { if (desktopLoginIn) desktopLoginIn.style.display = 'none';
// 确认未登录, 显示登录按钮 if (mobileNoLogin) mobileNoLogin.style.display = 'block';
if (desktopNoLogin) desktopNoLogin.style.display = 'flex'; if (mobileLoginIn) mobileLoginIn.style.display = 'none';
if (desktopLoginIn) desktopLoginIn.style.display = 'none';
if (mobileNoLogin) mobileNoLogin.style.display = 'block';
if (mobileLoginIn) mobileLoginIn.style.display = 'none';
}
} }
} }