feat: 优化多 API 管理和添加连通性测试
- 移除 AI 文章摘要的 h3 标题 - 多 API 管理显示所有提供商的 API 配置(不再只显示选中的) - 为每个 API 配置添加测试按钮 - 实现 API 连通性测试功能(argon_test_api_connection) - 测试功能显示响应时间和连接状态 - 优化界面布局,提升用户体验
This commit is contained in:
@@ -7896,6 +7896,88 @@ function argon_get_ai_models() {
|
|||||||
}
|
}
|
||||||
add_action('wp_ajax_argon_get_ai_models', '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 配置
|
* AJAX: 添加 API 配置
|
||||||
*/
|
*/
|
||||||
|
|||||||
82
settings.php
82
settings.php
@@ -1985,8 +1985,6 @@ function themeoptions_page(){
|
|||||||
<!-- ========== 12. 文章功能 ========== -->
|
<!-- ========== 12. 文章功能 ========== -->
|
||||||
<tr><th class="subtitle"><h2 id="section-post-features"><?php _e('文章功能', 'argon');?></h2></th></tr>
|
<tr><th class="subtitle"><h2 id="section-post-features"><?php _e('文章功能', 'argon');?></h2></th></tr>
|
||||||
|
|
||||||
<tr><th class="subtitle"><h3 id="subsection-ai-summary"><?php _e('AI 文章摘要', 'argon');?></h3></th></tr>
|
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<th><label><?php _e('启用 AI 文章摘要', 'argon');?></label></th>
|
<th><label><?php _e('启用 AI 文章摘要', 'argon');?></label></th>
|
||||||
<td>
|
<td>
|
||||||
@@ -2034,24 +2032,19 @@ function themeoptions_page(){
|
|||||||
'siliconflow' => __('硅基流动 (SiliconFlow)', 'argon')
|
'siliconflow' => __('硅基流动 (SiliconFlow)', 'argon')
|
||||||
];
|
];
|
||||||
|
|
||||||
$current_provider = get_option('argon_ai_summary_provider', 'openai');
|
// 显示所有提供商的 API 配置
|
||||||
|
|
||||||
foreach ($providers as $provider_key => $provider_name) {
|
foreach ($providers as $provider_key => $provider_name) {
|
||||||
$is_current = ($provider_key === $current_provider);
|
|
||||||
$display_style = $is_current ? '' : 'display:none;';
|
|
||||||
|
|
||||||
// 获取该提供商的所有 API 配置
|
// 获取该提供商的所有 API 配置
|
||||||
$apis = argon_get_provider_apis($provider_key);
|
$apis = argon_get_provider_apis($provider_key);
|
||||||
$active_api_id = get_option("argon_ai_{$provider_key}_active_api", '');
|
$active_api_id = get_option("argon_ai_{$provider_key}_active_api", '');
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<tr class="argon-provider-config" data-provider="<?php echo esc_attr($provider_key); ?>" style="<?php echo $display_style; ?>">
|
<tr class="argon-provider-config" data-provider="<?php echo esc_attr($provider_key); ?>">
|
||||||
<th><label><?php echo esc_html($provider_name); ?> - <?php _e('API 配置', 'argon');?></label></th>
|
<th><label><?php echo esc_html($provider_name); ?></label></th>
|
||||||
<td>
|
<td>
|
||||||
<div class="argon-api-list" data-provider="<?php echo esc_attr($provider_key); ?>">
|
<div class="argon-api-list" data-provider="<?php echo esc_attr($provider_key); ?>">
|
||||||
<?php if (!empty($apis)): ?>
|
<?php if (!empty($apis)): ?>
|
||||||
<div style="margin-bottom: 15px;">
|
<div style="margin-bottom: 15px;">
|
||||||
<strong><?php _e('已配置的 API:', 'argon'); ?></strong>
|
|
||||||
<div style="margin-top: 10px;">
|
<div style="margin-top: 10px;">
|
||||||
<?php foreach ($apis as $index => $api): ?>
|
<?php foreach ($apis as $index => $api): ?>
|
||||||
<div class="argon-api-item" style="padding: 10px; background: #f5f5f5; margin-bottom: 8px; border-radius: 4px;">
|
<div class="argon-api-item" style="padding: 10px; background: #f5f5f5; margin-bottom: 8px; border-radius: 4px;">
|
||||||
@@ -2075,10 +2068,17 @@ function themeoptions_page(){
|
|||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</small>
|
</small>
|
||||||
</span>
|
</span>
|
||||||
<button type="button" class="button button-small argon-edit-api"
|
<button type="button" class="button button-small argon-test-api"
|
||||||
data-provider="<?php echo esc_attr($provider_key); ?>"
|
data-provider="<?php echo esc_attr($provider_key); ?>"
|
||||||
data-index="<?php echo esc_attr($index); ?>"
|
data-index="<?php echo esc_attr($index); ?>"
|
||||||
style="margin-left: 10px;">
|
style="margin-left: 10px;">
|
||||||
|
<span class="dashicons dashicons-yes-alt" style="margin-top: 3px;"></span>
|
||||||
|
<?php _e('测试', 'argon'); ?>
|
||||||
|
</button>
|
||||||
|
<button type="button" class="button button-small argon-edit-api"
|
||||||
|
data-provider="<?php echo esc_attr($provider_key); ?>"
|
||||||
|
data-index="<?php echo esc_attr($index); ?>"
|
||||||
|
style="margin-left: 5px;">
|
||||||
<?php _e('编辑', 'argon'); ?>
|
<?php _e('编辑', 'argon'); ?>
|
||||||
</button>
|
</button>
|
||||||
<button type="button" class="button button-small argon-delete-api"
|
<button type="button" class="button button-small argon-delete-api"
|
||||||
@@ -2188,11 +2188,61 @@ function themeoptions_page(){
|
|||||||
</style>
|
</style>
|
||||||
<script>
|
<script>
|
||||||
jQuery(document).ready(function($) {
|
jQuery(document).ready(function($) {
|
||||||
// 切换服务商时显示/隐藏对应配置
|
// 测试 API 连通性
|
||||||
$('#argon_ai_summary_provider').on('change', function() {
|
$(document).on('click', '.argon-test-api', function() {
|
||||||
var selectedProvider = $(this).val();
|
var btn = $(this);
|
||||||
$('.argon-provider-config').hide();
|
var provider = btn.data('provider');
|
||||||
$('.argon-provider-config[data-provider="' + selectedProvider + '"]').show();
|
var index = btn.data('index');
|
||||||
|
var apiList = $('.argon-api-list[data-provider="' + provider + '"]');
|
||||||
|
var apisData = JSON.parse(apiList.find('.argon-apis-data').val() || '[]');
|
||||||
|
|
||||||
|
if (!apisData[index]) {
|
||||||
|
alert('<?php _e('API 配置不存在', 'argon'); ?>');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var api = apisData[index];
|
||||||
|
var originalHtml = btn.html();
|
||||||
|
|
||||||
|
// 显示加载状态
|
||||||
|
btn.prop('disabled', true);
|
||||||
|
btn.html('<span class="dashicons dashicons-update spin" style="margin-top: 3px;"></span> <?php _e('测试中...', 'argon'); ?>');
|
||||||
|
|
||||||
|
// 发送测试请求
|
||||||
|
$.post(ajaxurl, {
|
||||||
|
action: 'argon_test_api_connection',
|
||||||
|
nonce: '<?php echo wp_create_nonce('argon_test_api_connection'); ?>',
|
||||||
|
provider: provider,
|
||||||
|
api_key: api.api_key,
|
||||||
|
api_endpoint: api.api_endpoint || '',
|
||||||
|
model: api.model || ''
|
||||||
|
}, function(response) {
|
||||||
|
btn.prop('disabled', false);
|
||||||
|
btn.html(originalHtml);
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
btn.html('<span class="dashicons dashicons-yes-alt" style="margin-top: 3px; color: #46b450;"></span> <?php _e('测试', 'argon'); ?>');
|
||||||
|
alert('<?php _e('连接成功!', 'argon'); ?>\n\n' +
|
||||||
|
'<?php _e('响应时间:', 'argon'); ?> ' + response.data.response_time + 'ms\n' +
|
||||||
|
(response.data.model ? '<?php _e('模型:', 'argon'); ?> ' + response.data.model : ''));
|
||||||
|
setTimeout(function() {
|
||||||
|
btn.html(originalHtml);
|
||||||
|
}, 3000);
|
||||||
|
} else {
|
||||||
|
btn.html('<span class="dashicons dashicons-dismiss" style="margin-top: 3px; color: #dc3232;"></span> <?php _e('测试', 'argon'); ?>');
|
||||||
|
alert('<?php _e('连接失败!', 'argon'); ?>\n\n' + (response.data || '<?php _e('未知错误', 'argon'); ?>'));
|
||||||
|
setTimeout(function() {
|
||||||
|
btn.html(originalHtml);
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
|
}).fail(function() {
|
||||||
|
btn.prop('disabled', false);
|
||||||
|
btn.html('<span class="dashicons dashicons-dismiss" style="margin-top: 3px; color: #dc3232;"></span> <?php _e('测试', 'argon'); ?>');
|
||||||
|
alert('<?php _e('网络请求失败', 'argon'); ?>');
|
||||||
|
setTimeout(function() {
|
||||||
|
btn.html(originalHtml);
|
||||||
|
}, 3000);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// 显示/隐藏密码(表单内)
|
// 显示/隐藏密码(表单内)
|
||||||
|
|||||||
Reference in New Issue
Block a user