feat: 增加json数据导出功能

master
Lxy 2 weeks ago
parent 705bd802ca
commit ce7b5fc133

@ -914,6 +914,12 @@
查询缓存 查询缓存
</button> </button>
</div> </div>
<div class="form-group">
<button class="btn btn-success" onclick="exportData()" style="height: 42px;" id="btnExportData" disabled>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>
导出数据
</button>
</div>
</div> </div>
<div id="queryResult" class="hidden" style="margin-top: 20px;"></div> <div id="queryResult" class="hidden" style="margin-top: 20px;"></div>
</div> </div>
@ -1805,12 +1811,14 @@
if (!res.ok) { if (!res.ok) {
showToast(data.detail || '未找到缓存数据', 'error'); showToast(data.detail || '未找到缓存数据', 'error');
document.getElementById('btnExportData').disabled = true;
return; return;
} }
addLog(`查询成功: ${symbol}, 缓存 ${data.timeframes ? data.timeframes.length : 0} 个周期`, 'success'); addLog(`查询成功: ${symbol}, 缓存 ${data.timeframes ? data.timeframes.length : 0} 个周期`, 'success');
currentQueryData = data; currentQueryData = data;
document.getElementById('btnExportData').disabled = false;
if (!data.timeframes || data.timeframes.length === 0) { if (!data.timeframes || data.timeframes.length === 0) {
document.getElementById('queryResult').innerHTML = '<div class="empty-state"><p>暂无K线数据</p></div>'; document.getElementById('queryResult').innerHTML = '<div class="empty-state"><p>暂无K线数据</p></div>';
@ -1822,7 +1830,49 @@
} catch (e) { } catch (e) {
showToast(`查询失败: ${e.message}`, 'error'); showToast(`查询失败: ${e.message}`, 'error');
document.getElementById('btnExportData').disabled = true;
}
}
function exportData() {
if (!currentQueryData || !currentQueryData.timeframes || currentQueryData.timeframes.length === 0) {
showToast('暂无可导出的数据', 'error');
return;
} }
const symbol = document.getElementById('querySymbol').value.trim() || 'unknown';
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
const filename = `${symbol}_多周期数据_${timestamp}.json`;
const exportObj = {
symbol: currentQueryData.symbol || symbol,
type: currentQueryData.type || 'futures',
current_price: currentQueryData.current_price,
timestamp: currentQueryData.timestamp || new Date().toISOString(),
timeframes: {}
};
currentQueryData.timeframes.forEach(tf => {
exportObj.timeframes[tf.period] = tf.candles || [];
});
const jsonStr = JSON.stringify(exportObj, null, 2);
const blob = new Blob([jsonStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
const periodCount = currentQueryData.timeframes.length;
const totalCandles = currentQueryData.timeframes.reduce((sum, tf) => sum + (tf.candles ? tf.candles.length : 0), 0);
addLog(`数据导出成功: ${filename}, ${periodCount} 个周期, ${totalCandles} 条K线`, 'success');
showToast(`已导出 ${periodCount} 个周期数据`, 'success');
} }
function renderKlineChart(timeframe, symbol) { function renderKlineChart(timeframe, symbol) {

Loading…
Cancel
Save