Browse Source

feature: 新增恢复出厂设置功能。 (#291)

pull/292/head
王良 8 months ago committed by GitHub
parent
commit
d7751144bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 35
      packages/core/src/config.js
  2. 22
      packages/gui/src/view/mixins/plugin.js
  3. 41
      packages/gui/src/view/pages/setting.vue

35
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)

22
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'
},

41
packages/gui/src/view/pages/setting.vue

@ -76,6 +76,7 @@
</div>
<template slot="footer">
<div class="footer-bar">
<a-button :loading="removeUserConfigLoading" class="md-mr-10" icon="sync" @click="restoreFactorySettings()">恢复出厂设置</a-button>
<a-button :loading="resetDefaultLoading" class="md-mr-10" icon="sync" @click="resetDefault()">恢复默认</a-button>
<a-button :loading="applyLoading" icon="check" type="primary" @click="apply()">应用</a-button>
</div>
@ -92,6 +93,7 @@ export default {
data () {
return {
key: 'app',
removeUserConfigLoading: false,
reloadLoading: false
}
},
@ -108,16 +110,6 @@ export default {
this.$api.autoStart.enabled(this.config.app.autoStart.enabled)
this.saveConfig()
},
async reloadAndRestart () {
this.$api.config.reload()
if (this.status.server.enabled || this.status.proxy.enabled) {
await this.$api.proxy.restart()
await this.$api.server.restart()
this.$message.info('代理服务和系统代理重启成功')
} else {
this.$message.info('代理服务和系统代理未启动,无需重启')
}
},
async onRemoteConfigEnabledChange () {
await this.saveConfig()
if (this.config.app.remoteConfig.enabled === true) {
@ -125,11 +117,11 @@ export default {
this.$message.info('开始下载远程配置')
await this.$api.config.downloadRemoteConfig()
this.$message.info('下载远程配置成功,开始重启代理服务和系统代理')
await this.reloadAndRestart()
await this.reloadConfigAndRestart()
this.reloadLoading = false
} else {
this.$message.info('开始重启代理服务和系统代理')
await this.reloadAndRestart()
this.$message.info('远程配置已关闭,开始重启代理服务和系统代理')
await this.reloadConfigAndRestart()
}
},
async reloadRemoteConfig () {
@ -146,10 +138,31 @@ export default {
this.$message.warn('如果您确实修改了远程配置,请稍等片刻再重试!')
} else {
this.$message.success('获取到了最新的远程配置,开始重启代理服务和系统代理')
await this.reloadAndRestart()
await this.reloadConfigAndRestart()
}
this.reloadLoading = false
},
async restoreFactorySettings () {
this.$confirm({
title: '提示',
content: '确定要恢复出厂设置吗????????????——————————————————————警告:该功能将删除您的所有页面的个性化配置,并重载默认配置及远程配置(如果启用了的话),请谨慎操作!!!',
cancelText: '取消',
okText: '确定',
onOk: async () => {
this.removeUserConfigLoading = true
const result = await this.$api.config.removeUserConfig()
if (result) {
this.config = await this.$api.config.get()
this.$message.success('恢复出厂配置成功,开始重启代理服务和系统代理')
await this.reloadConfigAndRestart()
} else {
this.$message.info('已是出厂配置,无需恢复')
}
this.removeUserConfigLoading = false
},
onCancel () {}
})
}
}
}

Loading…
Cancel
Save