|
478 | 478 | <button
|
479 | 479 | class="btn btn-sm btn-outline"
|
480 | 480 | @click="testDatasourceConnection(agentDatasource.datasource.id)"
|
481 |
| - title="测试连接" |
| 481 | + :disabled="testingConnections.has(agentDatasource.datasource.id)" |
| 482 | + :title="testingConnections.has(agentDatasource.datasource.id) ? '测试中...' : '测试连接'" |
482 | 483 | >
|
483 |
| - 测试连接 |
| 484 | + <i v-if="testingConnections.has(agentDatasource.datasource.id)" class="bi bi-arrow-clockwise spin"></i> |
| 485 | + <span v-if="!testingConnections.has(agentDatasource.datasource.id)">测试连接</span> |
| 486 | + <span v-else>测试中...</span> |
484 | 487 | </button>
|
485 | 488 | <button
|
486 | 489 | class="btn btn-sm btn-danger"
|
@@ -1165,6 +1168,7 @@ export default {
|
1165 | 1168 | // 数据源测试相关
|
1166 | 1169 | const showTestResult = ref(false)
|
1167 | 1170 | const testResultMessage = ref('')
|
| 1171 | + const testingConnections = ref(new Set()) // 存储正在测试的数据源ID |
1168 | 1172 |
|
1169 | 1173 | // 初始化信息源相关数据
|
1170 | 1174 | const showSchemaInitModal = ref(false)
|
@@ -1505,18 +1509,43 @@ export default {
|
1505 | 1509 | }
|
1506 | 1510 |
|
1507 | 1511 | const testDatasourceConnection = async (datasourceId) => {
|
| 1512 | + // 防止重复测试同一个数据源 |
| 1513 | + if (testingConnections.value.has(datasourceId)) { |
| 1514 | + showMessage('该数据源正在测试中,请稍候', 'warning') |
| 1515 | + return |
| 1516 | + } |
| 1517 | +
|
| 1518 | + // 添加到测试中的集合 |
| 1519 | + testingConnections.value.add(datasourceId) |
| 1520 | + |
| 1521 | + // 创建30秒超时的Promise |
| 1522 | + const timeoutPromise = new Promise((_, reject) => { |
| 1523 | + setTimeout(() => { |
| 1524 | + reject(new Error('连接测试超时(30秒),请检查网络连接或数据库配置')) |
| 1525 | + }, 30000) |
| 1526 | + }) |
| 1527 | +
|
1508 | 1528 | try {
|
1509 |
| - const result = await datasourceApi.testConnection(datasourceId) |
| 1529 | + // 使用Promise.race来实现超时控制 |
| 1530 | + const result = await Promise.race([ |
| 1531 | + datasourceApi.testConnection(datasourceId), |
| 1532 | + timeoutPromise |
| 1533 | + ]) |
| 1534 | + |
1510 | 1535 | if (result.success) {
|
1511 | 1536 | showMessage('连接测试成功', 'success')
|
1512 | 1537 | } else {
|
1513 | 1538 | showMessage('连接测试失败:' + result.message, 'error')
|
1514 | 1539 | }
|
1515 | 1540 | // 重新加载数据源状态
|
1516 |
| - loadDatasources() |
| 1541 | + await loadDatasources() |
1517 | 1542 | } catch (error) {
|
1518 | 1543 | console.error('连接测试失败:', error)
|
1519 |
| - showMessage('连接测试失败,请重试', 'error') |
| 1544 | + const errorMessage = error.message || '连接测试失败,请重试' |
| 1545 | + showMessage(errorMessage, 'error') |
| 1546 | + } finally { |
| 1547 | + // 无论成功还是失败,都要从测试中的集合移除 |
| 1548 | + testingConnections.value.delete(datasourceId) |
1520 | 1549 | }
|
1521 | 1550 | }
|
1522 | 1551 |
|
@@ -2210,6 +2239,7 @@ export default {
|
2210 | 2239 | datasourceForm,
|
2211 | 2240 | showTestResult,
|
2212 | 2241 | testResultMessage,
|
| 2242 | + testingConnections, |
2213 | 2243 | // 初始化信息源相关
|
2214 | 2244 | schemaInitForm,
|
2215 | 2245 | availableTables,
|
|
0 commit comments