feat: 增加配置导入导出功能
This commit is contained in:
+88
-8
@@ -1368,25 +1368,50 @@ function bindEvents() {
|
||||
}
|
||||
});
|
||||
|
||||
// 导入导出
|
||||
// 配置导入导出
|
||||
document.getElementById('btnExportConfig').addEventListener('click', () => {
|
||||
window.location.href = '/api/config/export';
|
||||
// 勾了「包含密钥」才把真实密码/加签密钥写进文件,默认脱敏,方便分享
|
||||
const withSecrets = document.getElementById('exportWithSecrets').checked;
|
||||
window.location.href = `/api/config/export${withSecrets ? '?secrets=1' : ''}`;
|
||||
toast(withSecrets ? '正在导出(含密钥,请妥善保管)' : '正在导出(密钥已脱敏)', 'ok');
|
||||
});
|
||||
|
||||
document.getElementById('btnImportConfig').addEventListener('click', () => document.getElementById('importFile').click());
|
||||
document.getElementById('importFile').addEventListener('change', async (e) => {
|
||||
const file = e.target.files?.[0];
|
||||
e.target.value = '';
|
||||
if (!file) return;
|
||||
try {
|
||||
const text = await file.text();
|
||||
const json = JSON.parse(text);
|
||||
const res = await api('/api/config/import', { method: 'POST', body: json });
|
||||
applyState(res, true);
|
||||
fillSettings();
|
||||
toast('配置已导入', 'ok');
|
||||
const data = json?.config && typeof json.config === 'object' ? json.config : json;
|
||||
if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error('文件内容不是配置对象');
|
||||
if (!Array.isArray(data.instruments) && !Array.isArray(data.rules)) {
|
||||
throw new Error('这不像是本工具导出的配置文件(缺少 instruments / rules)');
|
||||
}
|
||||
// 先把摘要摆出来,让用户确认后再真正覆盖
|
||||
ui.pendingImport = data;
|
||||
ui.pendingImportFile = file.name;
|
||||
document.getElementById('importSummary').innerHTML = buildImportSummary(data, file);
|
||||
setResult('importResult', '');
|
||||
openModal('modalImport');
|
||||
} catch (err) {
|
||||
toast(`导入失败:${err.message}`, 'err');
|
||||
} finally {
|
||||
e.target.value = '';
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('btnConfirmImport').addEventListener('click', async () => {
|
||||
if (!ui.pendingImport) return;
|
||||
try {
|
||||
const res = await api('/api/config/import', { method: 'POST', body: ui.pendingImport });
|
||||
applyState(res, true);
|
||||
fillSettings();
|
||||
closeModal('modalImport');
|
||||
ui.pendingImport = null;
|
||||
toast(`配置已导入${res.backup ? `,原配置已备份为 ${res.backup}` : ''}`, 'ok');
|
||||
refreshBackupList();
|
||||
} catch (err) {
|
||||
setResult('importResult', err.message, false);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1410,10 +1435,65 @@ function bindEvents() {
|
||||
|
||||
const TABS = ['market', 'rules', 'channels', 'alerts'];
|
||||
|
||||
/**
|
||||
* 生成导入前的配置摘要(覆盖前先看清楚要导入什么)
|
||||
* @param {object} data 待导入的配置对象
|
||||
* @param {File} file 用户选择的文件
|
||||
* @returns {string} 摘要 HTML
|
||||
*/
|
||||
function buildImportSummary(data, file) {
|
||||
const instruments = Array.isArray(data.instruments) ? data.instruments : [];
|
||||
const rules = Array.isArray(data.rules) ? data.rules : [];
|
||||
const typeNames = { threshold: '价格阈值', change: '涨跌幅', spread: '价差', schedule: '定时' };
|
||||
const ruleCounts = {};
|
||||
rules.forEach((r) => {
|
||||
const key = typeNames[r.type] || String(r.type || '未知');
|
||||
ruleCounts[key] = (ruleCounts[key] || 0) + 1;
|
||||
});
|
||||
const ruleText = rules.length
|
||||
? `${rules.length} 条(${Object.entries(ruleCounts).map(([k, v]) => `${k} ${v}`).join('、')})`
|
||||
: '0 条';
|
||||
const instText = instruments.length
|
||||
? `${instruments.length} 个:${instruments.slice(0, 4).map((i) => i.name || i.id).join('、')}${instruments.length > 4 ? ' 等' : ''}`
|
||||
: '0 个';
|
||||
const channelText = [
|
||||
data.desktop?.enabled ? '桌面通知' : null,
|
||||
data.email?.enabled ? '邮件' : null,
|
||||
data.webhook?.enabled ? `WebHook(${data.webhook?.format === 'dingtalk' ? '钉钉' : '内置 JSON'})` : null,
|
||||
].filter(Boolean).join(' + ') || '仅使用规则自带渠道';
|
||||
const sizeText = file?.size ? `${(file.size / 1024).toFixed(1)} KB` : '未知大小';
|
||||
|
||||
return [
|
||||
`<div>文件:<b>${esc(file?.name || '未命名')}</b>(${esc(sizeText)})</div>`,
|
||||
data.exportedAt ? `<div>导出时间:<b>${esc(data.exportedAt)}</b></div>` : '',
|
||||
`<div>监控品种:<b>${esc(instText)}</b></div>`,
|
||||
`<div>提醒规则:<b>${esc(ruleText)}</b></div>`,
|
||||
`<div>通知渠道:<b>${esc(channelText)}</b></div>`,
|
||||
`<div>监听地址:<b>${esc(`${data.app?.host || '127.0.0.1'}:${data.app?.port || 8787}`)}</b>(端口改动需重启程序才生效)</div>`,
|
||||
data.email?.pass ? '<div>注意:文件里带有邮箱密码,导入后会按文件中的值覆盖当前密码。</div>' : '',
|
||||
].filter(Boolean).join('');
|
||||
}
|
||||
|
||||
/** 读取并展示最近的配置备份 */
|
||||
async function refreshBackupList() {
|
||||
const el = document.getElementById('backupListHint');
|
||||
if (!el) return;
|
||||
try {
|
||||
const res = await api('/api/config/backups');
|
||||
const list = res.backups || [];
|
||||
el.textContent = list.length
|
||||
? `最近备份(最多保留 20 份):${list.map((b) => `${b.name}(${b.at})`).join(';')}`
|
||||
: '还没有备份:每次导入配置前会自动生成一份。';
|
||||
} catch (err) {
|
||||
el.textContent = `读取备份列表失败:${err.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
/** 启动 */
|
||||
async function boot() {
|
||||
bindEvents();
|
||||
await refreshState(true);
|
||||
refreshBackupList();
|
||||
const hashTab = (location.hash || '').replace('#', '');
|
||||
if (TABS.includes(hashTab)) activateTab(hashTab);
|
||||
window.addEventListener('hashchange', () => {
|
||||
|
||||
Reference in New Issue
Block a user