优化结构

This commit is contained in:
zhinianboke 2025-08-03 20:55:31 +08:00
parent 7386db9496
commit 04a391616f

View File

@ -1,4 +1,4 @@
// 全局变量 // ==================== 全局变量 ====================
const apiBase = location.origin; const apiBase = location.origin;
let keywordsData = {}; let keywordsData = {};
let currentCookieId = ''; let currentCookieId = '';
@ -14,6 +14,8 @@ let accountKeywordCache = {};
let cacheTimestamp = 0; let cacheTimestamp = 0;
const CACHE_DURATION = 30000; // 30秒缓存 const CACHE_DURATION = 30000; // 30秒缓存
// ==================== 页面导航功能 ====================
// 菜单切换功能 // 菜单切换功能
function showSection(sectionName) { function showSection(sectionName) {
console.log('切换到页面:', sectionName); // 调试信息 console.log('切换到页面:', sectionName); // 调试信息
@ -46,7 +48,7 @@ function showSection(sectionName) {
}); });
// 根据不同section加载对应数据 // 根据不同section加载对应数据
switch(sectionName) { switch (sectionName) {
case 'dashboard': case 'dashboard':
loadDashboard(); loadDashboard();
break; break;
@ -99,6 +101,8 @@ function toggleSidebar() {
document.getElementById('sidebar').classList.toggle('show'); document.getElementById('sidebar').classList.toggle('show');
} }
// ==================== 仪表盘管理 ====================
// 加载仪表盘数据 // 加载仪表盘数据
async function loadDashboard() { async function loadDashboard() {
try { try {
@ -239,6 +243,8 @@ function updateDashboardAccountsList(accounts) {
}); });
} }
// ==================== 关键词缓存管理 ====================
// 获取账号关键词数量(带缓存)- 包含普通关键词和商品关键词 // 获取账号关键词数量(带缓存)- 包含普通关键词和商品关键词
async function getAccountKeywordCount(accountId) { async function getAccountKeywordCount(accountId) {
const now = Date.now(); const now = Date.now();
@ -280,6 +286,8 @@ function clearKeywordCache() {
cacheTimestamp = 0; cacheTimestamp = 0;
} }
// ==================== 账号列表管理 ====================
// 刷新账号列表(用于自动回复页面) // 刷新账号列表(用于自动回复页面)
async function refreshAccountList() { async function refreshAccountList() {
try { try {
@ -339,7 +347,7 @@ async function refreshAccountList() {
// 分组显示:先显示启用的账号,再显示禁用的账号 // 分组显示:先显示启用的账号,再显示禁用的账号
const enabledAccounts = accountsWithKeywords.filter(account => { const enabledAccounts = accountsWithKeywords.filter(account => {
const enabled = account.enabled === undefined ? true : account.enabled; const enabled = account.enabled === undefined ? true : account.enabled;
console.log(`账号 ${account.id} 过滤状态: enabled=${account.enabled}, 判断为启用=${enabled}`); // 调试信息 console.log(`账号 ${account.id} 过滤状态: enabled=${account.enabled}, 判断为启用=${enabled}`);
return enabled; return enabled;
}); });
const disabledAccounts = accountsWithKeywords.filter(account => { const disabledAccounts = accountsWithKeywords.filter(account => {
@ -399,7 +407,7 @@ async function refreshAccountList() {
}); });
} }
console.log('账号列表刷新完成,关键词统计:', accountsWithKeywords.map(a => ({id: a.id, keywords: a.keywordCount}))); console.log('账号列表刷新完成,关键词统计:', accountsWithKeywords.map(a => ({ id: a.id, keywords: a.keywordCount })));
} else { } else {
showToast('获取账号列表失败', 'danger'); showToast('获取账号列表失败', 'danger');
} }
@ -875,6 +883,8 @@ async function deleteKeyword(cookieId, index) {
} }
} }
// ==================== 通用工具函数 ====================
// 显示/隐藏加载动画 // 显示/隐藏加载动画
function toggleLoading(show) { function toggleLoading(show) {
document.getElementById('loading').classList.toggle('d-none', !show); document.getElementById('loading').classList.toggle('d-none', !show);
@ -959,6 +969,8 @@ async function fetchJSON(url, opts = {}) {
} }
} }
// ==================== Cookie管理 ====================
// 加载Cookie列表 // 加载Cookie列表
async function loadCookies() { async function loadCookies() {
try { try {
@ -1181,8 +1193,13 @@ async function delCookie(id) {
} }
} }
// 内联编辑Cookie /**
function editCookieInline(id, currentValue) { * 内联编辑Cookie
* @param {string} id - 账号ID
* @param {string} currentValue - 当前Cookie值
* @param {Event} event - 点击事件对象
*/
function editCookieInline(id, currentValue, event) {
const row = event.target.closest('tr'); const row = event.target.closest('tr');
const cookieValueCell = row.querySelector('.cookie-value'); const cookieValueCell = row.querySelector('.cookie-value');
const originalContent = cookieValueCell.innerHTML; const originalContent = cookieValueCell.innerHTML;
@ -1234,7 +1251,7 @@ function editCookieInline(id, currentValue) {
input.select(); input.select();
// 添加键盘事件监听 // 添加键盘事件监听
input.addEventListener('keydown', function(e) { input.addEventListener('keydown', function (e) {
if (e.key === 'Enter') { if (e.key === 'Enter') {
e.preventDefault(); e.preventDefault();
saveCookieInline(id); saveCookieInline(id);
@ -1562,11 +1579,14 @@ async function checkAuth() {
} }
} }
// ==================== 应用初始化 ====================
// 初始化事件监听 // 初始化事件监听
document.addEventListener('DOMContentLoaded', async () => { document.addEventListener('DOMContentLoaded', async () => {
// 首先检查认证状态 // 首先检查认证状态
const isAuthenticated = await checkAuth(); const isAuthenticated = await checkAuth();
if (!isAuthenticated) return; if (!isAuthenticated) return;
// 添加Cookie表单提交 // 添加Cookie表单提交
document.getElementById('addForm').addEventListener('submit', async (e) => { document.getElementById('addForm').addEventListener('submit', async (e) => {
e.preventDefault(); e.preventDefault();
@ -1592,14 +1612,14 @@ document.addEventListener('DOMContentLoaded', async () => {
}); });
// 增强的键盘快捷键和用户体验 // 增强的键盘快捷键和用户体验
document.getElementById('newKeyword')?.addEventListener('keypress', function(e) { document.getElementById('newKeyword')?.addEventListener('keypress', function (e) {
if (e.key === 'Enter') { if (e.key === 'Enter') {
e.preventDefault(); e.preventDefault();
document.getElementById('newReply').focus(); document.getElementById('newReply').focus();
} }
}); });
document.getElementById('newReply')?.addEventListener('keypress', function(e) { document.getElementById('newReply')?.addEventListener('keypress', function (e) {
if (e.key === 'Enter') { if (e.key === 'Enter') {
e.preventDefault(); e.preventDefault();
addKeyword(); addKeyword();
@ -1607,7 +1627,7 @@ document.addEventListener('DOMContentLoaded', async () => {
}); });
// ESC键取消编辑 // ESC键取消编辑
document.addEventListener('keydown', function(e) { document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && typeof window.editingIndex !== 'undefined') { if (e.key === 'Escape' && typeof window.editingIndex !== 'undefined') {
e.preventDefault(); e.preventDefault();
cancelEdit(); cancelEdit();
@ -1615,7 +1635,7 @@ document.addEventListener('DOMContentLoaded', async () => {
}); });
// 输入框实时验证和提示 // 输入框实时验证和提示
document.getElementById('newKeyword')?.addEventListener('input', function(e) { document.getElementById('newKeyword')?.addEventListener('input', function (e) {
const value = e.target.value.trim(); const value = e.target.value.trim();
const addBtn = document.querySelector('.add-btn'); const addBtn = document.querySelector('.add-btn');
const replyInput = document.getElementById('newReply'); const replyInput = document.getElementById('newReply');
@ -1633,7 +1653,7 @@ document.addEventListener('DOMContentLoaded', async () => {
} }
}); });
document.getElementById('newReply')?.addEventListener('input', function(e) { document.getElementById('newReply')?.addEventListener('input', function (e) {
const value = e.target.value.trim(); const value = e.target.value.trim();
const addBtn = document.querySelector('.add-btn'); const addBtn = document.querySelector('.add-btn');
const keywordInput = document.getElementById('newKeyword'); const keywordInput = document.getElementById('newKeyword');
@ -1655,7 +1675,7 @@ document.addEventListener('DOMContentLoaded', async () => {
loadDashboard(); loadDashboard();
// 点击侧边栏外部关闭移动端菜单 // 点击侧边栏外部关闭移动端菜单
document.addEventListener('click', function(e) { document.addEventListener('click', function (e) {
const sidebar = document.getElementById('sidebar'); const sidebar = document.getElementById('sidebar');
const toggle = document.querySelector('.mobile-toggle'); const toggle = document.querySelector('.mobile-toggle');
@ -1857,8 +1877,12 @@ async function saveDefaultReply() {
} }
} }
// 测试默认回复(占位函数) /**
* 测试默认回复占位函数
* @param {string} accountId - 账号ID
*/
function testDefaultReply(accountId) { function testDefaultReply(accountId) {
console.log('测试默认回复功能账号ID:', accountId);
showToast('测试功能开发中...', 'info'); showToast('测试功能开发中...', 'info');
} }