新增聊天窗口打开路由

This commit is contained in:
启航老齐 2026-03-07 19:59:36 +08:00
parent 506ee46439
commit e92196ddae
3 changed files with 132 additions and 85 deletions

View File

@ -72,8 +72,17 @@ public class SseController {
// 使用AiService处理消息传递模型参数
String response = aiService.processMessage(message, model);
log.info("==========AI回复{}",response);
// 将响应消息包装成JSON格式
String jsonResponse = String.format("{\"text\": \"%s\"}", response.replace("\"", "\\\"").replace("\n", "\\n"));
// 检查响应是否已经是JSON格式{开头
String jsonResponse;
if (response.trim().startsWith("{")) {
// 如果是JSON格式直接使用
jsonResponse = response;
} else {
// 否则包装成JSON格式
jsonResponse = String.format("{\"text\": \"%s\"}", response.replace("\"", "\\\"").replace("\n", "\\n"));
}
// 发送JSON格式的消息
emitter.send(SseEmitter.event()
.name("message")

View File

@ -29,6 +29,11 @@ public class AiService {
*/
public String processMessage(String message, String model) {
try {
// 检查是否包含打开页面的指令
if (message.contains("打开店铺管理") || message.contains("进入店铺管理") || message.contains("前往店铺管理")) {
return "{\"action\": \"navigate\", \"route\": \"/shop/shop_list\", \"message\": \"正在跳转到店铺管理页面\"}";
}
// 获取当前日期
String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

View File

@ -222,89 +222,6 @@ export default {
}, 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) {
this.messages = this.messages.filter(msg => !msg.isLoading);
this.isLoading = false;
}
// 使markdown-itmarkdownHTML
const htmlContent = this.md.render(textContent);
this.messages.push({
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
const htmlContent = this.md.render(messageContent);
this.messages.push({
content: htmlContent,
time: this.formatTime(new Date()),
isMe: false,
avatar: ''
});
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) => {
console.log('收到心跳:', event.data);
@ -418,6 +335,122 @@ export default {
this.$router.push(path);
},
processMessageBuffer() {
//
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);
// action
if (jsonData.action) {
//
if (jsonData.action === 'navigate' && jsonData.route) {
//
if (jsonData.message) {
this.messages.push({
content: this.md.render(jsonData.message),
time: this.formatTime(new Date()),
isMe: false,
avatar: ''
});
this.scrollToBottom();
}
//
this.$router.push(jsonData.route);
}
} else {
//
let textContent = jsonData.text || messageContent;
//
if (textContent.includes('订单号:') || textContent.includes('订单详情')) {
//
textContent = this.convertOrderToTable(textContent);
}
//
this.checkOpenPageCommand(textContent);
//
if (this.isLoading) {
this.messages = this.messages.filter(msg => !msg.isLoading);
this.isLoading = false;
}
// 使markdown-itmarkdownHTML
const htmlContent = this.md.render(textContent);
this.messages.push({
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
const htmlContent = this.md.render(messageContent);
this.messages.push({
content: htmlContent,
time: this.formatTime(new Date()),
isMe: false,
avatar: ''
});
this.scrollToBottom();
}
}
//
this.messageBuffer = '';
},
//
checkOpenPageCommand(text) {
//
if (text.includes('打开店铺管理页面') || text.includes('进入店铺管理') || text.includes('前往店铺管理')) {
//
this.$router.push('/shop/shop_list');
}
//
},
// markdown
convertOrderToTable(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;
},
loadSystemStats() {
todayDaily().then(resp => {
this.stats = resp.data;