From 4b0c5c159a05a6c6a6e4ea2694638790018b7bf3 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Mon, 26 Jan 2026 14:12:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E9=A1=B5=E6=B5=8B=E8=AF=95=20AI=20=E8=BF=9E=E9=80=9A=E6=80=A7?= =?UTF-8?q?=E6=B0=B8=E8=BF=9C=E6=8A=A5=E8=BF=9E=E6=8E=A5=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在所有 wp_send_json_error 和 wp_send_json_success 后添加 return 语句 - 添加 API 端点存在性检查,避免访问不存在的数组键 - 添加 stream 参数设置为 false(非流式响应) - 添加 Accept 请求头确保返回 JSON 格式 - 使用 JSON_UNESCAPED_UNICODE 编码确保中文正确传输 - 明确设置 sslverify 为 true - 改进错误消息显示,截断过长的响应内容 - 优化成功消息,添加'测试成功!'前缀 --- functions.php | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/functions.php b/functions.php index 08f1dff..0e17d58 100644 --- a/functions.php +++ b/functions.php @@ -8788,6 +8788,7 @@ function argon_ajax_test_unified_api() { if (!current_user_can('manage_options')) { wp_send_json_error('权限不足'); + return; } $api_id = sanitize_text_field($_POST['api_id']); @@ -8795,6 +8796,7 @@ function argon_ajax_test_unified_api() { if (!$api) { wp_send_json_error('API 不存在'); + return; } // 获取默认端点 @@ -8811,7 +8813,13 @@ function argon_ajax_test_unified_api() { 'siliconflow' => 'https://api.siliconflow.cn/v1/chat/completions' ]; - $api_endpoint = !empty($api['api_endpoint']) ? $api['api_endpoint'] : $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)) { + wp_send_json_error('未配置 API 端点'); + return; + } + $model = !empty($api['model']) ? $api['model'] : 'gpt-4o-mini'; // 构建测试请求 @@ -8823,7 +8831,8 @@ function argon_ajax_test_unified_api() { 'content' => '你好,这是一个测试。请回复"测试成功"。' ] ], - 'max_tokens' => 50 + 'max_tokens' => 50, + 'stream' => false ]; $start_time = microtime(true); @@ -8831,16 +8840,19 @@ function argon_ajax_test_unified_api() { $response = wp_remote_post($api_endpoint, [ 'headers' => [ 'Content-Type' => 'application/json', - 'Authorization' => 'Bearer ' . $api['api_key'] + 'Authorization' => 'Bearer ' . $api['api_key'], + 'Accept' => 'application/json' ], - 'body' => json_encode($data), - 'timeout' => 30 + 'body' => json_encode($data, JSON_UNESCAPED_UNICODE), + 'timeout' => 30, + 'sslverify' => true ]); $response_time = round((microtime(true) - $start_time) * 1000); if (is_wp_error($response)) { - wp_send_json_error($response->get_error_message()); + wp_send_json_error('连接失败: ' . $response->get_error_message()); + return; } $status_code = wp_remote_retrieve_response_code($response); @@ -8850,14 +8862,21 @@ function argon_ajax_test_unified_api() { $result = json_decode($body, true); if (isset($result['choices'][0]['message']['content']) || isset($result['content'])) { wp_send_json_success([ - 'message' => '响应时间: ' . $response_time . 'ms', + 'message' => '测试成功!响应时间: ' . $response_time . 'ms', 'response_time' => $response_time ]); } else { - wp_send_json_error('API 返回格式异常: ' . $body); + wp_send_json_error('API 返回格式异常: ' . substr($body, 0, 200)); } } else { - wp_send_json_error('HTTP ' . $status_code . ': ' . $body); + $error_msg = 'HTTP ' . $status_code; + $result = json_decode($body, true); + if ($result && isset($result['error']['message'])) { + $error_msg .= ': ' . $result['error']['message']; + } else { + $error_msg .= ': ' . substr($body, 0, 200); + } + wp_send_json_error($error_msg); } } add_action('wp_ajax_argon_test_unified_api', 'argon_ajax_test_unified_api');