优化订单输出信息

This commit is contained in:
启航老齐 2026-03-07 19:22:48 +08:00
parent 3737874a46
commit 506ee46439
2 changed files with 103 additions and 27 deletions

View File

@ -72,15 +72,12 @@ public class SseController {
// 使用AiService处理消息传递模型参数 // 使用AiService处理消息传递模型参数
String response = aiService.processMessage(message, model); String response = aiService.processMessage(message, model);
log.info("==========AI回复{}",response); log.info("==========AI回复{}",response);
// 确保SSE消息格式正确处理多行消息 // 将响应消息包装成JSON格式
String[] lines = response.split("\n"); String jsonResponse = String.format("{\"text\": \"%s\"}", response.replace("\"", "\\\"").replace("\n", "\\n"));
StringBuilder formattedResponse = new StringBuilder(); // 发送JSON格式的消息
for (String line : lines) { emitter.send(SseEmitter.event()
formattedResponse.append("data:").append(line).append("\n"); .name("message")
} .data(jsonResponse));
formattedResponse.append("\n"); // 结束消息
emitter.send(formattedResponse.toString());
log.info("发送给前端的消息: {}", response); log.info("发送给前端的消息: {}", response);
return "消息发送成功"; return "消息发送成功";

View File

@ -140,6 +140,8 @@ export default {
return { return {
md: new MarkdownIt(), md: new MarkdownIt(),
inputMessage: '', inputMessage: '',
messageBuffer: '',
messageTimeout: null,
messages: [ messages: [
{ {
content: '您好!我是您的工作助手,有什么可以帮助您的吗?', content: '您好!我是您的工作助手,有什么可以帮助您的吗?',
@ -205,16 +207,70 @@ export default {
// //
this.sse.addEventListener('message', (event) => { this.sse.addEventListener('message', (event) => {
console.log('收到SSE消息:', event.data); console.log('收到SSE消息:', event.data);
//
if (this.messageTimeout) {
clearTimeout(this.messageTimeout);
}
//
this.messageBuffer += event.data + '\n';
// 1
this.messageTimeout = setTimeout(() => {
this.processMessageBuffer();
}, 1000);
});
//
this.processMessageBuffer = function() {
//
if (this.messageTimeout) {
clearTimeout(this.messageTimeout);
this.messageTimeout = null;
}
// data:
let messageContent = this.messageBuffer.replace(/^data:/gm, '');
//
messageContent = messageContent.trim();
//
if (messageContent) {
try {
// JSON
const jsonData = JSON.parse(messageContent);
let textContent = jsonData.text || messageContent;
//
if (textContent.includes('订单号:') || textContent.includes('订单详情')) {
//
textContent = this.convertOrderToTable(textContent);
}
// //
if (this.isLoading) { if (this.isLoading) {
this.messages = this.messages.filter(msg => !msg.isLoading); this.messages = this.messages.filter(msg => !msg.isLoading);
this.isLoading = false; this.isLoading = false;
} }
//
// // 使markdown-itmarkdownHTML
let messageContent = event.data; const htmlContent = this.md.render(textContent);
// data: this.messages.push({
messageContent = messageContent.replace(/^data:/g, ''); content: htmlContent,
time: this.formatTime(new Date()),
isMe: false,
avatar: ''
});
this.scrollToBottom();
} catch (e) {
console.error('解析SSE消息失败:', e);
//
if (this.isLoading) {
this.messages = this.messages.filter(msg => !msg.isLoading);
this.isLoading = false;
}
// 使markdown-itmarkdownHTML // 使markdown-itmarkdownHTML
const htmlContent = this.md.render(messageContent); const htmlContent = this.md.render(messageContent);
this.messages.push({ this.messages.push({
@ -224,7 +280,30 @@ export default {
avatar: '' avatar: ''
}); });
this.scrollToBottom(); this.scrollToBottom();
}); }
}
//
this.messageBuffer = '';
};
// markdown
this.convertOrderToTable = function(text) {
//
const orderMatches = text.match(/订单号:\s*(\S+)/);
const dateMatches = text.match(/日期:\s*(\S+)/);
const customerMatches = text.match(/客户:\s*(\S+)/);
const amountMatches = text.match(/金额:\s*(\S+)/);
const statusMatches = text.match(/状态:\s*(\S+)/);
if (orderMatches && dateMatches && customerMatches && amountMatches && statusMatches) {
// markdown
return `| 订单号 | 日期 | 客户 | 金额 | 状态 |
| --- | --- | --- | --- | --- |
| ${orderMatches[1]} | ${dateMatches[1]} | ${customerMatches[1]} | ${amountMatches[1]} | ${statusMatches[1]} |`;
}
return text;
};
// //
this.sse.addEventListener('heartbeat', (event) => { this.sse.addEventListener('heartbeat', (event) => {