fix: 修复 AI 测试链接返回连接失败的问题

- 修复 AJAX 函数中缺少 api_id 参数检查导致的错误
- 统一错误消息格式,使用数组包装错误信息
- 修复 JavaScript 中错误消息解析逻辑
- 改进错误提示的可读性和调试信息
This commit is contained in:
2026-01-26 14:31:47 +08:00
parent 3794f37618
commit 540fe7b543
2 changed files with 16 additions and 10 deletions

View File

@@ -8787,15 +8787,21 @@ function argon_ajax_test_unified_api() {
check_ajax_referer('argon_test_unified_api', 'nonce'); check_ajax_referer('argon_test_unified_api', 'nonce');
if (!current_user_can('manage_options')) { if (!current_user_can('manage_options')) {
wp_send_json_error('权限不足'); wp_send_json_error(['message' => '权限不足']);
return;
}
$api_id = isset($_POST['api_id']) ? sanitize_text_field($_POST['api_id']) : '';
if (empty($api_id)) {
wp_send_json_error(['message' => '缺少 API ID 参数']);
return; return;
} }
$api_id = sanitize_text_field($_POST['api_id']);
$api = argon_get_api_by_id($api_id); $api = argon_get_api_by_id($api_id);
if (!$api) { if (!$api) {
wp_send_json_error('API 不存在'); wp_send_json_error(['message' => 'API 不存在']);
return; return;
} }
@@ -8816,7 +8822,7 @@ function argon_ajax_test_unified_api() {
$api_endpoint = !empty($api['api_endpoint']) ? $api['api_endpoint'] : (isset($default_endpoints[$api['provider']]) ? $default_endpoints[$api['provider']] : ''); $api_endpoint = !empty($api['api_endpoint']) ? $api['api_endpoint'] : (isset($default_endpoints[$api['provider']]) ? $default_endpoints[$api['provider']] : '');
if (empty($api_endpoint)) { if (empty($api_endpoint)) {
wp_send_json_error('未配置 API 端点'); wp_send_json_error(['message' => '未配置 API 端点']);
return; return;
} }
@@ -8865,7 +8871,7 @@ function argon_ajax_test_unified_api() {
$response_time = round((microtime(true) - $start_time) * 1000); $response_time = round((microtime(true) - $start_time) * 1000);
if (is_wp_error($response)) { if (is_wp_error($response)) {
wp_send_json_error('连接失败: ' . $response->get_error_message()); wp_send_json_error(['message' => '连接失败: ' . $response->get_error_message()]);
return; return;
} }
@@ -8880,7 +8886,7 @@ function argon_ajax_test_unified_api() {
'response_time' => $response_time 'response_time' => $response_time
]); ]);
} else { } else {
wp_send_json_error('API 返回格式异常: ' . substr($body, 0, 200)); wp_send_json_error(['message' => 'API 返回格式异常: ' . substr($body, 0, 200)]);
} }
} else { } else {
$error_msg = 'HTTP ' . $status_code; $error_msg = 'HTTP ' . $status_code;
@@ -8890,7 +8896,7 @@ function argon_ajax_test_unified_api() {
} else { } else {
$error_msg .= ': ' . substr($body, 0, 200); $error_msg .= ': ' . substr($body, 0, 200);
} }
wp_send_json_error($error_msg); wp_send_json_error(['message' => $error_msg]);
} }
} }
add_action('wp_ajax_argon_test_unified_api', 'argon_ajax_test_unified_api'); add_action('wp_ajax_argon_test_unified_api', 'argon_ajax_test_unified_api');

View File

@@ -2455,8 +2455,8 @@ function themeoptions_page(){
if (response.success) { if (response.success) {
alert('✓ ' + response.data.message); alert('✓ ' + response.data.message);
} else { } else {
// wp_send_json_error 的错误消息在 response.data 中 // wp_send_json_error 的错误消息在 response.data.message
let errorMsg = response.data || '<?php _e('未知错误', 'argon'); ?>'; let errorMsg = (response.data && response.data.message) ? response.data.message : '<?php _e('未知错误', 'argon'); ?>';
alert('✗ <?php _e('测试失败:', 'argon'); ?> ' + errorMsg); alert('✗ <?php _e('测试失败:', 'argon'); ?> ' + errorMsg);
} }
$btn.prop('disabled', false).html(originalHtml); $btn.prop('disabled', false).html(originalHtml);
@@ -2464,7 +2464,7 @@ function themeoptions_page(){
// AJAX 请求本身失败 // AJAX 请求本身失败
console.error('AJAX Error:', xhr, status, error); console.error('AJAX Error:', xhr, status, error);
console.error('Response Text:', xhr.responseText); console.error('Response Text:', xhr.responseText);
alert('<?php _e('请求失败,请重试', 'argon'); ?>'); alert('<?php _e('请求失败:', 'argon'); ?> ' + (xhr.responseText || error || '<?php _e('网络错误', 'argon'); ?>'));
$btn.prop('disabled', false).html(originalHtml); $btn.prop('disabled', false).html(originalHtml);
}); });
}); });