v1.0.0 bug修复更新
This commit is contained in:
40
js/common.js
40
js/common.js
@@ -1,8 +1,5 @@
|
||||
/**
|
||||
* PerToolBox Front - 公共 JavaScript
|
||||
* Copyright (C) 2024 Sea Network Technology Studio
|
||||
* Author: Canglan <admin@sea-studio.top>
|
||||
* License: AGPL v3
|
||||
*/
|
||||
|
||||
// ========== 全局变量 ==========
|
||||
@@ -27,7 +24,10 @@ function isLoggedIn() {
|
||||
|
||||
// ========== API 请求封装 ==========
|
||||
async function apiRequest(endpoint, options = {}) {
|
||||
const url = endpoint.startsWith('http') ? endpoint : `${window.API_BASE}${endpoint}`;
|
||||
// 使用 window.API_BASE,如果未定义则使用默认值
|
||||
const baseUrl = window.API_BASE || '/api/v1';
|
||||
const url = endpoint.startsWith('http') ? endpoint : `${baseUrl}${endpoint}`;
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers
|
||||
@@ -45,7 +45,6 @@ async function apiRequest(endpoint, options = {}) {
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
// token 失效,清除本地存储并跳转登录页
|
||||
setToken(null);
|
||||
if (!window.location.pathname.includes('/login.php')) {
|
||||
window.location.href = '/login.php';
|
||||
@@ -68,6 +67,7 @@ async function apiRequest(endpoint, options = {}) {
|
||||
async function recordUsage(toolName) {
|
||||
try {
|
||||
await apiRequest(`/tool/usage?tool_name=${toolName}`, { method: 'POST' });
|
||||
console.log(`✅ 热度上报: ${toolName}`);
|
||||
} catch (error) {
|
||||
console.warn('热度上报失败:', error);
|
||||
}
|
||||
@@ -102,17 +102,19 @@ function updateUserUI(user) {
|
||||
|
||||
if (user) {
|
||||
const displayName = user.username || user.phone || user.email || '用户';
|
||||
userInfoDiv.innerHTML = `
|
||||
<div class="px-6 py-3 bg-blue-800 rounded-lg mx-4 mb-2">
|
||||
<div class="text-sm text-blue-200">欢迎,</div>
|
||||
<div class="font-semibold">${escapeHtml(displayName)}</div>
|
||||
</div>
|
||||
`;
|
||||
if (userInfoDiv) {
|
||||
userInfoDiv.innerHTML = `
|
||||
<div class="px-6 py-3 bg-blue-800 rounded-lg mx-4 mb-2">
|
||||
<div class="text-sm text-blue-200">欢迎,</div>
|
||||
<div class="font-semibold">${escapeHtml(displayName)}</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
if (profileLink) profileLink.style.display = 'flex';
|
||||
if (logoutBtn) logoutBtn.style.display = 'flex';
|
||||
if (loginLink) loginLink.style.display = 'none';
|
||||
} else {
|
||||
userInfoDiv.innerHTML = '';
|
||||
if (userInfoDiv) userInfoDiv.innerHTML = '';
|
||||
if (profileLink) profileLink.style.display = 'none';
|
||||
if (logoutBtn) logoutBtn.style.display = 'none';
|
||||
if (loginLink) loginLink.style.display = 'flex';
|
||||
@@ -149,17 +151,6 @@ function initSidebar() {
|
||||
if (menuBtn) menuBtn.addEventListener('click', openSidebar);
|
||||
if (closeBtn) closeBtn.addEventListener('click', closeSidebar);
|
||||
if (overlay) overlay.addEventListener('click', closeSidebar);
|
||||
|
||||
// 点击侧边栏内的链接后自动关闭(移动端)
|
||||
if (sidebar) {
|
||||
sidebar.querySelectorAll('a').forEach(link => {
|
||||
link.addEventListener('click', () => {
|
||||
if (window.innerWidth < 768) {
|
||||
closeSidebar();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ========== HTML 转义 ==========
|
||||
@@ -186,7 +177,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
initSidebar();
|
||||
loadUserInfo();
|
||||
|
||||
// 绑定退出按钮
|
||||
const logoutBtn = document.getElementById('logoutBtn');
|
||||
if (logoutBtn) {
|
||||
logoutBtn.addEventListener('click', (e) => {
|
||||
@@ -194,4 +184,4 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
logout();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user