document.addEventListener('DOMContentLoaded', function () { // 获取DOM元素 - 计算器模块 const calculateBtn = document.getElementById('calculate-btn'); const solutionsContainer = document.getElementById('solutions'); const statsContainer = document.getElementById('stats'); // 获取DOM元素 - 一星数据模块 const flag1MinInput = document.getElementById('flag1-min'); const flag1SearchBtn = document.getElementById('flag1-search-btn'); const flag1Table = document.getElementById('flag1-table'); // 获取DOM元素 - 二星数据模块 const flag2MinInput = document.getElementById('flag2-min'); const flag2SearchBtn = document.getElementById('flag2-search-btn'); const flag2Table = document.getElementById('flag2-table'); // 获取DOM元素 - 历史记录模块 const historyTypeSelect = document.getElementById('history-type'); const historyStartDateInput = document.getElementById('history-start-date'); const historyEndDateInput = document.getElementById('history-end-date'); const historySearchBtn = document.getElementById('history-search-btn'); const historyContainer = document.getElementById('history-container'); const historyLoadMoreBtn = document.getElementById('history-load-more'); // 获取DOM元素 - 出题模块 const problemContent = document.getElementById('problem-content'); const problemAnswer = document.getElementById('problem-answer'); const showAnswerBtn = document.getElementById('show-answer-btn'); // 数字输入框 const num1Input = document.getElementById('num1'); const num2Input = document.getElementById('num2'); const num3Input = document.getElementById('num3'); const num4Input = document.getElementById('num4'); // 限制输入范围为1-13 [num1Input, num2Input, num3Input, num4Input].forEach(input => { input.addEventListener('input', function () { const value = parseInt(this.value); if (value < 1) this.value = 1; if (value > 13) this.value = 13; }); }); // 存储当前解法和问题数据 let currentSolutions = []; let currentProblem = null; // 获取问题类型选择框和出题按钮 const problemTypeSelect = document.getElementById('problem-type-select'); const generateProblemBtn = document.getElementById('generate-problem-btn'); // 为出题按钮添加点击事件 generateProblemBtn.addEventListener('click', function () { const selectedType = problemTypeSelect.value; if (selectedType) { // 根据选择的类型生成题目 generateProblem(selectedType); } else { // 如果没有选择类型,提示用户 problemContent.innerHTML = '
请先选择题目类型
'; } }); // 为显示答案按钮添加点击事件 showAnswerBtn.addEventListener('click', function () { if (currentProblem && currentProblem.answers) { console.log('currentProblem:', currentProblem.answers) if (problemAnswer.style.display === 'block') { problemAnswer.style.display = 'none'; showAnswerBtn.textContent = '显示答案'; } else { problemAnswer.style.display = 'block'; renderProblemAnswers(currentProblem.answers); showAnswerBtn.textContent = '隐藏答案'; } } }); // 为计算按钮添加点击事件 calculateBtn.addEventListener('click', fetchSolutions); // 为历史记录查询按钮添加点击事件 historySearchBtn.addEventListener('click', ()=>{fetchHistory(false)}); // 根据题型生成题目 let answerTimer = null; let timerInterval = null; let timerSeconds = 0; let timerDisplay; async function generateProblem(type) { if (answerTimer) clearTimeout(answerTimer); if (timerInterval) clearInterval(timerInterval); // 重置问题和答案区域 problemContent.innerHTML = '正在生成题目,请稍候...
'; problemAnswer.innerHTML = ''; problemAnswer.style.display = 'none'; showAnswerBtn.style.display = 'none'; currentProblem = null; // 重置计时器 timerSeconds = 0; try { // 调用API获取问题 const response = await fetch(`/api/questions?t=${type}`); const data = await response.json(); if (response.ok && data.question) { // 保存当前问题数据 currentProblem = data; // 显示问题内容 problemContent.innerHTML = `${data.detail || '获取题目失败'}
`; } } catch (error) { console.error('Error:', error); problemContent.innerHTML = '请求失败,请检查网络连接
'; } // 开始计时器 function startTimer() { // 清除之前的计时器 if (timerInterval) { clearInterval(timerInterval); } // 重置计时 timerSeconds = 0; updateTimerDisplay(); // 启动新的计时器 timerInterval = setInterval(() => { timerSeconds++; if (timerSeconds > 130) { clearInterval(timerInterval); } updateTimerDisplay(); }, 1000); } // 更新计时器显示 function updateTimerDisplay() { if (timerDisplay) { if (timerSeconds >= 120) { timerDisplay.textContent = "时间到"; timerDisplay.classList.add('time-up'); } else { const minutes = Math.floor(timerSeconds / 60); const seconds = timerSeconds % 60; timerDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; timerDisplay.classList.remove('time-up'); } } } } // 渲染问题答案 function renderProblemAnswers(answers) { if (!answers || answers.length === 0) { problemAnswer.innerHTML = '没有找到答案
'; return; } // 根据答案类型渲染不同的内容 if (Array.isArray(answers)) { // 如果是数组,渲染为列表 const answersHTML = answers.map(answer => { if (typeof answer === 'object' && answer.expression) { // 如果是解法对象 return `正在计算解法,请稍候...
'; statsContainer.textContent = ''; try { // 调用API const response = await fetch(`/api/solutions?num1=${num1}&num2=${num2}&num3=${num3}&num4=${num4}`); const data = await response.json(); if (response.ok) { // 保存解法 currentSolutions = data.solutions || []; // 显示统计信息 if (currentSolutions.length > 0) { statsContainer.textContent = `共 ${data.count} 种解法(一星: ${data.flag1_count}, 二星: ${data.flag2_count})`; } else { statsContainer.textContent = '没有找到任何解法'; } // 渲染解法 renderSolutions(); } else { // 显示错误信息 solutionsContainer.innerHTML = `${data.detail || '没有找到解法'}
`; statsContainer.textContent = ''; } } catch (error) { console.error('Error:', error); solutionsContainer.innerHTML = '请求失败,请检查网络连接
'; statsContainer.textContent = ''; } } // 渲染解法 function renderSolutions() { // 如果没有解法 if (!currentSolutions || currentSolutions.length === 0) { solutionsContainer.innerHTML = '没有找到匹配的解法,请尝试其他数字组合
'; return; } // 直接使用所有解法 const filteredSolutions = currentSolutions; // 渲染解法列表 const solutionsHTML = filteredSolutions.map(solution => { return `请输入4个数字并点击"计算解法"按钮
'; } // 切换到一星数据模块时加载数据 if (this.value === '3' && flag1Table.querySelector('tbody tr td.placeholder')) { fetchFlagData(1, flag1MinInput.value, flag1Table); } // 切换到二星数据模块时加载数据 if (this.value === '4' && flag2Table.querySelector('tbody tr td.placeholder')) { fetchFlagData(2, flag2MinInput.value, flag2Table); } // 切换到历史记录模块时加载数据 if (this.value === '5' && historyContainer) { fetchHistory(false); } }); // 初始化默认显示模块 document.querySelectorAll('.module-container').forEach(module => { module.style.display = module.classList.contains('module-1') ? 'block' : 'none'; }); // 历史记录相关变量 let currentHistoryPage = 1; let isLoadingHistory = false; let hasMoreHistory = true; // 获取历史记录数据 async function fetchHistory(loadMore = false) { if (isLoadingHistory) return; isLoadingHistory = true; // 如果不是加载更多,则重置状态 if (!loadMore) { currentHistoryPage = 1; hasMoreHistory = true; historyContainer.innerHTML = '没有找到答案
'; return; } // 根据答案类型渲染不同的内容 if (Array.isArray(answers)) { // 如果是数组,渲染为列表 const answersHTML = answers.map(answer => { if (typeof answer === 'object' && answer.expression) { // 如果是解法对象 return `