diff --git a/server/app/config/index.js b/server/app/config/index.js index 4c41ea7..075bde5 100644 --- a/server/app/config/index.js +++ b/server/app/config/index.js @@ -24,6 +24,7 @@ module.exports = { proxyDBPath: path.join(process.cwd(),'app/db/proxy.db'), fileTransferDBPath: path.join(process.cwd(),'app/db/file-transfer.db'), terminalConfigDBPath: path.join(process.cwd(),'app/db/terminal-config.db'), + serverListDBPath: path.join(process.cwd(),'app/db/server-list-config.db'), apiPrefix: '/api/v1', logConfig: { outDir: path.join(process.cwd(),'./app/db/logs'), diff --git a/server/app/controller/server-list-config.js b/server/app/controller/server-list-config.js new file mode 100644 index 0000000..247eb47 --- /dev/null +++ b/server/app/controller/server-list-config.js @@ -0,0 +1,70 @@ +const { ServerListDB } = require('../utils/db-class') +const serverListDB = new ServerListDB().getInstance() + +// 获取服务器列表配置 +async function getServerListConfig({ res }) { + try { + const config = await serverListDB.findOneAsync({}) + if (!config) { + // 返回默认配置 + const defaultConfig = { + columnSettings: { + selection: true, + index: true, + name: true, + username: true, + host: true, + port: true, + authType: true, + proxyType: false, + expired: false, + consoleUrl: false, + tag: true + }, + displayMode: 'group' + } + return res.success({ data: defaultConfig }) + } + // eslint-disable-next-line no-unused-vars + const { _id, ...configData } = config + res.success({ data: configData }) + } catch (error) { + console.error('getServerListConfig error: ', error) + res.fail({ msg: '获取配置失败' }) + } +} + +// 保存服务器列表配置 +async function saveServerListConfig({ res, request }) { + try { + const { body } = request + if (!body || typeof body !== 'object') { + return res.fail({ msg: '参数错误' }) + } + + const existingConfig = await serverListDB.findOneAsync({}) + + if (existingConfig) { + // 更新现有配置 + await serverListDB.updateAsync( + { _id: existingConfig._id }, + { $set: body }, + {} + ) + } else { + // 插入新配置 + await serverListDB.insertAsync(body) + } + + res.success({ msg: '保存成功' }) + } catch (error) { + console.error('saveServerListConfig error: ', error) + res.fail({ msg: '保存配置失败' }) + } +} + +module.exports = { + getServerListConfig, + saveServerListConfig +} + diff --git a/server/app/router/routes.js b/server/app/router/routes.js index 7d70f71..7830ea1 100644 --- a/server/app/router/routes.js +++ b/server/app/router/routes.js @@ -10,6 +10,7 @@ const { getLog, saveIpWhiteList, removeSomeLoginRecords } = require('../controll const { getAIConfig, saveAIConfig, getAIModels, getChatHistory, saveChatHistory, removeChatHistory } = require('../controller/chat') const { getProxyList, addProxy, updateProxy, removeProxy } = require('../controller/proxy') const { getTerminalConfig, saveTerminalConfig } = require('../controller/terminal-config') +const { getServerListConfig, saveServerListConfig } = require('../controller/server-list-config') const ssh = [ { @@ -349,6 +350,19 @@ const terminalConfig = [ } ] +const serverListConfig = [ + { + method: 'get', + path: '/server-list-config', + controller: getServerListConfig + }, + { + method: 'post', + path: '/server-list-config', + controller: saveServerListConfig + } +] + module.exports = [].concat( ssh, host, @@ -361,5 +375,6 @@ module.exports = [].concat( log, aiConfig, proxy, - terminalConfig + terminalConfig, + serverListConfig ) diff --git a/server/app/utils/db-class.js b/server/app/utils/db-class.js index 8ca0cd9..1a4a930 100644 --- a/server/app/utils/db-class.js +++ b/server/app/utils/db-class.js @@ -16,7 +16,8 @@ const { favoriteSftpDBPath, proxyDBPath, fileTransferDBPath, - terminalConfigDBPath + terminalConfigDBPath, + serverListDBPath } = require('../config') module.exports.KeyDB = class KeyDB { @@ -213,4 +214,15 @@ module.exports.TerminalConfigDB = class TerminalConfigDB { getInstance() { return TerminalConfigDB.instance } +} + +module.exports.ServerListDB = class ServerListDB { + constructor() { + if (!ServerListDB.instance) { + ServerListDB.instance = new Datastore({ filename: serverListDBPath, autoload: true }) + } + } + getInstance() { + return ServerListDB.instance + } } \ No newline at end of file diff --git a/web/src/api/index.js b/web/src/api/index.js index 8ac0b8a..cf92c82 100644 --- a/web/src/api/index.js +++ b/web/src/api/index.js @@ -190,5 +190,12 @@ export default { }, saveTerminalConfig(data) { return axios({ url: '/terminal-config', method: 'post', data }) + }, + // 服务器列表配置相关API + getServerListConfig(params = {}) { + return axios({ url: '/server-list-config', method: 'get', params }) + }, + saveServerListConfig(data) { + return axios({ url: '/server-list-config', method: 'post', data }) } } diff --git a/web/src/store/index.js b/web/src/store/index.js index d3b885f..5725814 100644 --- a/web/src/store/index.js +++ b/web/src/store/index.js @@ -51,6 +51,23 @@ const useStore = defineStore('global', { ], // 终端配置占位 terminalConfig: {}, + // 服务器列表配置 + serverListConfig: { + columnSettings: { + selection: true, + index: true, + name: true, + username: true, + host: true, + port: true, + authType: true, + proxyType: false, + expired: false, + consoleUrl: false, + tag: false + }, + displayMode: localStorage.getItem('host_list_display_mode') || 'group' + }, menuSetting: { ...{ scriptLibrary: true, @@ -95,6 +112,7 @@ const useStore = defineStore('global', { await this.getPlusInfo() await this.getProxyList() await this.getTerminalConfig() // 添加终端配置获取 + await this.getServerListConfig() // 添加服务器列表配置获取 this.getAIConfig() this.getChatHistory() }, @@ -162,6 +180,19 @@ const useStore = defineStore('global', { await $api.saveTerminalConfig(newConfig) this.$patch({ terminalConfig: newConfig }) }, + async getServerListConfig() { + const { data: serverListConfig } = await $api.getServerListConfig() + if (serverListConfig.displayMode) { + // 将displayMode写入localStorage,避免刷新时闪烁 + localStorage.setItem('host_list_display_mode', serverListConfig.displayMode) + } + this.$patch({ serverListConfig }) + }, + async setServerListConfig(setTarget = {}) { + const newConfig = { ...this.serverListConfig, ...setTarget } + await $api.saveServerListConfig(newConfig) + await this.getServerListConfig() + }, setMenuSetting(setTarget = {}) { let newConfig = { ...this.menuSetting, ...setTarget } localStorage.setItem('menuSetting', JSON.stringify(newConfig)) diff --git a/web/src/views/scripts/index.vue b/web/src/views/scripts/index.vue index e573973..a9a39b1 100644 --- a/web/src/views/scripts/index.vue +++ b/web/src/views/scripts/index.vue @@ -68,7 +68,12 @@ - +