🐛Fix: 修复组件不及时更新的问题
This commit is contained in:
@@ -99,7 +99,6 @@ const getPlatformName = (platformId) => {
|
||||
}
|
||||
|
||||
// 发送消息
|
||||
// TODO 需要将用户配置(端点、密钥)和用户消息发送给后端统一处理
|
||||
const sendMessage = async () => {
|
||||
if (!canSend.value) return
|
||||
|
||||
@@ -126,12 +125,8 @@ const sendMessage = async () => {
|
||||
throw new Error('未找到厂商配置')
|
||||
}
|
||||
|
||||
// TODO 这里无需解密,应该交由后端解密
|
||||
// 解密密钥
|
||||
const apiKey = decrypt(config.apiKey)
|
||||
|
||||
// 调用API
|
||||
const result = await callAIPlatform(selectedPlatform.value, apiKey, config.endpoint, userInput.value)
|
||||
// 将配置信息和用户消息发送给后端处理
|
||||
const result = await callAIPlatform(selectedPlatform.value, config.apiKey, config.endpoint, userInput.value)
|
||||
|
||||
// 显示响应
|
||||
responseMessage.value = result
|
||||
@@ -144,14 +139,50 @@ const sendMessage = async () => {
|
||||
}
|
||||
|
||||
// 调用AI平台API
|
||||
// TODO 此处由后端统一处理
|
||||
// TODO 这里需要调用实际后端API接口
|
||||
const callAIPlatform = async (platformId, apiKey, endpoint, message) => {
|
||||
// TODO 待完善
|
||||
try {
|
||||
// 发送请求到后端API
|
||||
const response = await fetch('/api/chat', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
platformId,
|
||||
apiKey,
|
||||
endpoint,
|
||||
message
|
||||
})
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return data.response
|
||||
} catch (error) {
|
||||
console.error('调用AI平台API失败:', error)
|
||||
throw new Error('调用AI平台API失败: ' + error.message)
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化
|
||||
onMounted(() => {
|
||||
loadConfiguredPlatforms()
|
||||
|
||||
// 监听配置保存事件,以便刷新数据
|
||||
const handleConfigSaved = () => {
|
||||
loadConfiguredPlatforms()
|
||||
}
|
||||
|
||||
window.addEventListener('config-saved', handleConfigSaved)
|
||||
|
||||
// 组件卸载时移除事件监听器
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('config-saved', handleConfigSaved)
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -183,6 +183,12 @@ const saveConfig = () => {
|
||||
resetForm()
|
||||
|
||||
showValidation('配置保存成功', 'success')
|
||||
|
||||
// TODO: 保存后应该使得 ChatPanel 组件刷新,使得能够选择厂商
|
||||
// 触发全局事件,通知 ChatPanel 刷新配置
|
||||
if (typeof window !== 'undefined') {
|
||||
window.dispatchEvent(new CustomEvent('config-saved', { detail: { platformId: selectedPlatform.value } }))
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存配置失败:', error)
|
||||
showValidation('保存配置失败,请重试', 'error')
|
||||
|
||||
Reference in New Issue
Block a user