// ==================== 纯请求函数 ==================== /** * 请求 K 线数据 * @param {string} endpoint API 端点 URL * @param {number} timeoutMs 超时毫秒数,默认 45000 * @returns {Promise} 解析后的 JSON 对象 * @throws {Error} 网络错误、HTTP 错误、超时错误、解析错误 */ async function requestKlines(endpoint, timeoutMs = 45000) { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeoutMs); try { const response = await fetch(endpoint, { method: 'GET', headers: { 'Accept': 'application/json' }, signal: controller.signal }); clearTimeout(timeoutId); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const json = await response.json(); if (!json || Object.keys(json).length === 0) { throw new Error('后端返回空对象'); } return json; } catch (err) { clearTimeout(timeoutId); if (err.name === 'AbortError') { throw new Error('请求超时'); } throw err; } }