From cedd9776734cbbb72a9e47bd8da1570c66d2e829 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Mon, 26 Jan 2026 13:52:26 +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=20AI=20API=20=E6=B5=8B=E8=AF=95=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 改进错误消息的显示逻辑,正确处理 wp_send_json_error 返回的数据 - 添加更详细的 AJAX 错误日志输出 - 添加测试脚本 test-api-test-function.php 用于诊断 API 测试功能 --- settings.php | 8 +- test-api-test-function.php | 164 +++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 test-api-test-function.php diff --git a/settings.php b/settings.php index de7c242..2654b11 100644 --- a/settings.php +++ b/settings.php @@ -2454,10 +2454,14 @@ function themeoptions_page(){ if (response.success) { alert('✓ ' + response.data.message); } else { - alert('✗ ' + response.data.message); + // wp_send_json_error 的错误消息在 response.data 中 + let errorMsg = response.data || ''; + alert('✗ ' + errorMsg); } $btn.prop('disabled', false).html(originalHtml); - }).fail(function() { + }).fail(function(xhr, status, error) { + // AJAX 请求本身失败 + console.error('AJAX Error:', status, error); alert(''); $btn.prop('disabled', false).html(originalHtml); }); diff --git a/test-api-test-function.php b/test-api-test-function.php new file mode 100644 index 0000000..de60996 --- /dev/null +++ b/test-api-test-function.php @@ -0,0 +1,164 @@ +API 测试功能诊断'; + +// 1. 检查函数是否存在 +echo '

1. 函数存在性检查

'; +$functions = [ + 'argon_ajax_test_unified_api', + 'argon_get_api_by_id', + 'argon_get_all_apis' +]; + +foreach ($functions as $func) { + if (function_exists($func)) { + echo '

✓ ' . $func . ' 存在

'; + } else { + echo '

✗ ' . $func . ' 不存在

'; + } +} + +// 2. 检查是否有 API 配置 +echo '

2. API 配置检查

'; +$all_apis = argon_get_all_apis(); +if (empty($all_apis)) { + echo '

⚠ 没有配置任何 API

'; +} else { + echo '

✓ 已配置 ' . count($all_apis) . ' 个 API

'; + foreach ($all_apis as $api) { + echo '
'; + echo '' . esc_html($api['name']) . '
'; + echo 'ID: ' . esc_html($api['id']) . '
'; + echo '提供商: ' . esc_html($api['provider']) . '
'; + echo '模型: ' . esc_html($api['model']) . '
'; + echo '
'; + } +} + +// 3. 检查 AJAX action 是否注册 +echo '

3. AJAX Action 注册检查

'; +global $wp_filter; +if (isset($wp_filter['wp_ajax_argon_ajax_test_unified_api'])) { + echo '

✓ wp_ajax_argon_ajax_test_unified_api 已注册

'; +} else { + echo '

✗ wp_ajax_argon_ajax_test_unified_api 未注册

'; +} + +// 4. 测试 nonce 生成 +echo '

4. Nonce 生成测试

'; +$nonce = wp_create_nonce('argon_test_unified_api'); +echo '

生成的 nonce: ' . $nonce . '

'; +echo '

验证结果: '; +if (wp_verify_nonce($nonce, 'argon_test_unified_api')) { + echo '✓ 验证通过

'; +} else { + echo '✗ 验证失败

'; +} + +// 5. 模拟 AJAX 请求测试 +if (!empty($all_apis)) { + echo '

5. 模拟 AJAX 请求测试

'; + $test_api = $all_apis[0]; + echo '

测试 API: ' . esc_html($test_api['name']) . '

'; + + // 模拟 $_POST 数据 + $_POST['api_id'] = $test_api['id']; + $_POST['nonce'] = wp_create_nonce('argon_test_unified_api'); + + echo '

开始测试...

'; + + // 获取 API 配置 + $api = argon_get_api_by_id($test_api['id']); + if (!$api) { + echo '

✗ 无法获取 API 配置

'; + } else { + echo '

✓ 成功获取 API 配置

'; + + // 获取默认端点 + $default_endpoints = [ + 'openai' => 'https://api.openai.com/v1/chat/completions', + 'anthropic' => 'https://api.anthropic.com/v1/messages', + 'deepseek' => 'https://api.deepseek.com/v1/chat/completions', + 'xiaomi' => 'https://api.mimo.xiaomi.com/v1/chat/completions', + 'qianwen' => 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions', + 'wenxin' => 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions', + 'doubao' => 'https://ark.cn-beijing.volces.com/api/v3/chat/completions', + 'kimi' => 'https://api.moonshot.cn/v1/chat/completions', + 'zhipu' => 'https://open.bigmodel.cn/api/paas/v4/chat/completions', + 'siliconflow' => 'https://api.siliconflow.cn/v1/chat/completions' + ]; + + $api_endpoint = !empty($api['api_endpoint']) ? $api['api_endpoint'] : $default_endpoints[$api['provider']]; + $model = !empty($api['model']) ? $api['model'] : 'gpt-4o-mini'; + + echo '

API 端点: ' . esc_html($api_endpoint) . '

'; + echo '

模型: ' . esc_html($model) . '

'; + + // 构建测试请求 + $data = [ + 'model' => $model, + 'messages' => [ + [ + 'role' => 'user', + 'content' => '你好,这是一个测试。请回复"测试成功"。' + ] + ], + 'max_tokens' => 50 + ]; + + echo '

发送测试请求...

'; + $start_time = microtime(true); + + $response = wp_remote_post($api_endpoint, [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'Authorization' => 'Bearer ' . $api['api_key'] + ], + 'body' => json_encode($data), + 'timeout' => 30 + ]); + + $response_time = round((microtime(true) - $start_time) * 1000); + + if (is_wp_error($response)) { + echo '

✗ 请求失败: ' . $response->get_error_message() . '

'; + } else { + $status_code = wp_remote_retrieve_response_code($response); + $body = wp_remote_retrieve_body($response); + + echo '

HTTP 状态码: ' . $status_code . '

'; + echo '

响应时间: ' . $response_time . 'ms

'; + + if ($status_code === 200) { + $result = json_decode($body, true); + if (isset($result['choices'][0]['message']['content']) || isset($result['content'])) { + echo '

✓ API 测试成功!

'; + if (isset($result['choices'][0]['message']['content'])) { + echo '

AI 回复: ' . esc_html($result['choices'][0]['message']['content']) . '

'; + } + } else { + echo '

✗ API 返回格式异常

'; + echo '
' . htmlspecialchars($body) . '
'; + } + } else { + echo '

✗ API 返回错误

'; + echo '
' . htmlspecialchars($body) . '
'; + } + } + } +} + +echo '
'; +echo '

测试完成

'; +echo '

返回设置页

'; +?>