chore:客服回答不重复

This commit is contained in:
2026-04-07 15:10:26 +08:00
parent e018f9a8b9
commit cef2b6e02e
3 changed files with 63 additions and 84 deletions

View File

@@ -46,6 +46,6 @@ const localDevConfig = ({
uniacid: 2847,
domain: 'https://xcx6.aigc-quickapp.com/',
},
})['2847']; // 选择要使用的环境配置
})['1']; // 选择要使用的环境配置
export default localDevConfig;

View File

@@ -110,20 +110,20 @@ export default {
}
// 处理流式消息块
else if (data.type === 'message' || data.event === 'message') {
else if (data.event === 'message') {
const text = data.answer || data.content || data.text || '';
content += text;
if (onChunk) onChunk(text);
}
// 处理流结束
else if (data.event === 'message_end' || data.type === 'message_end') {
else if (data.event === 'message_end') {
conversationId = data.conversation_id || '';
if (conversationId) {
this.setConversationId(conversationId);
}
if (onComplete) {
onComplete({ content, conversation_id: conversationId });
onComplete({ content, conversation_id: conversationId });
}
resolve({ content, conversation_id: conversationId });
socketTask.close();

View File

@@ -418,50 +418,22 @@ export default {
isFetchingHistory: false
}
},
onShow() {
// 如果不是 AI 客服,立即退出并跳转
if (customerServiceType !== 'ai') {
uni.showToast({ title: '当前客服类型不支持此页面', icon: 'none' });
setTimeout(() => {
uni.navigateBack({ delta: 1 }); // 或 redirectTo 首页
}, 1000);
return; // ⚠️ 关键:阻止后续初始化
}
// 优先读取本地缓存的会话 ID
const localConvId = this.getConversationIdFromLocal();
if (localConvId) {
this.currentConversationId = localConvId;
aiService.setConversationId(localConvId);
} else {
this.currentConversationId = aiService.getConversationId();
}
// 加载初始历史后,同步historyOffset
this.loadChatHistoryIfExist().then(() => {
// 初始历史条数作为偏移量
this.historyOffset = this.messages.length;
});
// 移除:this.historyOffset = 0;(避免覆盖初始历史的偏移量)
},
// 新增:组件销毁时清除防抖定时器
beforeDestroy() {
if (this.scrollTimer) clearTimeout(this.scrollTimer);
},
created() {
this.messages = [...this.initialMessages]
this.messageId = this.messages.length
this.initAudioContext()
// 初始化用户头像(缓存优先)
this.initUserAvatarData()
// 初始化用户昵称(缓存优先)
this.initUserNicknameData()
this.currentConversationId = aiService.getConversationId();
},
mounted() {
// 初始加载时滚底(仅一次)
this.$nextTick(() => {
this.scrollToBottom()
})
},
created() {
this.messages = [...this.initialMessages]
this.messageId = this.messages.length
this.initAudioContext()
// 初始化用户头像(缓存优先)
this.initUserAvatarData()
// 初始化用户昵称(缓存优先)
this.initUserNicknameData()
// 👇 关键:在这里加载历史记录,只执行一次
this.loadChatHistoryIfExist().then(() => {
this.historyOffset = this.messages.length;
});
this.currentConversationId = aiService.getConversationId();
},
watch: {
messages: {
handler() {
@@ -530,42 +502,49 @@ export default {
}
},
// 👇 新增:加载历史记录
async loadChatHistoryIfExist() {
const convId = aiService.getConversationId();
if (convId) {
try {
const history = await aiService.getChatHistory();
if (!history || !Array.isArray(history.messages)) {
console.warn('历史记录为空或格式无效');
return;
}
this.messages = history.messages.map(msg => {
let content = '';
if (msg.role === 'user') {
content = msg.query || (typeof msg.inputs === 'string' ? msg.inputs : JSON.stringify(msg.inputs || '')) || '';
} else {
content = msg.answer || '';
}
return {
id: msg.message_id || (Date.now() + Math.random() * 10000),
role: msg.role === 'user' ? 'user' : 'assistant',
content: content,
timestamp: msg.created_at,
actions: msg.role !== 'user' ? [
{ id: 1, text: '有帮助', type: 'like' },
{ id: 2, text: '没帮助', type: 'dislike' }
] : []
};
});
} catch (error) {
console.error('加载历史记录失败:', error);
aiService.clearConversationId();
this.clearConversationIdFromLocal();
this.currentConversationId = null;
}
}
},
// 👇 修正:加载历史记录
async loadChatHistoryIfExist() {
const convId = aiService.getConversationId();
if (convId) {
try {
// ✅ 调用正确的方法,并传入会话ID
const response = await aiService.getConversationHistory({
conversation_id: convId
});
if (!response.success || !Array.isArray(response.messages)) {
console.warn('历史记录为空或格式无效');
return;
}
// 注意:getConversationHistory 返回的数据结构与 getChatHistory 预期的不同
// 需要根据实际返回的 response.data.messages 来映射
this.messages = response.messages.map(msg => {
let content = '';
if (msg.role === 'user') {
content = msg.query || (typeof msg.inputs === 'string' ? msg.inputs : JSON.stringify(msg.inputs || '')) || '';
} else {
content = msg.answer || '';
}
return {
id: msg.message_id || (Date.now() + Math.random() * 10000),
role: msg.role === 'user' ? 'user' : 'assistant',
content: content,
timestamp: msg.created_at * 1000, // 后端时间戳通常是秒,前端需要毫秒
actions: msg.role !== 'user' ? [
{ id: 1, text: '有帮助', type: 'like' },
{ id: 2, text: '没帮助', type: 'dislike' }
] : []
};
});
} catch (error) {
console.error('加载历史记录失败:', error);
// 只需调用服务层的清除方法,它会处理本地存储
aiService.clearConversationId();
this.currentConversationId = null;
}
}
},
// 新增:初始化用户昵称(支持缓存持久化)
initUserNicknameData() {
// 1. 优先读取本地缓存的昵称