File size: 1,765 Bytes
81d8726 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 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
}
|