From 079ba0a261f2707f634ee9af4d71cfbbc58e9941 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Mon, 26 Jan 2026 11:58:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=A4=9A=20API=20?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=92=8C=E6=B7=BB=E5=8A=A0=E8=BF=9E=E9=80=9A?= =?UTF-8?q?=E6=80=A7=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 AI 文章摘要的 h3 标题 - 多 API 管理显示所有提供商的 API 配置(不再只显示选中的) - 为每个 API 配置添加测试按钮 - 实现 API 连通性测试功能(argon_test_api_connection) - 测试功能显示响应时间和连接状态 - 优化界面布局,提升用户体验 --- functions.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ settings.php | 82 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 148 insertions(+), 16 deletions(-) diff --git a/functions.php b/functions.php index eac8eb8..dea9608 100644 --- a/functions.php +++ b/functions.php @@ -7896,6 +7896,88 @@ function argon_get_ai_models() { } add_action('wp_ajax_argon_get_ai_models', 'argon_get_ai_models'); +/** + * AJAX: 测试 API 连通性 + */ +function argon_test_api_connection() { + check_ajax_referer('argon_test_api_connection', 'nonce'); + + if (!current_user_can('manage_options')) { + wp_send_json_error(__('权限不足', 'argon')); + } + + $provider = sanitize_text_field($_POST['provider']); + $api_key = sanitize_text_field($_POST['api_key']); + $api_endpoint = sanitize_text_field($_POST['api_endpoint']); + $model = sanitize_text_field($_POST['model']); + + if (empty($api_key)) { + wp_send_json_error(__('API 密钥不能为空', 'argon')); + } + + // 记录开始时间 + $start_time = microtime(true); + + // 测试提示词 + $test_prompt = __('请回复"连接成功"', 'argon'); + $test_content = __('测试', 'argon'); + + // 根据不同服务商调用 API + $result = false; + try { + switch ($provider) { + case 'openai': + $result = argon_call_openai_api($api_key, $test_prompt, $test_content); + break; + case 'anthropic': + $result = argon_call_anthropic_api($api_key, $test_prompt, $test_content); + break; + case 'deepseek': + $result = argon_call_deepseek_api($api_key, $test_prompt, $test_content); + break; + case 'xiaomi': + $result = argon_call_xiaomi_api($api_key, $test_prompt, $test_content); + break; + case 'qianwen': + $result = argon_call_qianwen_api($api_key, $test_prompt, $test_content); + break; + case 'wenxin': + $result = argon_call_wenxin_api($api_key, $test_prompt, $test_content); + break; + case 'doubao': + $result = argon_call_doubao_api($api_key, $test_prompt, $test_content); + break; + case 'kimi': + $result = argon_call_kimi_api($api_key, $test_prompt, $test_content); + break; + case 'zhipu': + $result = argon_call_zhipu_api($api_key, $test_prompt, $test_content); + break; + case 'siliconflow': + $result = argon_call_siliconflow_api($api_key, $test_prompt, $test_content); + break; + default: + wp_send_json_error(__('不支持的服务商', 'argon')); + } + } catch (Exception $e) { + wp_send_json_error($e->getMessage()); + } + + // 计算响应时间 + $response_time = round((microtime(true) - $start_time) * 1000); + + if ($result !== false) { + wp_send_json_success([ + 'message' => __('连接成功', 'argon'), + 'response_time' => $response_time, + 'model' => $model + ]); + } else { + wp_send_json_error(__('连接失败,请检查 API 密钥和网络连接', 'argon')); + } +} +add_action('wp_ajax_argon_test_api_connection', 'argon_test_api_connection'); + /** * AJAX: 添加 API 配置 */ diff --git a/settings.php b/settings.php index 4aa1b39..fa45cc0 100644 --- a/settings.php +++ b/settings.php @@ -1985,8 +1985,6 @@ function themeoptions_page(){

-

- @@ -2034,24 +2032,19 @@ function themeoptions_page(){ 'siliconflow' => __('硅基流动 (SiliconFlow)', 'argon') ]; - $current_provider = get_option('argon_ai_summary_provider', 'openai'); - + // 显示所有提供商的 API 配置 foreach ($providers as $provider_key => $provider_name) { - $is_current = ($provider_key === $current_provider); - $display_style = $is_current ? '' : 'display:none;'; - // 获取该提供商的所有 API 配置 $apis = argon_get_provider_apis($provider_key); $active_api_id = get_option("argon_ai_{$provider_key}_active_api", ''); ?> - - + +
-
$api): ?>
@@ -2075,10 +2068,17 @@ function themeoptions_page(){ - +