修改api显示逻辑

This commit is contained in:
小贺
2025-09-06 12:35:13 +08:00
parent c1a493222c
commit 557aef7a7f

View File

@@ -1,5 +1,5 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { CheckCircle2, XCircle, Loader2, Trash2, RefreshCw } from 'lucide-react'; import { CheckCircle2, XCircle, Loader2, Trash2, RefreshCw, Copy, Eye, EyeOff } from 'lucide-react';
import { apiKeyService } from '../services/api'; import { apiKeyService } from '../services/api';
import type { ApiKey } from '../types/api'; import type { ApiKey } from '../types/api';
@@ -12,6 +12,8 @@ interface ApiKeyCardProps {
export function ApiKeyCard({ apiKey, onUpdate, onDelete }: ApiKeyCardProps) { export function ApiKeyCard({ apiKey, onUpdate, onDelete }: ApiKeyCardProps) {
const [isValidating, setIsValidating] = useState(false); const [isValidating, setIsValidating] = useState(false);
const [isDeleting, setIsDeleting] = useState(false); const [isDeleting, setIsDeleting] = useState(false);
const [showFullKey, setShowFullKey] = useState(false);
const [copied, setCopied] = useState(false);
const handleValidate = async () => { const handleValidate = async () => {
setIsValidating(true); setIsValidating(true);
@@ -41,6 +43,16 @@ export function ApiKeyCard({ apiKey, onUpdate, onDelete }: ApiKeyCardProps) {
} }
}; };
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(apiKey.key);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch (error) {
console.error('复制失败:', error);
}
};
const formatCurrency = (amount: number) => { const formatCurrency = (amount: number) => {
return new Intl.NumberFormat('zh-CN', { return new Intl.NumberFormat('zh-CN', {
style: 'currency', style: 'currency',
@@ -54,6 +66,14 @@ export function ApiKeyCard({ apiKey, onUpdate, onDelete }: ApiKeyCardProps) {
return new Date(dateString).toLocaleString('zh-CN'); return new Date(dateString).toLocaleString('zh-CN');
}; };
const maskApiKey = (key: string) => {
if (key.length <= 8) return key; // 如果很短就直接显示
const start = key.slice(0, 4);
const end = key.slice(-4);
const masked = '*'.repeat(Math.max(0, key.length - 8));
return showFullKey ? key : `${start}${masked}${end}`;
};
return ( return (
<div className="bg-white border border-gray-200 rounded-lg shadow-sm hover:shadow-md transition-shadow"> <div className="bg-white border border-gray-200 rounded-lg shadow-sm hover:shadow-md transition-shadow">
<div className="p-6"> <div className="p-6">
@@ -69,11 +89,25 @@ export function ApiKeyCard({ apiKey, onUpdate, onDelete }: ApiKeyCardProps) {
)} )}
</div> </div>
<div className="flex items-center ml-4"> <div className="flex items-center ml-4 space-x-1">
<button
onClick={handleCopy}
className="p-2 text-blue-600 hover:bg-blue-50 rounded-md transition-colors"
title="复制API Key"
>
<Copy className={`w-4 h-4 ${copied ? 'text-green-600' : ''}`} />
</button>
<button
onClick={() => setShowFullKey(!showFullKey)}
className="p-2 text-blue-600 hover:bg-blue-50 rounded-md transition-colors"
title={showFullKey ? "隐藏" : "显示"}
>
{showFullKey ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
</button>
<button <button
onClick={handleValidate} onClick={handleValidate}
disabled={isValidating} disabled={isValidating}
className="p-2 mr-2 text-blue-600 hover:bg-blue-50 rounded-md disabled:opacity-50" className="p-2 text-blue-600 hover:bg-blue-50 rounded-md disabled:opacity-50"
title="验证API Key" title="验证API Key"
> >
<RefreshCw className={`w-4 h-4 ${isValidating ? 'animate-spin' : ''}`} /> <RefreshCw className={`w-4 h-4 ${isValidating ? 'animate-spin' : ''}`} />
@@ -101,21 +135,26 @@ export function ApiKeyCard({ apiKey, onUpdate, onDelete }: ApiKeyCardProps) {
</span> </span>
)} )}
{copied && (
<span className="ml-2 text-xs text-green-600"> </span>
)}
</div> </div>
{/* 直接显示完整的API Key */} {/* API Key 显示区域 */}
<div className="mb-3"> <div className="mb-4">
<label className="block text-sm font-medium text-gray-700 mb-1">API Key:</label> <label className="block text-sm font-medium text-gray-700 mb-1">API Key:</label>
<div className="p-2 bg-gray-50 border border-gray-200 rounded-md text-xs font-mono text-gray-600 break-all"> <div className="flex items-center space-x-2">
{apiKey.key} <div className="flex-1 p-2 bg-gray-50 border border-gray-200 rounded-md text-xs font-mono text-gray-600 break-all cursor-text select-all">
{maskApiKey(apiKey.key)}
</div>
</div> </div>
</div> </div>
{apiKey.error ? ( {apiKey.error && (
<div className="mb-3 p-2 bg-red-50 border border-red-200 rounded-md"> <div className="mb-3 p-2 bg-red-50 border border-red-200 rounded-md">
<p className="text-sm text-red-600">{apiKey.error}</p> <p className="text-sm text-red-600">{apiKey.error}</p>
</div> </div>
) : null} )}
{apiKey.balance !== undefined && ( {apiKey.balance !== undefined && (
<div className="mb-3 p-3 bg-gray-50 rounded-md"> <div className="mb-3 p-3 bg-gray-50 rounded-md">