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

pull/292/head
王良 2024-04-10 16:32:22 +08:00 committed by GitHub
parent 106769c53e
commit d7751144bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 75 additions and 23 deletions

View File

@ -161,7 +161,7 @@ const configApi = {
const diffConfig = mergeApi.doDiff(defConfig, newConfig) const diffConfig = mergeApi.doDiff(defConfig, newConfig)
const configPath = _getConfigPath() const configPath = _getConfigPath()
fs.writeFileSync(configPath, jsonApi.stringify(diffConfig)) fs.writeFileSync(configPath, jsonApi.stringify(diffConfig))
log.info('保存自定义配置文件成功:', configPath) log.info('保存 config.json 自定义配置文件成功:', configPath)
// 重载配置 // 重载配置
const allConfig = configApi.set(diffConfig) const allConfig = configApi.set(diffConfig)
@ -203,15 +203,19 @@ const configApi = {
log.warn('newConfig 为空,不做任何操作') log.warn('newConfig 为空,不做任何操作')
return configTarget return configTarget
} }
return configApi.load(newConfig)
},
load (newConfig) {
const merged = lodash.cloneDeep(defConfig) const merged = lodash.cloneDeep(defConfig)
const remoteConfig = configApi.readRemoteConfig() const remoteConfig = configApi.readRemoteConfig()
mergeApi.doMerge(merged, remoteConfig) mergeApi.doMerge(merged, remoteConfig)
if (newConfig != null) {
mergeApi.doMerge(merged, newConfig) mergeApi.doMerge(merged, newConfig)
}
mergeApi.deleteNullItems(merged) mergeApi.deleteNullItems(merged)
configTarget = merged configTarget = merged
log.info('加载配置完成') log.info('加载及合并远程配置完成')
return configTarget return configTarget
}, },
@ -221,6 +225,29 @@ const configApi = {
addDefault (key, defValue) { addDefault (key, defValue) {
lodash.set(defConfig, 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) { resetDefault (key) {
if (key) { if (key) {
let value = lodash.get(defConfig, key) let value = lodash.get(defConfig, key)

View File

@ -31,13 +31,10 @@ export default {
}, },
async init () { async init () {
this.status = this.$status this.status = this.$status
await this.reloadConfig()
const config = await this.$api.config.reload() this.printConfig('Init, ')
this.setConfig(config)
this.systemPlatform = await this.$api.info.getSystemPlatform() this.systemPlatform = await this.$api.info.getSystemPlatform()
this.printConfig()
if (this.ready) { if (this.ready) {
return this.ready(this.config) return this.ready(this.config)
} }
@ -105,6 +102,21 @@ export default {
} }
return value 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 () { isWindows () {
return this.systemPlatform === 'windows' return this.systemPlatform === 'windows'
}, },

View File

@ -76,6 +76,7 @@
</div> </div>
<template slot="footer"> <template slot="footer">
<div class="footer-bar"> <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="resetDefaultLoading" class="md-mr-10" icon="sync" @click="resetDefault()"></a-button>
<a-button :loading="applyLoading" icon="check" type="primary" @click="apply()"></a-button> <a-button :loading="applyLoading" icon="check" type="primary" @click="apply()"></a-button>
</div> </div>
@ -92,6 +93,7 @@ export default {
data () { data () {
return { return {
key: 'app', key: 'app',
removeUserConfigLoading: false,
reloadLoading: false reloadLoading: false
} }
}, },
@ -108,16 +110,6 @@ export default {
this.$api.autoStart.enabled(this.config.app.autoStart.enabled) this.$api.autoStart.enabled(this.config.app.autoStart.enabled)
this.saveConfig() 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 () { async onRemoteConfigEnabledChange () {
await this.saveConfig() await this.saveConfig()
if (this.config.app.remoteConfig.enabled === true) { if (this.config.app.remoteConfig.enabled === true) {
@ -125,11 +117,11 @@ export default {
this.$message.info('开始下载远程配置') this.$message.info('开始下载远程配置')
await this.$api.config.downloadRemoteConfig() await this.$api.config.downloadRemoteConfig()
this.$message.info('下载远程配置成功,开始重启代理服务和系统代理') this.$message.info('下载远程配置成功,开始重启代理服务和系统代理')
await this.reloadAndRestart() await this.reloadConfigAndRestart()
this.reloadLoading = false this.reloadLoading = false
} else { } else {
this.$message.info('开始重启代理服务和系统代理') this.$message.info('远程配置已关闭,开始重启代理服务和系统代理')
await this.reloadAndRestart() await this.reloadConfigAndRestart()
} }
}, },
async reloadRemoteConfig () { async reloadRemoteConfig () {
@ -146,10 +138,31 @@ export default {
this.$message.warn('如果您确实修改了远程配置,请稍等片刻再重试!') this.$message.warn('如果您确实修改了远程配置,请稍等片刻再重试!')
} else { } else {
this.$message.success('获取到了最新的远程配置,开始重启代理服务和系统代理') this.$message.success('获取到了最新的远程配置,开始重启代理服务和系统代理')
await this.reloadAndRestart() await this.reloadConfigAndRestart()
} }
this.reloadLoading = false 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 () {}
})
} }
} }
} }