🐛Fix: 修复组件不及时更新的问题
This commit is contained in:
@@ -99,7 +99,6 @@ const getPlatformName = (platformId) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 发送消息
|
// 发送消息
|
||||||
// TODO 需要将用户配置(端点、密钥)和用户消息发送给后端统一处理
|
|
||||||
const sendMessage = async () => {
|
const sendMessage = async () => {
|
||||||
if (!canSend.value) return
|
if (!canSend.value) return
|
||||||
|
|
||||||
@@ -126,12 +125,8 @@ const sendMessage = async () => {
|
|||||||
throw new Error('未找到厂商配置')
|
throw new Error('未找到厂商配置')
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 这里无需解密,应该交由后端解密
|
// 将配置信息和用户消息发送给后端处理
|
||||||
// 解密密钥
|
const result = await callAIPlatform(selectedPlatform.value, config.apiKey, config.endpoint, userInput.value)
|
||||||
const apiKey = decrypt(config.apiKey)
|
|
||||||
|
|
||||||
// 调用API
|
|
||||||
const result = await callAIPlatform(selectedPlatform.value, apiKey, config.endpoint, userInput.value)
|
|
||||||
|
|
||||||
// 显示响应
|
// 显示响应
|
||||||
responseMessage.value = result
|
responseMessage.value = result
|
||||||
@@ -144,14 +139,50 @@ const sendMessage = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 调用AI平台API
|
// 调用AI平台API
|
||||||
// TODO 此处由后端统一处理
|
// TODO 这里需要调用实际后端API接口
|
||||||
const callAIPlatform = async (platformId, apiKey, endpoint, message) => {
|
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(() => {
|
onMounted(() => {
|
||||||
loadConfiguredPlatforms()
|
loadConfiguredPlatforms()
|
||||||
|
|
||||||
|
// 监听配置保存事件,以便刷新数据
|
||||||
|
const handleConfigSaved = () => {
|
||||||
|
loadConfiguredPlatforms()
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('config-saved', handleConfigSaved)
|
||||||
|
|
||||||
|
// 组件卸载时移除事件监听器
|
||||||
|
onUnmounted(() => {
|
||||||
|
window.removeEventListener('config-saved', handleConfigSaved)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -248,4 +279,4 @@ onMounted(() => {
|
|||||||
.info-message p {
|
.info-message p {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -183,6 +183,12 @@ const saveConfig = () => {
|
|||||||
resetForm()
|
resetForm()
|
||||||
|
|
||||||
showValidation('配置保存成功', 'success')
|
showValidation('配置保存成功', 'success')
|
||||||
|
|
||||||
|
// TODO: 保存后应该使得 ChatPanel 组件刷新,使得能够选择厂商
|
||||||
|
// 触发全局事件,通知 ChatPanel 刷新配置
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
window.dispatchEvent(new CustomEvent('config-saved', { detail: { platformId: selectedPlatform.value } }))
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('保存配置失败:', error)
|
console.error('保存配置失败:', error)
|
||||||
showValidation('保存配置失败,请重试', 'error')
|
showValidation('保存配置失败,请重试', 'error')
|
||||||
@@ -362,4 +368,4 @@ onMounted(() => {
|
|||||||
.remove-btn:hover {
|
.remove-btn:hover {
|
||||||
background-color: #c82333;
|
background-color: #c82333;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user