/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React from 'react'; import { useTranslation } from 'react-i18next'; import { Card, Button, Typography, Tag } from '@douyinfe/semi-ui'; import { copy, showSuccess } from '../../../helpers'; /** * 解析密钥数据,支持多种格式 * @param {string} keyData - 密钥数据 * @param {Function} t - 翻译函数 * @returns {Array} 解析后的密钥数组 */ const parseChannelKeys = (keyData, t) => { if (!keyData) return []; const trimmed = keyData.trim(); // 检查是否是JSON数组格式(如Vertex AI) if (trimmed.startsWith('[')) { try { const parsed = JSON.parse(trimmed); if (Array.isArray(parsed)) { return parsed.map((item, index) => ({ id: index, content: typeof item === 'string' ? item : JSON.stringify(item, null, 2), type: typeof item === 'string' ? 'text' : 'json', label: `${t('密钥')} ${index + 1}`, })); } } catch (e) { // 如果解析失败,按普通文本处理 console.warn('Failed to parse JSON keys:', e); } } // 检查是否是多行密钥(按换行符分割) const lines = trimmed.split('\n').filter((line) => line.trim()); if (lines.length > 1) { return lines.map((line, index) => ({ id: index, content: line.trim(), type: 'text', label: `${t('密钥')} ${index + 1}`, })); } // 单个密钥 return [ { id: 0, content: trimmed, type: trimmed.startsWith('{') ? 'json' : 'text', label: t('密钥'), }, ]; }; /** * 可复用的密钥显示组件 * @param {Object} props * @param {string} props.keyData - 密钥数据 * @param {boolean} props.showSuccessIcon - 是否显示成功图标 * @param {string} props.successText - 成功文本 * @param {boolean} props.showWarning - 是否显示安全警告 * @param {string} props.warningText - 警告文本 */ const ChannelKeyDisplay = ({ keyData, showSuccessIcon = true, successText, showWarning = true, warningText, }) => { const { t } = useTranslation(); const parsedKeys = parseChannelKeys(keyData, t); const isMultipleKeys = parsedKeys.length > 1; const handleCopyAll = () => { copy(keyData); showSuccess(t('所有密钥已复制到剪贴板')); }; const handleCopyKey = (content) => { copy(content); showSuccess(t('密钥已复制到剪贴板')); }; return (
{/* 成功状态 */} {showSuccessIcon && (
{successText || t('验证成功')}
)} {/* 密钥内容 */}
{isMultipleKeys ? t('渠道密钥列表') : t('渠道密钥')} {isMultipleKeys && (
{t('共 {{count}} 个密钥', { count: parsedKeys.length })}
)}
{parsedKeys.map((keyItem) => (
{keyItem.label}
{keyItem.type === 'json' && ( {t('JSON')} )}
{keyItem.content}
{keyItem.type === 'json' && ( {t('JSON格式密钥,请确保格式正确')} )}
))}
{isMultipleKeys && (
{t( '检测到多个密钥,您可以单独复制每个密钥,或点击复制全部获取完整内容。', )}
)}
{/* 安全警告 */} {showWarning && (
{t('安全提醒')} {warningText || t( '请妥善保管密钥信息,不要泄露给他人。如有安全疑虑,请及时更换密钥。', )}
)}
); }; export default ChannelKeyDisplay;