| |
| |
| |
| |
|
|
| |
| let currentSection = 'my-tasks'; |
|
|
| |
| document.addEventListener('DOMContentLoaded', () => { |
| checkAuth(); |
| checkUserRole(); |
| initNavigation(); |
| initModals(); |
| initEventListeners(); |
| |
| loadUserMyTasks(); |
| }); |
|
|
| |
|
|
| function checkAuth() { |
| if (!isLoggedIn()) { |
| window.location.href = '/auth'; |
| } |
| } |
|
|
| |
|
|
| async function checkUserRole() { |
| try { |
| const currentUser = await apiGet('/users/me'); |
| if (currentUser) { |
| const isAdmin = currentUser.is_superuser; |
|
|
| |
| const goToManagerBtn = document.getElementById('goToManagerBtn'); |
| const navDivider = document.getElementById('navDivider'); |
|
|
| if (isAdmin) { |
| if (goToManagerBtn) goToManagerBtn.classList.remove('hidden'); |
| if (navDivider) navDivider.classList.remove('hidden'); |
| } |
| } |
| } catch (error) { |
| console.error('检查用户角色失败:', error); |
| } |
| } |
|
|
| |
|
|
| function initNavigation() { |
| const navItems = document.querySelectorAll('.nav-item'); |
| navItems.forEach(item => { |
| item.addEventListener('click', () => { |
| |
| if (item.id === 'goToManagerBtn') { |
| window.location.href = '/manager'; |
| return; |
| } |
| const section = item.dataset.section; |
| if (section) { |
| switchSection(section); |
| } |
| }); |
| }); |
|
|
| |
| const logoutBtn = document.getElementById('logoutBtn'); |
| if (logoutBtn) { |
| logoutBtn.addEventListener('click', () => { |
| clearToken(); |
| window.location.href = '/auth'; |
| }); |
| } |
| } |
|
|
| function switchSection(section) { |
| |
| document.querySelectorAll('.nav-item[data-section]').forEach(item => { |
| item.classList.toggle('active', item.dataset.section === section); |
| }); |
|
|
| |
| document.querySelectorAll('.content-section').forEach(sec => { |
| sec.classList.toggle('active', sec.id === `${section}-section`); |
| }); |
|
|
| currentSection = section; |
|
|
| |
| switch(section) { |
| case 'seed-questions': |
| loadUserSeedQuestion(); |
| break; |
| case 'available-tasks': |
| loadUserAvailableTasks(); |
| break; |
| case 'my-tasks': |
| loadUserMyTasks(); |
| break; |
| } |
| } |
|
|
| |
|
|
| function initModals() { |
| const modal = document.getElementById('modal'); |
| const closeBtn = document.getElementById('modalClose'); |
| const cancelBtn = document.getElementById('modalCancel'); |
|
|
| if (closeBtn) { |
| closeBtn.addEventListener('click', closeModal); |
| } |
| if (cancelBtn) { |
| cancelBtn.addEventListener('click', closeModal); |
| } |
| } |
|
|
| function openModal(title, content, onSubmit, hideSubmit = false) { |
| const modal = document.getElementById('modal'); |
| const modalTitle = document.getElementById('modalTitle'); |
| const modalBody = document.getElementById('modalBody'); |
| const submitBtn = document.getElementById('modalSubmit'); |
| const cancelBtn = document.getElementById('modalCancel'); |
|
|
| if (modalTitle) modalTitle.textContent = title; |
| if (modalBody) modalBody.innerHTML = content; |
| if (submitBtn) { |
| submitBtn.style.display = hideSubmit ? 'none' : 'inline-flex'; |
| submitBtn.onclick = onSubmit || null; |
| } |
| if (cancelBtn) { |
| cancelBtn.onclick = closeModal; |
| } |
| if (modal) modal.classList.add('active'); |
| } |
|
|
| function closeModal() { |
| const modal = document.getElementById('modal'); |
| if (modal) modal.classList.remove('active'); |
| } |
|
|
| |
|
|
| function initEventListeners() { |
| |
| } |
|
|
| |
|
|
| async function loadUserSeedQuestion() { |
| const container = document.getElementById('user-seed-question-container'); |
| if (!container) return; |
|
|
| |
| if (container.innerHTML.trim() !== '') { |
| return; |
| } |
|
|
| try { |
| |
| const htmlResponse = await fetch('/user-seed-question.html'); |
| if (!htmlResponse.ok) { |
| throw new Error('加载种子问题HTML失败'); |
| } |
| container.innerHTML = await htmlResponse.text(); |
|
|
| |
| if (window.updateTemplateTranslations) { |
| window.updateTemplateTranslations(container); |
| } |
|
|
| |
| await ensureStylesheetLoaded('/static/css/user-seed-question.css'); |
| |
| await ensureStylesheetLoaded('/static/css/seed-question.css'); |
|
|
| |
| await ensureScriptLoaded('/static/js/user-seed-question.js'); |
|
|
| |
| if (window.UserSeedQuestion) { |
| window.UserSeedQuestion.init(container); |
| } |
| } catch (error) { |
| console.error('加载种子问题模块失败:', error); |
| showError('加载种子问题模块失败: ' + (error.message || '未知错误')); |
| } |
| } |
|
|
| |
|
|
| async function loadUserAvailableTasks() { |
| const container = document.getElementById('user-available-tasks-container'); |
| if (!container) return; |
|
|
| |
| if (container.innerHTML.trim() !== '') { |
| return; |
| } |
|
|
| try { |
| |
| const htmlResponse = await fetch('/user-available-tasks.html'); |
| if (!htmlResponse.ok) { |
| throw new Error('加载可领取任务HTML失败'); |
| } |
| container.innerHTML = await htmlResponse.text(); |
|
|
| |
| if (window.updateTemplateTranslations) { |
| window.updateTemplateTranslations(container); |
| } |
|
|
| |
| await ensureStylesheetLoaded('/static/css/user-task.css'); |
|
|
| |
| await ensureScriptLoaded('/static/js/user-available-tasks.js'); |
|
|
| |
| if (window.UserAvailableTasks) { |
| window.UserAvailableTasks.init(container); |
| } |
| } catch (error) { |
| console.error('加载可领取任务模块失败:', error); |
| showError('加载可领取任务模块失败: ' + (error.message || '未知错误')); |
| } |
| } |
|
|
| |
|
|
| async function loadUserMyTasks() { |
| const container = document.getElementById('user-my-tasks-container'); |
| if (!container) return; |
|
|
| |
| if (container.innerHTML.trim() !== '') { |
| return; |
| } |
|
|
| try { |
| |
| const htmlResponse = await fetch('/user-my-tasks.html'); |
| if (!htmlResponse.ok) { |
| throw new Error('加载我的任务HTML失败'); |
| } |
| container.innerHTML = await htmlResponse.text(); |
|
|
| |
| if (window.updateTemplateTranslations) { |
| window.updateTemplateTranslations(container); |
| } |
|
|
| |
| await ensureStylesheetLoaded('/static/css/user-task.css'); |
|
|
| |
| await ensureScriptLoaded('/static/js/user-my-tasks.js'); |
|
|
| |
| if (window.UserMyTasks) { |
| window.UserMyTasks.init(container); |
| } |
| } catch (error) { |
| console.error('加载我的任务模块失败:', error); |
| showError('加载我的任务模块失败: ' + (error.message || '未知错误')); |
| } |
| } |
|
|
| |
|
|
| |
| async function ensureStylesheetLoaded(href) { |
| const linkId = href.replace(/[^a-zA-Z0-9]/g, '_'); |
| if (document.getElementById(linkId)) { |
| return; |
| } |
|
|
| const link = document.createElement('link'); |
| link.id = linkId; |
| link.rel = 'stylesheet'; |
| link.href = href; |
| document.head.appendChild(link); |
|
|
| return new Promise((resolve) => { |
| link.onload = resolve; |
| link.onerror = resolve; |
| }); |
| } |
|
|
| |
| async function ensureScriptLoaded(src) { |
| const scriptId = src.replace(/[^a-zA-Z0-9]/g, '_'); |
| if (document.getElementById(scriptId)) { |
| return Promise.resolve(); |
| } |
|
|
| return new Promise((resolve, reject) => { |
| const script = document.createElement('script'); |
| script.id = scriptId; |
| script.src = src; |
| script.onload = resolve; |
| script.onerror = reject; |
| document.head.appendChild(script); |
| }); |
| } |
|
|
| |
|
|
| function showSuccess(message) { |
| |
| alert(message); |
| } |
|
|
| function showError(message) { |
| |
| alert('错误: ' + message); |
| } |
|
|
| function escapeHtml(text) { |
| if (!text) return ''; |
| const div = document.createElement('div'); |
| div.textContent = text; |
| return div.innerHTML; |
| } |
|
|