From 3f188b76f43575cd5d55fed234c1ed7a792bdf29 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Mon, 26 Jan 2026 13:42:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=20AI=20=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E9=A1=B5=E9=9D=A2=20UI=20=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除旧的按提供商分组的 API 配置界面(约 600 行) - 添加新的统一 API 管理界面 - 实现完整的 JavaScript 交互功能: - 添加/编辑/删除 API - 测试 API 连通性 - 刷新模型列表 - 场景化 API 选择 - 重新组织 AI 功能结构: - API 管理 (h2) - 文章摘要 (h2) - 评论审核 (h2) - 所有 API 在一个列表中统一管理 - 不同场景可以使用不同的 API - 文件行数:7812 7514 (-298 行) --- functions.php | 103 +++++ settings.php | 1165 ++++++++++++++++++++++++------------------------- 2 files changed, 664 insertions(+), 604 deletions(-) diff --git a/functions.php b/functions.php index 05ff436..8637a0f 100644 --- a/functions.php +++ b/functions.php @@ -8968,6 +8968,109 @@ function argon_ajax_get_all_unified_apis() { } add_action('wp_ajax_argon_get_all_unified_apis', 'argon_ajax_get_all_unified_apis'); +/** + * AJAX: 获取单个 API 配置 + */ +function argon_ajax_get_unified_api() { + check_ajax_referer('argon_manage_unified_apis', 'nonce'); + + if (!current_user_can('manage_options')) { + wp_send_json_error('权限不足'); + } + + $api_id = sanitize_text_field($_POST['api_id']); + $api = argon_get_api_by_id($api_id); + + if ($api) { + wp_send_json_success($api); + } else { + wp_send_json_error('API 不存在'); + } +} +add_action('wp_ajax_argon_get_unified_api', 'argon_ajax_get_unified_api'); + +/** + * AJAX: 测试统一 API 连通性 + */ +function argon_ajax_test_unified_api() { + check_ajax_referer('argon_test_unified_api', 'nonce'); + + if (!current_user_can('manage_options')) { + wp_send_json_error('权限不足'); + } + + $api_id = sanitize_text_field($_POST['api_id']); + $api = argon_get_api_by_id($api_id); + + if (!$api) { + wp_send_json_error('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'; + + // 构建测试请求 + $data = [ + 'model' => $model, + 'messages' => [ + [ + 'role' => 'user', + 'content' => '你好,这是一个测试。请回复"测试成功"。' + ] + ], + 'max_tokens' => 50 + ]; + + $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)) { + wp_send_json_error($response->get_error_message()); + } + + $status_code = wp_remote_retrieve_response_code($response); + $body = wp_remote_retrieve_body($response); + + if ($status_code === 200) { + $result = json_decode($body, true); + if (isset($result['choices'][0]['message']['content']) || isset($result['content'])) { + wp_send_json_success([ + 'message' => '响应时间: ' . $response_time . 'ms', + 'response_time' => $response_time + ]); + } else { + wp_send_json_error('API 返回格式异常: ' . $body); + } + } else { + wp_send_json_error('HTTP ' . $status_code . ': ' . $body); + } +} +add_action('wp_ajax_argon_test_unified_api', 'argon_ajax_test_unified_api'); + /** * AJAX: 获取提供商的所有 API 配置 */ diff --git a/settings.php b/settings.php index bbd613b..0dc942f 100644 --- a/settings.php +++ b/settings.php @@ -1982,622 +1982,579 @@ function themeoptions_page(){ - -

+ + +

+

- - - - -

- - + +

- - - - -

- - - - 'OpenAI (ChatGPT)', - 'anthropic' => 'Anthropic (Claude)', - 'deepseek' => 'DeepSeek', - 'xiaomi' => __('小米 Mimo', 'argon'), - 'qianwen' => __('通义千问', 'argon'), - 'wenxin' => __('文心一言', 'argon'), - 'doubao' => __('豆包 (火山引擎)', 'argon'), - 'kimi' => 'Kimi (Moonshot)', - 'zhipu' => __('智谱 AI (GLM)', 'argon'), - 'siliconflow' => __('硅基流动 (SiliconFlow)', 'argon') - ]; - - // 显示所有提供商的 API 配置 - foreach ($providers as $provider_key => $provider_name) { - // 获取该提供商的所有 API 配置 - $apis = argon_get_provider_apis($provider_key); - $active_api_id = get_option("argon_ai_{$provider_key}_active_api", ''); - ?> - - - - -
- -
-
- $api): ?> -
- + + + + 'OpenAI (ChatGPT)', + 'anthropic' => 'Anthropic (Claude)', + 'deepseek' => 'DeepSeek', + 'xiaomi' => __('小米 Mimo', 'argon'), + 'qianwen' => __('通义千问', 'argon'), + 'wenxin' => __('文心一言', 'argon'), + 'doubao' => __('豆包 (火山引擎)', 'argon'), + 'kimi' => 'Kimi (Moonshot)', + 'zhipu' => __('智谱 AI (GLM)', 'argon'), + 'siliconflow' => __('硅基流动 (SiliconFlow)', 'argon') + ]; + ?> + +
+ + +
+
+
+
+ + + () +
- +
+ + + + ... + + | + +
+
+ + + + + + + + + + +
+
+
+ + + +
- -

- - - - - - - - - -
- - - - - - - - -

- - + + +

+ +

+ +
+ + + + + + +

+ + +

+ + - - - - -

- - + +

- - - - -

- - + + + + +

+ + - - - - -

- - - + html += '
'; + $list.html(html); + } else { + $list.html('

'); + } + $btn.prop('disabled', false).html(originalHtml); + }).fail(function() { + $list.html('

'); + $btn.prop('disabled', false).html(originalHtml); + }); + }); + + // 选择模型 + $(document).on('click', '.argon-select-model', function() { + let model = $(this).data('model'); + $('#argon-unified-api-form-model').val(model); + $('#argon-unified-models-list').slideUp(); + }); + + // ========== 清除 AI 摘要缓存 ========== + $('#argon-clear-ai-summary-cache').on('click', function() { + if (!confirm('')) { + return; + } + + let $btn = $(this); + let originalHtml = $btn.html(); + $btn.prop('disabled', true).html(' '); + + $.post(ajaxurl, { + action: 'argon_ajax_clear_ai_summaries' + }, function(response) { + if (response.success) { + alert('✓ ' + response.data.message); + } else { + alert('✗ ' + response.data.message); + } + $btn.prop('disabled', false).html(originalHtml); + }).fail(function() { + alert(''); + $btn.prop('disabled', false).html(originalHtml); + }); + }); + + // ========== Prompt 模式切换 ========== + $('select[name="argon_comment_spam_detection_prompt_mode"]').on('change', function() { + if ($(this).val() === 'custom') { + $('.argon-custom-prompt-row').slideDown(); + } else { + $('.argon-custom-prompt-row').slideUp(); + } + }); + }); + - - - - - - -

- - + -

+ +