// API接口列表 const API_ENDPOINTS = [ "https://60s.api.shumengya.top", ]; // 当前使用的API索引 let currentApiIndex = 0; // DOM元素 const loadingElement = document.getElementById('loading'); const lunarInfoElement = document.getElementById('lunarInfo'); const errorMessageElement = document.getElementById('errorMessage'); const updateTimeElement = document.getElementById('updateTime'); const dateInput = document.getElementById('dateInput'); const queryBtn = document.getElementById('queryBtn'); // 页面加载完成后初始化 document.addEventListener('DOMContentLoaded', function() { initializePage(); }); // 初始化页面 function initializePage() { // 设置默认日期为今天 const today = new Date(); const dateString = today.toISOString().split('T')[0]; dateInput.value = dateString; // 绑定事件 queryBtn.addEventListener('click', queryLunarInfo); dateInput.addEventListener('change', queryLunarInfo); // 自动查询当天信息 queryLunarInfo(); } // 查询农历信息 async function queryLunarInfo() { const selectedDate = dateInput.value; if (!selectedDate) { showError('请选择查询日期'); return; } showLoading(); hideError(); hideLunarInfo(); try { const data = await fetchLunarData(selectedDate); displayLunarInfo(data.data); updateQueryTime(); } catch (error) { console.error('查询失败:', error); showError('查询农历信息失败,请稍后重试'); } hideLoading(); } // 获取农历数据 async function fetchLunarData(date) { for (let i = 0; i < API_ENDPOINTS.length; i++) { const apiUrl = API_ENDPOINTS[currentApiIndex]; try { const response = await fetch(`${apiUrl}/v2/lunar?date=${date}`, { method: 'GET', headers: { 'Accept': 'application/json', }, timeout: 10000 }); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const data = await response.json(); if (data.code === 200 && data.data) { return data; } else { throw new Error('数据格式错误'); } } catch (error) { console.error(`API ${apiUrl} 请求失败:`, error); currentApiIndex = (currentApiIndex + 1) % API_ENDPOINTS.length; if (i === API_ENDPOINTS.length - 1) { throw new Error('所有API接口都无法访问'); } } } } // 显示农历信息 function displayLunarInfo(lunarData) { lunarInfoElement.innerHTML = `