diff --git a/README.md b/README.md index 3e5ecff..6ecb6e8 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,9 @@ npm start | GET | `/api/state` | 全部行情 + 配置 + 提醒 | | GET | `/api/health` | 存活检查 | | PUT | `/api/config` | 局部更新配置(对象深合并) | -| GET/POST | `/api/config/export` `/api/config/import` | 备份 / 恢复配置 | +| GET | `/api/config/export?secrets=1` | 导出配置 JSON(不带 `secrets=1` 时邮箱密码 / WebHook 加签自动脱敏) | +| POST | `/api/config/import` | 导入配置(覆盖式,导入前自动备份到 `config/backups/`,返回备份文件名) | +| GET | `/api/config/backups` | 查看最近的配置备份(最多保留 20 份) | | POST | `/api/instruments` | 新增品种(`presetKey` 或自定义字段) | | PUT/DELETE | `/api/instruments/:id` | 修改 / 删除品种 | | POST | `/api/instruments/:id/manual` | 手动推价 `{ "price": 781.2, "prevClose": 780 }` | @@ -228,7 +230,19 @@ curl -X POST http://127.0.0.1:8787/api/instruments/ins_czbjcj/manual \ ``` 改端口/监听地址需要重启进程,其它配置网页保存后即时生效。 -`config.json` 里含邮箱授权码,请勿外传;页面导出配置同样含真实密码。 + +**配置导入导出**(「通知设置 → 配置备份」卡片): + +- 导出:生成 `gold-price-watcher-config-20261124-165146.json`,包含品种、规则、渠道、端口等全部配置; + 默认把邮箱密码 / WebHook 加签**脱敏为空**,方便分享;勾选「导出时包含密钥」才会写入真实密钥(此时文件请勿外传)。 +- 导入:选 JSON 文件后先弹「确认导入配置」,摘要里列出文件大小、导出时间、品种数、规则数(按类型分解)、通知渠道、监听地址; + 点「确认导入」才会真正覆盖。 +- 覆盖前程序自动把当前配置备份到 `config/backups/config-<时间戳>.json`(最多保留 20 份), + 页面上会显示最近备份清单,导入效果不对可以直接拿备份文件再导入回去。 +- 导入后品种会按新配置重新编排轮询;端口/监听地址改动仍需重启进程才生效。 +- 不是本工具导出的 JSON(缺少 `instruments` / `rules`)会被拒绝,不会覆盖配置。 + +`config.json` 里含邮箱授权码,请勿外传;导出时默认已脱敏,只有勾选「包含密钥」才会写出真实密码。 --- diff --git a/public/app.js b/public/app.js index 8d62837..7593c8d 100644 --- a/public/app.js +++ b/public/app.js @@ -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 [ + `
导出成 JSON 文件,可用于换机迁移、分享给同事,或改坏配置后回滚;导入会覆盖当前全部配置(品种、规则、渠道、端口等),导入前程序会自动把现有配置备份到 config/backups/。
导入会覆盖当前全部配置(品种、规则、通知渠道、端口等);导入前程序会自动把现有配置备份到 config/backups/,不满意可以用备份回滚。