chore:ai智能客服可以正常对话

This commit is contained in:
2025-12-08 17:16:10 +08:00
parent 4cf19e417f
commit 7dbf455341
6 changed files with 55 additions and 63 deletions

View File

@@ -11,14 +11,16 @@ export default {
async sendMessage(message, options = {}) {
try {
// 获取AI配置
const aiConfig = store.getters.globalAIAgentConfig
const aiConfig = store.getters.globalAIKefuConfig
// const new_conversationId = await this.generateConversationId()
// 构建Dify API请求参数
const params = {
url: '/api/ai/chat', // 后端代理接口
url: '/api/kefu/chat', // 后端代理接口
data: {
message: message,
conversation_id: options.conversationId || this.generateConversationId(),
// conversation_id: options.conversationId ?? new_conversationId,
user_id: store.state.memberInfo?.id || 'anonymous',
stream: options.stream || false, // 是否流式响应
// Dify API参数
@@ -32,15 +34,6 @@ export default {
}
}
// 如果有Dify配置添加API密钥
if (aiConfig?.difyApiKey) {
params.header['Authorization'] = `Bearer ${aiConfig.difyApiKey}`
}
if (aiConfig?.difyBaseUrl) {
params.header['X-Dify-Url'] = aiConfig.difyBaseUrl
}
// 发送请求
const response = await http.sendRequest({
...params,
@@ -63,20 +56,8 @@ export default {
*/
async sendStreamMessage(message, onChunk, onComplete) {
try {
const aiConfig = store.getters.globalAIAgentConfig
// 检查配置
if (!aiConfig?.difyBaseUrl || !aiConfig?.difyApiKey) {
throw new Error('未配置Dify服务')
}
// 创建WebSocket连接或使用Server-Sent Events
if (aiConfig?.difyWsUrl) {
return this.connectWebSocket(message, onChunk, onComplete)
} else {
// 使用HTTP流式请求
return this.sendHttpStream(message, onChunk, onComplete)
}
} catch (error) {
console.error('流式消息发送失败:', error)
@@ -89,7 +70,7 @@ export default {
*/
connectWebSocket(message, onChunk, onComplete) {
return new Promise((resolve, reject) => {
const aiConfig = store.getters.globalAIAgentConfig
const aiConfig = store.getters.globalAIKefuConfig
const wsUrl = aiConfig.difyWsUrl
if (!wsUrl) {
@@ -181,10 +162,10 @@ export default {
* HTTP流式请求
*/
async sendHttpStream(message, onChunk, onComplete) {
const aiConfig = store.getters.globalAIAgentConfig
const aiConfig = store.getters.globalAIKefuConfig
const params = {
url: '/api/ai/chat-stream',
url: '/api/kefu/chat-stream',
data: {
message: message,
conversation_id: this.generateConversationId(),
@@ -203,11 +184,10 @@ export default {
// 使用fetch API进行流式请求H5环境
// #ifdef H5
try {
const response = await fetch(`${aiConfig.difyBaseUrl}/chat-messages`, {
const response = await fetch(`/api/kefu/chat-messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${aiConfig.difyApiKey}`
},
body: JSON.stringify({
inputs: {},
@@ -286,7 +266,7 @@ export default {
if (response.code === 0 || response.success) {
return {
success: true,
content: response.data?.answer || response.data?.content || response.data,
content: response.data?.answer || response.data?.content || response.data?.reply|| response.data,
conversationId: response.data?.conversation_id,
messageId: response.data?.message_id
}
@@ -298,8 +278,27 @@ export default {
/**
* 生成会话ID
*/
generateConversationId() {
return 'conv_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
async generateConversationId() {
// 构建Dify API请求参数
const params = {
url: '/api/kefu/createConversation', // 后端代理接口
data: {
uniacid: store.state.uniacid,
user_id: store.state.memberInfo?.id || 'anonymous',
member_id: store.state.memberInfo?.id || '',
},
header: {
'Content-Type': 'application/json'
}
}
// 发送请求
const response = await http.sendRequest({
...params,
async: false // 使用Promise方式
})
return this.handleResponse(response, options)
},
/**
@@ -307,24 +306,17 @@ export default {
*/
async getServiceStatus() {
try {
const aiConfig = store.getters.globalAIAgentConfig
if (!aiConfig?.difyBaseUrl || !aiConfig?.difyApiKey) {
return {
available: false,
reason: '未配置Dify服务'
}
}
// 简单的健康检查
const response = await http.sendRequest({
url: '/api/ai/health',
url: '/api/kefu/health',
async: false
})
const available = response?.data?.status === 'healthy';
return {
available: response.success,
reason: response.success ? '服务正常' : '服务异常'
available,
reason: available ? '服务正常' : '服务异常'
}
} catch (error) {
@@ -341,7 +333,7 @@ export default {
async clearConversation(conversationId) {
try {
const response = await http.sendRequest({
url: '/api/ai/clear-conversation',
url: '/api/kefu/clear-conversation',
data: { conversation_id: conversationId },
async: false
})

View File

@@ -14,18 +14,18 @@ try {
// 调试版本,配置说明
const devCfg = {
// 商户ID
uniacid: 460, //825
uniacid: 1, //825
//api请求地址
baseUrl: 'https://xcx30.5g-quickapp.com/',
baseUrl: 'https://dev.aigc-quickapp.com/',
// baseUrl: 'http://localhost:8010/',
// 图片域名
imgDomain: 'https://xcx30.5g-quickapp.com/',
imgDomain: 'https://dev.aigc-quickapp.com/',
//imgDomain: 'http://localhost:8010/',
// H5端域名
h5Domain: 'https://xcx30.5g-quickapp.com/',
h5Domain: 'https://dev.aigc-quickapp.com/',
// h5Domain: 'http://localhost:8010/',
// // api请求地址