feat(远程备份): 添加连接测试功能并优化协议切换逻辑

前端添加连接测试功能,使用 getFieldsValue 获取完整表单值并打印日志
后端实现 /remote/test 接口,支持协议自动推断和连接测试
优化协议切换时的表单重置逻辑,清空相关字段并设置默认路径
This commit is contained in:
zhang1106
2026-03-23 10:40:26 +08:00
parent a5da9c04c9
commit 66807b1660
2 changed files with 71 additions and 3 deletions
+53
View File
@@ -798,6 +798,59 @@ router.delete('/remote/targets/:id', (req, res) => {
}
});
// 直接测试远端连接(不保存,用于添加节点前的测试)
router.post('/remote/test', async (req, res) => {
try {
const config = req.body;
console.log(`[RemoteBackup] 收到连接测试请求:`, JSON.stringify(config));
if (!config.host || !config.port || !config.username) {
return res.status(400).json({
success: false,
message: '请提供完整的连接信息(主机、端口、用户名)',
});
}
if (!config.protocol) {
const port = parseInt(config.port);
if (port === 22 || port === 2222) {
config.protocol = 'sftp';
} else if (port === 445 || port === 139 || port === 443) {
config.protocol = 'smb';
} else if (port === 80 || port === 443 || config.url) {
config.protocol = 'webdav';
} else {
config.protocol = 'ftp';
}
console.log(`[RemoteBackup] 推断协议类型: ${config.protocol} (基于端口 ${config.port})`);
}
const result = await testRemoteConnection(config);
console.log(`[RemoteBackup] 连接测试结果:`, JSON.stringify(result));
if (result.success) {
res.json({
success: true,
message: '连接测试成功',
data: result,
});
} else {
res.status(500).json({
success: false,
message: result.message,
error: result.error,
});
}
} catch (error) {
console.error('[RemoteBackup] 测试远端连接失败:', error);
res.status(500).json({
success: false,
message: '测试远端连接失败',
error: error.message,
});
}
});
// 测试远端连接
router.post('/remote/targets/:id/test', async (req, res) => {
try {
+18 -3
View File
@@ -293,8 +293,10 @@ const RemoteBackupSettings = () => {
try {
setTesting(true);
setTestStatus('testing');
const values = await form.validateFields();
await form.validateFields();
const values = form.getFieldsValue(true);
console.log('[TestConnection] getFieldsValue(true):', JSON.stringify(values));
const response = await api.post('/backup/remote/test', values);
if (response?.success) {
@@ -832,7 +834,20 @@ const RemoteBackupSettings = () => {
placeholder="选择协议类型"
size="large"
onChange={() => {
form.setFieldsValue({});
form.setFieldsValue({
host: undefined,
port: undefined,
username: undefined,
password: undefined,
secure: undefined,
privateKey: undefined,
passphrase: undefined,
share: undefined,
domain: undefined,
url: undefined,
authType: undefined,
rootPath: '/',
});
setTestStatus(null);
}}
>