| 1428 | } |
| 1429 | |
| 1430 | async function handleTestCpaService() { |
| 1431 | const apiUrl = document.getElementById('cpa-service-url').value.trim(); |
| 1432 | const apiToken = document.getElementById('cpa-service-token').value.trim(); |
| 1433 | const id = document.getElementById('cpa-service-id').value; |
| 1434 | |
| 1435 | if (!apiUrl) { |
| 1436 | toast.error('请先填写 API URL'); |
| 1437 | return; |
| 1438 | } |
| 1439 | // 新增时必须有 token,编辑时 token 可为空(用已保存的) |
| 1440 | if (!id && !apiToken) { |
| 1441 | toast.error('请先填写 API Token'); |
| 1442 | return; |
| 1443 | } |
| 1444 | |
| 1445 | elements.testCpaServiceBtn.disabled = true; |
| 1446 | elements.testCpaServiceBtn.textContent = '测试中...'; |
| 1447 | |
| 1448 | try { |
| 1449 | let result; |
| 1450 | if (id && !apiToken) { |
| 1451 | // 编辑时未填 token,直接测试已保存的服务 |
| 1452 | result = await api.post(`/cpa-services/${id}/test`); |
| 1453 | } else { |
| 1454 | result = await api.post('/cpa-services/test-connection', { api_url: apiUrl, api_token: apiToken }); |
| 1455 | } |
| 1456 | if (result.success) { |
| 1457 | toast.success(result.message); |
| 1458 | } else { |
| 1459 | toast.error(result.message); |
| 1460 | } |
| 1461 | } catch (e) { |
| 1462 | toast.error('测试失败: ' + e.message); |
| 1463 | } finally { |
| 1464 | elements.testCpaServiceBtn.disabled = false; |
| 1465 | elements.testCpaServiceBtn.textContent = '🔌 测试连接'; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | // ============================================================================ |
| 1470 | // Sub2API 服务管理 |