/** * 应用设置(settings.json,与 config.json 同目录) * 目前包含 OCR 识别方式配置: * ocr.provider: 'local' | 'online' * ocr.online: { url, apiKey, model } 线上 OCR(OpenAI 兼容 /v1/chat/completions) */ const fs = require('fs'); const path = require('path'); const { getConfigDir } = require('./config'); const DEFAULTS = { ocr: { provider: 'local', // local = 本地 tesseract;online = 线上 OCR 接口 online: { url: 'https://ai.88531.cn/v1', // OpenAI 兼容接口(含 /v1),默认 ai.88531.cn apiKey: '', model: 'mimo-v2.6-flash' // 默认视觉模型 } }, telemetry: true, // CardAuth 心跳/版本/公告上报开关 // AI 生图(内置 NewAPI:Pollinations + CF Workers AI;或自定义 OpenAI 兼容提供商) imagegen: { provider: 'newapi', // 'newapi' | 'custom' newapi: { url: 'http://192.168.68.19:3099/v1', apiKey: '' // 留空则用服务器环境变量 NEWAPI_KEY }, custom: { url: '', apiKey: '', model: '' } } }; function settingsPath() { return path.join(getConfigDir(), 'settings.json'); } function getSettings() { try { const p = settingsPath(); if (!fs.existsSync(p)) return JSON.parse(JSON.stringify(DEFAULTS)); const data = JSON.parse(fs.readFileSync(p, 'utf8')); // 深合并默认值,防旧文件缺字段 return { ocr: { provider: (data.ocr && data.ocr.provider) === 'online' ? 'online' : 'local', online: { url: (data.ocr && data.ocr.online && data.ocr.online.url) || DEFAULTS.ocr.online.url, apiKey: (data.ocr && data.ocr.online && data.ocr.online.apiKey) || '', model: (data.ocr && data.ocr.online && data.ocr.online.model) || DEFAULTS.ocr.online.model } }, telemetry: data.telemetry !== false, imagegen: { provider: (data.imagegen && data.imagegen.provider) === 'custom' ? 'custom' : 'newapi', newapi: { url: (data.imagegen && data.imagegen.newapi && data.imagegen.newapi.url) || DEFAULTS.imagegen.newapi.url, apiKey: (data.imagegen && data.imagegen.newapi && data.imagegen.newapi.apiKey) || '' }, custom: { url: (data.imagegen && data.imagegen.custom && data.imagegen.custom.url) || '', apiKey: (data.imagegen && data.imagegen.custom && data.imagegen.custom.apiKey) || '', model: (data.imagegen && data.imagegen.custom && data.imagegen.custom.model) || '' } } }; } catch (e) { console.error('[Settings] 读取失败,使用默认:', e.message); return JSON.parse(JSON.stringify(DEFAULTS)); } } function saveSettings(settings) { const p = settingsPath(); const merged = getSettings(); if (settings.ocr) { merged.ocr.provider = settings.ocr.provider === 'online' ? 'online' : 'local'; if (settings.ocr.online) { // apiKey 传空/掩码 = 保持原值 const incomingKey = settings.ocr.online.apiKey; if (typeof incomingKey === 'string' && incomingKey.trim() && !incomingKey.includes('***')) { merged.ocr.online.apiKey = incomingKey.trim(); } if (typeof settings.ocr.online.url === 'string') merged.ocr.online.url = settings.ocr.online.url.trim(); if (typeof settings.ocr.online.model === 'string') merged.ocr.online.model = settings.ocr.online.model.trim(); } } if (typeof settings.telemetry === 'boolean') merged.telemetry = settings.telemetry; if (settings.imagegen) { const ig = settings.imagegen; if (ig.provider === 'custom' || ig.provider === 'newapi') merged.imagegen.provider = ig.provider; if (ig.newapi) { if (typeof ig.newapi.url === 'string') merged.imagegen.newapi.url = ig.newapi.url.trim(); const k = ig.newapi.apiKey; // 空串/掩码 = 保持原值(与 OCR 行为一致) if (typeof k === 'string' && k.trim() && !k.includes('***')) { merged.imagegen.newapi.apiKey = k.trim(); } } if (ig.custom) { if (typeof ig.custom.url === 'string') merged.imagegen.custom.url = ig.custom.url.trim(); if (typeof ig.custom.model === 'string') merged.imagegen.custom.model = ig.custom.model.trim(); const ck = ig.custom.apiKey; if (typeof ck === 'string' && ck.trim() && !ck.includes('***')) { merged.imagegen.custom.apiKey = ck.trim(); } } } fs.writeFileSync(p, JSON.stringify(merged, null, 2)); return merged; } /** 掩码展示用:sk-abcdef123456 -> sk-a***56 */ function maskKey(key) { if (!key) return ''; if (key.length <= 8) return '***'; return `${key.slice(0, 4)}***${key.slice(-4)}`; } module.exports = { getSettings, saveSettings, maskKey, settingsPath };