From d7751144bf14e7354901447437f6a206332d5a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Wed, 10 Apr 2024 16:32:22 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=96=B0=E5=A2=9E=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E5=87=BA=E5=8E=82=E8=AE=BE=E7=BD=AE=E5=8A=9F=E8=83=BD=E3=80=82?= =?UTF-8?q?=20(#291)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/config.js | 35 ++++++++++++++++++--- packages/gui/src/view/mixins/plugin.js | 22 ++++++++++--- packages/gui/src/view/pages/setting.vue | 41 ++++++++++++++++--------- 3 files changed, 75 insertions(+), 23 deletions(-) diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 4dbec30..19d61f1 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -161,7 +161,7 @@ const configApi = { const diffConfig = mergeApi.doDiff(defConfig, newConfig) const configPath = _getConfigPath() fs.writeFileSync(configPath, jsonApi.stringify(diffConfig)) - log.info('保存自定义配置文件成功:', configPath) + log.info('保存 config.json 自定义配置文件成功:', configPath) // 重载配置 const allConfig = configApi.set(diffConfig) @@ -203,15 +203,19 @@ const configApi = { log.warn('newConfig 为空,不做任何操作') return configTarget } - + return configApi.load(newConfig) + }, + load (newConfig) { const merged = lodash.cloneDeep(defConfig) const remoteConfig = configApi.readRemoteConfig() mergeApi.doMerge(merged, remoteConfig) - mergeApi.doMerge(merged, newConfig) + if (newConfig != null) { + mergeApi.doMerge(merged, newConfig) + } mergeApi.deleteNullItems(merged) configTarget = merged - log.info('加载配置完成') + log.info('加载及合并远程配置完成') return configTarget }, @@ -221,6 +225,29 @@ const configApi = { addDefault (key, defValue) { lodash.set(defConfig, key, defValue) }, + async removeUserConfig () { + const configPath = _getConfigPath() + if (fs.existsSync(configPath)) { + // 读取 config.json 文件内容 + const fileStr = fs.readFileSync(configPath).toString().replace(/\s/g, '') + + // 判断文件内容是否为空或空配置 + if (fileStr === '' || fileStr === '{}') { + fs.rmSync(configPath) + return false // config.json 内容为空,或为空json + } + + // 备份用户自定义配置文件 + fs.renameSync(configPath, configPath + '.bak' + new Date().getTime() + '.json') + + // 重新加载配置 + configApi.load(null) + + return true // 删除并重新加载配置成功 + } else { + return false // config.json 文件不存在或内容为配置 + } + }, resetDefault (key) { if (key) { let value = lodash.get(defConfig, key) diff --git a/packages/gui/src/view/mixins/plugin.js b/packages/gui/src/view/mixins/plugin.js index f3a65a4..ead08c9 100644 --- a/packages/gui/src/view/mixins/plugin.js +++ b/packages/gui/src/view/mixins/plugin.js @@ -31,13 +31,10 @@ export default { }, async init () { this.status = this.$status - - const config = await this.$api.config.reload() - this.setConfig(config) + await this.reloadConfig() + this.printConfig('Init, ') this.systemPlatform = await this.$api.info.getSystemPlatform() - this.printConfig() - if (this.ready) { return this.ready(this.config) } @@ -105,6 +102,21 @@ export default { } return value }, + async reloadConfig () { + const config = await this.$api.config.reload() + this.setConfig(config) + }, + async reloadConfigAndRestart () { + await this.reloadConfig() + this.printConfig('After reloadConfigAndRestart(), ') + if (this.status.server.enabled || this.status.proxy.enabled) { + await this.$api.proxy.restart() + await this.$api.server.restart() + this.$message.success('代理服务和系统代理重启成功') + } else { + this.$message.info('代理服务和系统代理未启动,无需重启') + } + }, isWindows () { return this.systemPlatform === 'windows' }, diff --git a/packages/gui/src/view/pages/setting.vue b/packages/gui/src/view/pages/setting.vue index 903842d..26fa41e 100644 --- a/packages/gui/src/view/pages/setting.vue +++ b/packages/gui/src/view/pages/setting.vue @@ -76,6 +76,7 @@