This commit is contained in:
liuyuan
2025-05-23 17:23:44 +08:00
parent 060344610a
commit 56ee2fee9e
16 changed files with 257 additions and 118 deletions

View File

@@ -60,7 +60,7 @@
</button>
</view>
<view class="message_wrap" :style="{ top: 82 + 'px' }" v-if="showMessages">
<view class="message_wrap" :style="{ top: 80 + 'px' }" v-if="showMessages">
<text class="message_title" v-if="tishi">好的结合您的情况下面是分析结果</text>
<!-- 显示聊天记录 -->
<view class="message-container-block" ref="messageContainerBlock">
@@ -102,6 +102,7 @@
<script>
import $http from "@/config/requestConfig.js";
import { mapState, mapMutations } from "vuex";
import qs from 'qs'
export default {
data() {
return {
@@ -135,9 +136,6 @@ export default {
},
computed: {
...mapState(["userInfo"]),
},
mounted() {
},
onLoad() {
uni.hideTabBar();
@@ -146,9 +144,13 @@ export default {
const systemInfo = uni.getSystemInfoSync();
this.containerHeight = systemInfo.windowHeight; //获取设备的窗口高度
this.getChatAssistants();
//重置
this.$nextTick(() => {
this.showMode();
});
},
onShow(){
this.showMessages = false;
console.log('进入到onShow方法')
this.activeRecord = null;
this.getRecordsData();
@@ -157,6 +159,11 @@ export default {
let index = uni.getStorageSync('homeParams').index;
if(data){
this.clickRecord(data, index);
}else{
uni.removeStorageSync('homeParams');
this.$nextTick(() => {
this.showMessages = false;
});
}
},
methods: {
@@ -174,8 +181,8 @@ export default {
},
})
.then(res=> {
uni.hideLoading();
if (res.list&&res.list.length>0) {
uni.hideLoading();
res.list = res.list.filter(item => {
return !item.name.includes('心理助手') && !item.name.includes('太湖云翳');
});
@@ -204,11 +211,13 @@ export default {
}
})
.catch(e=>{
uni.hideLoading();
uni.setStorageSync("guidePages", 2);
});
},
//获取对话记录数据
getRecordsData() {
this.record_list = [];
this.$http.request({
url: 'common/ragFlowApi/getChats',
method: "POST",
@@ -224,6 +233,7 @@ export default {
})
.then(res=> {
if(res.code==0){
console.log('请求最新列表')
if(res.list&&res.list.length>0){
this.record_list = res.list;
}else{
@@ -247,6 +257,8 @@ export default {
this.activeRecord = index;
this.messages = [];
this.showMessages = true;
//如果正在回答的时候切换需要中断回答
this.closeWebSocket();
uni.showLoading({
title: '加载中'
})
@@ -321,7 +333,7 @@ export default {
if(this.formData.diagnosis){
question += '诊断:'+this.formData.diagnosis+'';
}
question += '病情为:'+this.formData.illness+''+this.formData.symptoms+',请根据这位患者的情况出一个中医治疗方案';
question += '病情为:'+this.formData.illness+''+this.formData.symptoms;
this.question = question;
//创建对话 获取sessionId
@@ -345,6 +357,7 @@ export default {
if (res.code==0) {
this.sessionId = res.id;
//获取回答
console.log('sessionId', this.sessionId)
this.sendQuestion();
}
});
@@ -372,23 +385,46 @@ export default {
},
//开始监听 SSE 数据
startSSE(params){
// 拼接查询字符串
const query = new URLSearchParams({
...params,
token: uni.getStorageSync('token')
}).toString();
const queryString = qs.stringify(params);
var data = {};
this.eventSource = uni.connectSocket({
url: this.$baseUrl + `websocket`,
success: () => {
console.log('WebSocket连接中...');
$http.request({
url: `common/ragFlowApi/chatToAssistantStream?${queryString}`,
method: "GET",
data,
header: {
"Content-Type": "application/json",
},
})
.then(res=> {
console.log('请求成功')
})
.catch(e=>{
console.log('失败')
});
},
fail: (err) => {
console.error('连接失败', err);
uni.showToast({ title: '连接失败', icon: 'error' });
}
});
// 创建 SSE 连接
this.eventSource = new EventSource(this.$baseUrl+`common/ragFlowApi/chatToAssistantStream?${query}`);
// 监听服务器发送的消息
this.eventSource.onmessage = (event) => {
uni.onSocketMessage((event) => {
try {
const message = JSON.parse(event.data);
if (message.data === true) {
console.log("回答已结束");
console.log("回答已结束1111");
this.pauseStatus = false;
this.eventSource.close();
this.loading = false;
//获取最近的会话记录数据
this.getRecordsData();
setTimeout(() => {
this.closeWebSocket();
}, 200);
return;
}
@@ -415,50 +451,53 @@ export default {
this.previousAnswer = answer;
}
} catch (error) {
this.$commonJS.showToast("处理消息时发生错误");
this.loading = false;
this.closeWebSocket();
}
};
//监听 SSE 连接关闭
this.eventSource.onclose = () => {
console.log("SSE 连接已关闭");
this.loading = false;
};
//监听 SSE 错误
this.eventSource.onerror = (error) => {
console.error("SSE 连接发生错误", error);
this.loading = false;
};
//获取最近的会话记录数据
this.getRecordsData();
});
//监听WebSocket连接打开
uni.onSocketOpen(() => {
console.log('WebSocket已连接');
});
//监听WebSocket错误
uni.onSocketError((err) => {
console.error('WebSocket连接错误', err);
});
//监听WebSocket关闭
uni.onSocketClose((res) => {
console.log('WebSocket 已关闭', res);
});
},
//回答界面的提交
sendAgain(){
console.log('这是再一次提问')
if(!this.question_send){
this.$commonJS.showToast("请输入发送内容");
return
if(!this.pauseStatus){
if(!this.question_send){
this.$commonJS.showToast("请输入发送内容");
return
}
const params = {
chatId: this.chatId,
chatName: this.chatName,
question: this.question_send,
sessionId: this.sessionId,
sessionName: this.question_send.slice(0, 15),
patientName: this.formData.name
};
this.messages.push({
content: `${this.question_send}`,
type: 'question',
});
this.loading = true;
this.question_send = '';
this.pauseStatus = true;
this.previousAnswer = null;
//调用后端 SSE 接口,发送问题并接收实时回答
this.startSSE(params);
}else{
console.log('不能点击了')
}
const params = {
chatId: this.chatId,
chatName: this.chatName,
question: this.question_send,
sessionId: this.sessionId,
sessionName: this.question_send.slice(0, 15),
patientName: this.formData.name
};
this.messages.push({
content: `${this.question_send}`,
type: 'question',
});
this.loading = true;
this.question_send = '';
this.pauseStatus = true;
this.previousAnswer = null;
//调用后端 SSE 接口,发送问题并接收实时回答
this.startSSE(params);
},
//点击左侧弹窗
openDrawer() {
@@ -478,15 +517,34 @@ export default {
this.chatId = null;
this.activeIndex = null;
this.activeRecord = null;
if(this.eventSource){
this.eventSource.close();
}
//中断
this.closeWebSocket();
this.previousAnswer = null;
this.pauseStatus = false;
//把缓存清除
uni.removeStorageSync('homeParams');
},
//关闭进程和监听
closeWebSocket() {
if (this.eventSource) {
// 关闭连接并移除监听
this.eventSource.close({
success: () => {
console.log('WebSocket 已关闭-closeWebSocket');
uni.offSocketMessage(); //移除消息监听
}
});
this.eventSource = null;
this.loading = false;
this.pauseStatus = false;
}
}
},
beforeDestroy() {
onHide() {
this.closeWebSocket();
},
onUnload() {
this.closeWebSocket();
}
}
</script>
@@ -711,6 +769,7 @@ export default {
}
.message-container {
display: inline;
line-height: 48rpx;
}
.message-item {
@@ -757,7 +816,7 @@ h3{
text{
display: inline-block;
padding: 20rpx;
line-height: 34rpx;
line-height: 38rpx;
background-color: rgba(81, 136, 229, 0.2);
border-radius: 15rpx;
color: #333;
@@ -772,11 +831,11 @@ h3{
flex-direction: column;
}
.list_content{
padding: 30rpx 20rpx;
padding: 60rpx 20rpx 30rpx;
overflow-y: auto;
}
.list_item{
padding: 20rpx 10rpx;
padding: 15rpx 10rpx;
}
.text_item{
display: block;