| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 'use strict';
- import { getDataDir } from 'ee-core/ps';
- import { logger } from 'ee-core/log';
- import path from 'node:path';
- import fs from 'node:fs';
- /**
- * 本机(客户端)设置:落盘在应用数据目录下的 local-settings.json。
- *
- * <p>与后端 {@code QingJian/conf/c5e503ad.json} 无关:这些是「这台机器上的客户端」的偏好,
- * 例如离线地图目录 —— 后端可能部署在别的机器上,服务端路径对客户端没有意义。
- */
- class LocalSettingsService {
- get filePath() {
- return path.join(getDataDir(), 'local-settings.json');
- }
- readAll() {
- try {
- const file = this.filePath;
- if (!fs.existsSync(file)) return {};
- const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
- return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
- } catch (e) {
- logger.warn('[localSettings] 读取失败:', e instanceof Error ? e.message : String(e));
- return {};
- }
- }
- /** 读一个字符串设置;没有或类型不对返回空串 */
- get(key) {
- const value = this.readAll()[key];
- return typeof value === 'string' ? value : '';
- }
- /** 写一个设置;传空则删除该键 */
- set(key, value) {
- const all = this.readAll();
- const text = typeof value === 'string' ? value.trim() : '';
- if (text) {
- all[key] = text;
- } else {
- delete all[key];
- }
- const file = this.filePath;
- fs.mkdirSync(path.dirname(file), { recursive: true });
- fs.writeFileSync(file, `${JSON.stringify(all, null, 2)}\n`, 'utf8');
- return text;
- }
- }
- export const localSettingsService = new LocalSettingsService();
|