From dff84a56d8a5d8a759757a9b1c9408f43f5c7705 Mon Sep 17 00:00:00 2001 From: hongjli <3117313295@qq.com> Date: 星期二, 27 五月 2025 14:05:24 +0800 Subject: [PATCH] 聊天页面增加后台获取key接口 --- src/app/chat/page.tsx | 69 +++++++++++++++++++++++++++++----- 1 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/app/chat/page.tsx b/src/app/chat/page.tsx index 970b3a3..8fd3231 100644 --- a/src/app/chat/page.tsx +++ b/src/app/chat/page.tsx @@ -487,6 +487,9 @@ // 鍦ㄧ粍浠堕《閮ㄦ坊鍔犳樉绀�/闅愯棌瀵嗙爜鐨勭姸鎬� const [showApiKey, setShowApiKey] = useState(false); + // 娣诲姞API瀵嗛挜鍔犺浇鐘舵�� + const [isLoadingApiKey, setIsLoadingApiKey] = useState(false); + const messagesEndRef = useRef<HTMLDivElement>(null); const errorTimeoutRef = useRef<any>(null); @@ -496,6 +499,44 @@ // 鍒嗙娑堟伅杈撳叆鐘舵�侊紝閬垮厤瑙﹀彂涓嶅繀瑕佺殑閲嶆覆鏌� const messageInputRef = useRef<HTMLTextAreaElement>(null); const [localMessage, setLocalMessage] = useState(''); + + // 娣诲姞鑾峰彇API瀵嗛挜鐨勫嚱鏁� + const fetchApiKey = async () => { + setIsLoadingApiKey(true); + try { + const response = await fetch('http://121.43.139.99:8080/api/secret-key', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error(`鑾峰彇瀵嗛挜澶辫触: HTTP ${response.status}`); + } + + const result = await response.json(); + console.log('API鍝嶅簲:', result); + + // 鎸夌収鎺ュ彛鏂囨。鏍煎紡瑙f瀽鍝嶅簲 + if (result.code === 200 && result.data && result.data.key) { + const apiKeyValue = result.data.key; + setApiKey(apiKeyValue); + setError(null); + console.log('鎴愬姛鑾峰彇API瀵嗛挜:', apiKeyValue); + return apiKeyValue; + } else { + throw new Error(result.message || '鑾峰彇瀵嗛挜澶辫触锛氬搷搴旀牸寮忎笉姝g‘'); + } + } catch (err) { + console.error('鑾峰彇API瀵嗛挜澶辫触:', err); + const errorMessage = err instanceof Error ? err.message : '鑾峰彇瀵嗛挜鏃跺彂鐢熸湭鐭ラ敊璇�'; + showErrorMessage(`瀵嗛挜鑾峰彇澶辫触: ${errorMessage}`); + return null; + } finally { + setIsLoadingApiKey(false); + } + }; const handleMessageChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { // 鍙洿鏂版湰鍦扮姸鎬侊紝涓嶈Е鍙戝叏灞�閲嶆覆鏌� @@ -528,13 +569,12 @@ }, [message]); useEffect(() => { - // 鑾峰彇瀛樺偍鐨凙PI Key - const storedApiKey = localStorage.getItem('api-key'); - if (storedApiKey) { - setApiKey(storedApiKey); - } else { - setShowApiKeyInput(true); - } + // 缁勪欢鍔犺浇鏃朵粠API鑾峰彇瀵嗛挜 + const initializeApiKey = async () => { + await fetchApiKey(); + }; + + initializeApiKey(); }, []); useEffect(() => { @@ -604,9 +644,16 @@ // 浠巜indow瀵硅薄鑾峰彇娑堟伅鍐呭 const inputMessage = (window as any).messageToSend || ''; if (!inputMessage.trim() || !isMessageComplete) return; - if (!apiKey) { - showErrorMessage('璇峰厛璁剧疆API Key'); - return; + + // 妫�鏌PI瀵嗛挜锛屽鏋滄病鏈夊垯灏濊瘯鑾峰彇 + let currentApiKey = apiKey; + if (!currentApiKey) { + showErrorMessage('姝e湪鑾峰彇API瀵嗛挜...'); + currentApiKey = await fetchApiKey(); + if (!currentApiKey) { + showErrorMessage('鏃犳硶鑾峰彇API瀵嗛挜锛岃妫�鏌ョ綉缁滆繛鎺�'); + return; + } } setIsStreaming(true); @@ -652,7 +699,7 @@ const response = await fetch(`${BASE_URL}/v1/chat-messages`, { method: 'POST', headers: { - 'Authorization': `Bearer ${apiKey}`, + 'Authorization': `Bearer ${currentApiKey}`, 'Content-Type': 'application/json', }, mode: 'cors', -- Gitblit v1.9.3