chore:把 message替换成 query
This commit is contained in:
@@ -19,10 +19,9 @@ export default {
|
|||||||
const params = {
|
const params = {
|
||||||
url: '/api/kefu/chat', // 后端代理接口
|
url: '/api/kefu/chat', // 后端代理接口
|
||||||
data: {
|
data: {
|
||||||
message: message,
|
|
||||||
// conversation_id: options.conversationId ?? new_conversationId,
|
// conversation_id: options.conversationId ?? new_conversationId,
|
||||||
user_id: store.state.memberInfo?.id || 'anonymous',
|
user_id: store.state.memberInfo?.id || 'anonymous',
|
||||||
stream: options.stream || false, // 是否流式响应
|
stream: false,
|
||||||
// Dify API参数
|
// Dify API参数
|
||||||
inputs: {},
|
inputs: {},
|
||||||
query: message,
|
query: message,
|
||||||
@@ -65,126 +64,28 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* WebSocket连接
|
|
||||||
*/
|
|
||||||
connectWebSocket(message, onChunk, onComplete) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const aiConfig = store.getters.globalAIKefuConfig
|
|
||||||
const wsUrl = aiConfig.difyWsUrl
|
|
||||||
|
|
||||||
if (!wsUrl) {
|
|
||||||
reject(new Error('未配置WebSocket地址'))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// #ifdef H5
|
|
||||||
const ws = new WebSocket(wsUrl)
|
|
||||||
|
|
||||||
ws.onopen = () => {
|
|
||||||
// 发送消息
|
|
||||||
ws.send(JSON.stringify({
|
|
||||||
message: message,
|
|
||||||
user_id: store.state.memberInfo?.id || 'anonymous',
|
|
||||||
conversation_id: this.generateConversationId()
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
ws.onmessage = (event) => {
|
|
||||||
try {
|
|
||||||
const data = JSON.parse(event.data)
|
|
||||||
if (data.type === 'chunk' && onChunk) {
|
|
||||||
onChunk(data.content)
|
|
||||||
} else if (data.type === 'complete' && onComplete) {
|
|
||||||
onComplete(data.content)
|
|
||||||
ws.close()
|
|
||||||
resolve(data.content)
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error('WebSocket消息解析失败:', e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ws.onerror = (error) => {
|
|
||||||
console.error('WebSocket连接错误:', error)
|
|
||||||
reject(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
ws.onclose = () => {
|
|
||||||
console.log('WebSocket连接关闭')
|
|
||||||
}
|
|
||||||
// #endif
|
|
||||||
|
|
||||||
// #ifdef MP-WEIXIN || APP-PLUS
|
|
||||||
// 小程序和APP使用uni.connectSocket
|
|
||||||
uni.connectSocket({
|
|
||||||
url: wsUrl,
|
|
||||||
success: () => {
|
|
||||||
uni.onSocketOpen(() => {
|
|
||||||
uni.sendSocketMessage({
|
|
||||||
data: JSON.stringify({
|
|
||||||
message: message,
|
|
||||||
user_id: store.state.memberInfo?.id || 'anonymous',
|
|
||||||
conversation_id: this.generateConversationId()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
uni.onSocketMessage((res) => {
|
|
||||||
try {
|
|
||||||
const data = JSON.parse(res.data)
|
|
||||||
if (data.type === 'chunk' && onChunk) {
|
|
||||||
onChunk(data.content)
|
|
||||||
} else if (data.type === 'complete' && onComplete) {
|
|
||||||
onComplete(data.content)
|
|
||||||
uni.closeSocket()
|
|
||||||
resolve(data.content)
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error('WebSocket消息解析失败:', e)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
uni.onSocketError((error) => {
|
|
||||||
console.error('WebSocket连接错误:', error)
|
|
||||||
reject(error)
|
|
||||||
})
|
|
||||||
},
|
|
||||||
fail: (error) => {
|
|
||||||
reject(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// #endif
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP流式请求
|
* HTTP流式请求
|
||||||
*/
|
*/
|
||||||
async sendHttpStream(message, onChunk, onComplete) {
|
async sendHttpStream(message, onChunk, onComplete) {
|
||||||
const aiConfig = store.getters.globalAIKefuConfig
|
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
url: '/api/kefu/chat-stream',
|
url: '/api/kefu/chat',
|
||||||
data: {
|
data: {
|
||||||
message: message,
|
query: message,
|
||||||
conversation_id: this.generateConversationId(),
|
conversation_id: this.generateConversationId(),
|
||||||
user_id: store.state.memberInfo?.id || 'anonymous',
|
user_id: store.state.memberInfo?.id || 'anonymous',
|
||||||
stream: true
|
stream: true,
|
||||||
|
uniacid: store.state.uniacid, // 保留必填参数
|
||||||
},
|
},
|
||||||
header: {
|
header: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (aiConfig?.difyApiKey) {
|
|
||||||
params.header['Authorization'] = `Bearer ${aiConfig.difyApiKey}`
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用fetch API进行流式请求(H5环境)
|
// 使用fetch API进行流式请求(H5环境)
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/kefu/chat-messages`, {
|
const response = await fetch(`/api/kefu/chat`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|||||||
Reference in New Issue
Block a user