|
|
@@ -0,0 +1,67 @@
|
|
|
+// electron/utils/readConfig.js
|
|
|
+
|
|
|
+const fs = require('fs');
|
|
|
+const path = require('path');
|
|
|
+const { app } = require('electron');
|
|
|
+
|
|
|
+const package = require("../../package.json");
|
|
|
+const configPath = path.join(app.getPath("appData"), 'ZhiHuiYin', 'config.default.json');
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 读取配置文件
|
|
|
+ */
|
|
|
+function readConfigFile() {
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (fs.existsSync(configPath)) {
|
|
|
+ const data = fs.readFileSync(configPath, 'utf8');
|
|
|
+ return JSON.parse(data);
|
|
|
+ } else {
|
|
|
+ console.log('配置文件不存在');
|
|
|
+ // 创建空白JSON文件
|
|
|
+ fs.writeFileSync(configPath, '{}', 'utf8');
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('读取配置文件出错:', error);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 写入配置文件
|
|
|
+ * @param {string} key - 要写入的配置项键名
|
|
|
+ * @param {*} value - 要写入的配置项值
|
|
|
+ */
|
|
|
+function writeConfigFile(key, value) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ let config = {};
|
|
|
+
|
|
|
+ // 如果配置文件存在,则读取现有内容
|
|
|
+ if (fs.existsSync(configPath)) {
|
|
|
+ const data = fs.readFileSync(configPath, 'utf8');
|
|
|
+ config = JSON.parse(data);
|
|
|
+ }else {
|
|
|
+ console.log('配置文件不存在');
|
|
|
+ // 创建空白JSON文件
|
|
|
+ fs.writeFileSync(configPath, '{}', 'utf8');
|
|
|
+ config = {}
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新配置项
|
|
|
+ config[key] = value;
|
|
|
+
|
|
|
+ // 将更新后的配置写回文件
|
|
|
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
|
|
|
+ console.log(`成功写入配置项: ${key}`);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('写入配置文件出错:', error);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = {
|
|
|
+ readConfigFile,
|
|
|
+ writeConfigFile
|
|
|
+};
|