From 8bf5d42b9a235e7dade543130d0e7601caa67e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Thu, 22 Aug 2024 13:59:44 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=96=B0=E5=A2=9E=20`=E4=B8=AA?= =?UTF-8?q?=E4=BA=BA=E8=BF=9C=E7=A8=8B=E9=85=8D=E7=BD=AE`=20=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E3=80=82=20(#334)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/config.js | 80 +++++++++++++++++-------- packages/core/src/config/index.js | 5 +- packages/gui/src/view/pages/setting.vue | 38 +++++++++--- 3 files changed, 88 insertions(+), 35 deletions(-) diff --git a/packages/core/src/config.js b/packages/core/src/config.js index ff0f35d5..c164777d 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -18,12 +18,12 @@ const getDefaultConfigBasePath = function () { return get().server.setting.userBasePath } -function _getRemoteSavePath (prefix = '') { +function _getRemoteSavePath (suffix = '') { const dir = getDefaultConfigBasePath() if (!fs.existsSync(dir)) { fs.mkdirSync(dir) } - return path.join(dir, prefix + 'remote_config.json5') + return path.join(dir, `remote_config${suffix}.json5`) } function _getConfigPath () { @@ -51,11 +51,27 @@ const configApi = { await download() timer = setInterval(download, 24 * 60 * 60 * 1000) // 1天 }, - downloadRemoteConfig () { + async downloadRemoteConfig () { + if (get().app.remoteConfig.enabled !== true) { + // 删除保存的远程配置文件 + configApi.deleteRemoteConfigFile() + configApi.deleteRemoteConfigFile('_personal') + return + } + + const remoteConfig = get().app.remoteConfig + await configApi.doDownloadCommonRemoteConfig(remoteConfig.url) + await configApi.doDownloadCommonRemoteConfig(remoteConfig.personalUrl, '_personal') + }, + doDownloadCommonRemoteConfig (remoteConfigUrl, suffix = '') { if (get().app.remoteConfig.enabled !== true) { return } - const remoteConfigUrl = get().app.remoteConfig.url + if (!remoteConfigUrl) { + // 删除保存的远程配置文件 + configApi.deleteRemoteConfigFile(suffix) + return + } // eslint-disable-next-line handle-callback-err return new Promise((resolve, reject) => { log.info('开始下载远程配置:', remoteConfigUrl) @@ -89,7 +105,7 @@ const configApi = { } if (remoteConfig != null) { - const remoteSavePath = _getRemoteSavePath() + const remoteSavePath = _getRemoteSavePath(suffix) fs.writeFileSync(remoteSavePath, body) log.info('保存远程配置文件成功:', remoteSavePath) } else { @@ -111,31 +127,31 @@ const configApi = { }) }) }, - readRemoteConfig () { - if (get().app.remoteConfig.enabled !== true) { - return {} + deleteRemoteConfigFile (suffix = '') { + const remoteSavePath = _getRemoteSavePath(suffix) + if (fs.existsSync(remoteSavePath)) { + fs.unlinkSync(remoteSavePath) + log.info('删除远程配置文件成功:', remoteSavePath) } - const path = _getRemoteSavePath() - try { - if (fs.existsSync(path)) { - const file = fs.readFileSync(path) - log.info('读取远程配置文件成功:', path) - return jsonApi.parse(file.toString()) - } else { - log.warn('远程配置文件不存在:', path) - } - } catch (e) { - log.error('读取远程配置文件失败:', path, ', error:', e) - } - - return {} }, - readRemoteConfigStr () { + readRemoteConfig (suffix = '') { + return jsonApi.parse(configApi.readRemoteConfigStr(suffix)) + }, + readRemoteConfigStr (suffix = '') { if (get().app.remoteConfig.enabled !== true) { + if (suffix === '_personal') { + if (!get().app.remoteConfig.personalUrl) { + return '{}' + } + } else if (suffix === '') { + if (!get().app.remoteConfig.url) { + return '{}' + } + } return '{}' } try { - const path = _getRemoteSavePath() + const path = _getRemoteSavePath(suffix) if (fs.existsSync(path)) { const file = fs.readFileSync(path) log.info('读取远程配置文件内容成功:', path) @@ -159,7 +175,12 @@ const configApi = { // 如果开启了远程配置,则读取远程配置,合并到默认配置中 if (get().app.remoteConfig.enabled === true) { - defConfig = mergeApi.doMerge(defConfig, configApi.readRemoteConfig()) + if (get().app.remoteConfig.url) { + defConfig = mergeApi.doMerge(defConfig, configApi.readRemoteConfig()) + } + if (get().app.remoteConfig.personalUrl) { + defConfig = mergeApi.doMerge(defConfig, configApi.readRemoteConfig('_personal')) + } } // 计算新配置与默认配置(启用远程配置时,含远程配置)的差异,并保存到 config.json 中 @@ -215,7 +236,14 @@ const configApi = { const merged = newConfig != null ? lodash.cloneDeep(newConfig) : {} mergeApi.doMerge(merged, defConfig) // 合并默认配置 - mergeApi.doMerge(merged, configApi.readRemoteConfig()) // 合并远程配置 + if (get().app.remoteConfig.enabled === true) { + if (get().app.remoteConfig.url) { + mergeApi.doMerge(merged, configApi.readRemoteConfig()) // 合并共享远程配置 + } + if (get().app.remoteConfig.personalUrl) { + mergeApi.doMerge(merged, configApi.readRemoteConfig('_personal')) // 合并个人远程配置 + } + } if (newConfig != null) { mergeApi.doMerge(merged, newConfig) // 再合并一次用户配置,使用户配置重新生效 } diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index e7617c16..27b78060 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -21,7 +21,10 @@ module.exports = { }, remoteConfig: { enabled: true, - url: 'https://gitee.com/wangliang181230/dev-sidecar/raw/docmirror/packages/core/src/config/remote_config.json5' + // 共享远程配置地址 + url: 'https://gitee.com/wangliang181230/dev-sidecar/raw/docmirror/packages/core/src/config/remote_config.json5', + // 个人远程配置地址 + personalUrl: '' }, theme: 'dark', // 主题:light=亮色, dark=暗色 autoChecked: true, // 是否自动检查更新 diff --git a/packages/gui/src/view/pages/setting.vue b/packages/gui/src/view/pages/setting.vue index 3de6de73..05204250 100644 --- a/packages/gui/src/view/pages/setting.vue +++ b/packages/gui/src/view/pages/setting.vue @@ -30,12 +30,16 @@
应用启动时会向下面的地址请求配置补丁,获得最新的优化后的github访问体验。
- 如果您觉得远程更新配置有安全风险,请关闭此功能。 + 如果您觉得远程配置有安全风险,请关闭此功能,或删除共享远程配置,仅使用个人远程配置。
+ 配置优先级:本地修改配置 > 个人远程配置 > 共享远程配置
- + + + + 重载远程配置
@@ -130,7 +134,9 @@ export default { key: 'app', removeUserConfigLoading: false, reloadLoading: false, - themeBackup: null + themeBackup: null, + urlBackup: null, + personalUrlBackup: null } }, created () { @@ -141,15 +147,26 @@ export default { methods: { ready (config) { this.themeBackup = config.app.theme + this.urlBackup = config.app.remoteConfig.url + this.personalUrlBackup = config.app.remoteConfig.personalUrl }, async openLog () { const dir = await this.$api.info.getConfigDir() this.$api.ipc.openPath(dir + '/logs/') }, async applyAfter () { - // 判断是否切换了主题 + let reloadLazy = 10 + + // 判断远程配置地址是否变更过,如果是则重载远程配置并重启服务 + if (this.config.app.remoteConfig.url !== this.urlBackup || this.config.app.remoteConfig.personalUrl !== this.personalUrlBackup) { + await this.$api.config.downloadRemoteConfig() + await this.reloadConfigAndRestart() + reloadLazy = 300 + } + + // 判断是否切换了主题,如果是,则刷新页面 if (this.config.app.theme !== this.themeBackup) { - window.location.reload() + setTimeout(() => window.location.reload(), reloadLazy) } }, async openExternal (url) { @@ -174,15 +191,20 @@ export default { } }, async reloadRemoteConfig () { + if (this.config.app.remoteConfig.enabled === false) { + return + } this.reloadLoading = true const remoteConfig = {} - await this.$api.config.readRemoteConfigStr().then((ret) => { remoteConfig.old = ret }) + await this.$api.config.readRemoteConfigStr().then((ret) => { remoteConfig.old1 = ret }) + await this.$api.config.readRemoteConfigStr('_personal').then((ret) => { remoteConfig.old2 = ret }) await this.$api.config.downloadRemoteConfig() - await this.$api.config.readRemoteConfigStr().then((ret) => { remoteConfig.new = ret }) + await this.$api.config.readRemoteConfigStr().then((ret) => { remoteConfig.new1 = ret }) + await this.$api.config.readRemoteConfigStr('_personal').then((ret) => { remoteConfig.new2 = ret }) - if (remoteConfig.old === remoteConfig.new) { + if (remoteConfig.old1 === remoteConfig.new1 && remoteConfig.old2 === remoteConfig.new2) { this.$message.info('远程配置没有变化,不做任何处理。') this.$message.warn('如果您确实修改了远程配置,请稍等片刻再重试!') } else {