diff --git a/src/components/tabs.vue b/src/components/tabs.vue index 77c4719..39dcc23 100644 --- a/src/components/tabs.vue +++ b/src/components/tabs.vue @@ -35,6 +35,7 @@ const activePath = ref(route.fullPath) const tabs = useTabsStore(); // 设置标签 const setTags = (route: any) => { + console.log("set tag:",route); const isExist = tabs.list.some(item => { return item.path === route.fullPath; }); @@ -45,6 +46,7 @@ const setTags = (route: any) => { path: route.fullPath }); } + console.log("tabs list:", tabs.list); }; setTags(route); onBeforeRouteUpdate(to => { diff --git a/src/views/system/gen-chat.vue b/src/views/system/gen-chat.vue index 0df78a8..c9a4aa6 100644 --- a/src/views/system/gen-chat.vue +++ b/src/views/system/gen-chat.vue @@ -553,22 +553,26 @@ onMounted(() => { // // 浏览器环境 // md.use(markdownItMermaid); // } - let url = - "wss://pm.ljsea.top/im/ai_chat_ws?" + - "token=" + - localStorage.getItem("token"); - //获取模型列表 - let test_url = "ws://127.0.0.1:8084/im/ai_chat_ws?" + "token=" + localStorage.getItem("token"); - //url =test_url; - socket.value = new WebSocket(url); - socket.value.onopen = () => { - console.log("WebSocket 连接已建立"); - ElMessage.success("连接成功"); - }; - //getMessage(125); - messagesContainer.value = document.querySelector(".chat-messages"); + IMWSConnect(); - socket.value.onmessage = (event) => { + + + userUIconfigInfo.value = JSON.parse( + localStorage.getItem("userUIconfigInfo") || "{}" + ); + console.log("userUIconfigInfo:", userUIconfigInfo.value); + messagesContainer.value = document.querySelector(".chat-messages"); + //console.log("userUIconfigInfo:", userUIconfigInfo.value); +}); + +onUnmounted(() => { + if (socket.value) { + socket.value.close(); + } +}); + +const doReceiveMessage = (event) => { + // 处理接收到的消息 let msg: WSMessage = JSON.parse(event.data); const existingMessage = messages.find( (msg) => msg.role === "assistant" && !msg.finished @@ -584,64 +588,7 @@ onMounted(() => { finished: false, }); } - //console.log("resp:", msg); - sessionID.value = msg.session_id; - currentAIMessage.value += msg.msg.msg.response; - if (msg.msg.msg.done) { - const assistantMessage = messages[messages.length - 1]; - assistantMessage.finished = true; - loading.value = false; - doButtonD(); - } - nextTick(() => { - scrollToBottom(); // 新增滚动调用 - }); - }; - socket.value.onclose = () => { - console.log("WebSocket 连接已关闭"); - ElMessage.error("连接已关闭"); - //重新连接 - //socket.value = new WebSocket(url); - socket.value = null; - }; - - socket.value.onerror = (error) => { - //socket.value = new WebSocket(url); - socket.value = null; - console.error("WebSocket 发生错误:", error); - }; - - userUIconfigInfo.value = JSON.parse( - localStorage.getItem("userUIconfigInfo") || "{}" - ); - //console.log("userUIconfigInfo:", userUIconfigInfo.value); -}); - -onUnmounted(() => { - if (socket.value) { - socket.value.close(); - } -}); - -const doReceiveMessage = (event) => { - // 处理接收到的消息 - let msg: WSMessage = JSON.parse(event.data); - const existingMessage = messages.find( - (msg) => msg.role === "assistant" && !msg.finished - ); - if (existingMessage) { - // 追加内容 - existingMessage.content += msg.msg.msg.response; - } else { - // 新消息 - messages.push({ - role: "assistant", - content: msg.msg.msg.response, - finished: false, - }); - } - //console.log("resp:", msg); sessionID.value = msg.session_id; currentAIMessage.value += msg.msg.msg.response; if (msg.msg.msg.done) { @@ -675,14 +622,23 @@ const updateUserUIconfigInfo = () => { }); } -const sendMessage = () => { - localStorage.setItem("gen_ai_chat_model_id", selectModel.value.toString()); - sendMessageWithFile() +const sendMessage = async () => { + //测试ws是否连接断开 + if (socket.value == null) { + await IMWSConnect(); + + } + if(loading.value === true){ + ElMessage.warning("正在等待AI回复,请稍后再试"); + return; + } + await nextTick(); + sendMessageWithFile(); return; }; -const sendMessageWithFile = () => { +const sendMessageWithFile =async () => { if (inputMessage.value.trim() === "") { ElMessage.warning("消息不能为空"); return; @@ -725,33 +681,13 @@ const sendMessageWithFile = () => { } console.log("end_msg:", end_msg); - if(socket.value == null){ - socket.value = new WebSocket(wssUrl); - socket.value.onmessage = (event) => { - doReceiveMessage(event); - }; - socket.value.onclose = () => { - console.log("WebSocket 连接已关闭"); - ElMessage.error("连接已关闭"); - //重新连接 - //socket.value = new WebSocket(url); - socket.value = null; - }; - socket.value.onerror = (error) => { - //socket.value = new WebSocket(url); - socket.value = null; - console.error("WebSocket 发生错误:", error); - }; - ElMessage.error("已重新连接!"); - } - try { socket.value.send(JSON.stringify(end_msg)); console.log("send msg:", end_msg); } catch (e) { - ElMessage.error("发送失败!连接已断开!"); - socket.value = new WebSocket(socket.value.url); - return; + //ElMessage.error("发送失败!请刷新页面!"); + await IMWSConnect(); + socket.value.send(JSON.stringify(end_msg)); } if (sessionID.value == 0) { sessionName.value = inputMessage.value; @@ -769,10 +705,6 @@ const sendMessageWithFile = () => { } } - // let img_content= img_msg.image_content - // for (let i = 0; i < img_content.length; i++) { - // pMsgContent += `![${img_content[i].img_name}](${img_content[i].img_url})` + "\n"; - // } pMsgContent = pMsgContent + "输入:" + file_msg.text; } else { pMsgContent = end_msg.msg; @@ -788,6 +720,37 @@ const sendMessageWithFile = () => { } }; +const IMWSConnect = async () =>{ + let url = + "wss://pm.ljsea.top/im/ai_chat_ws?" + + "token=" + + localStorage.getItem("token"); + //获取模型列表 + let test_url = "ws://127.0.0.1:8084/im/ai_chat_ws?" + "token=" + localStorage.getItem("token"); + //url =test_url; + socket.value =new WebSocket(url); + socket.value.onopen = () => { + console.log("WebSocket 连接已建立"); + ElMessage.success("连接成功"); + }; + socket.value.onmessage = (event) => { + doReceiveMessage(event); + }; + + socket.value.onclose = () => { + console.log("WebSocket 连接已关闭"); + ElMessage.error("连接已关闭"); + //重新连接 + //socket.value = new WebSocket(url); + socket.value = null; + }; + + socket.value.onerror = (error) => { + socket.value = null; + console.log("WebSocket 连接发生错误:", error); + }; +} + const loadSession = async (session_id: number) => { sessionID.value = session_id; messages.length = 0; // 清空消息 @@ -836,7 +799,7 @@ const getMessage = async (session_id: number) => { }; result = await GetMessageService(req); if (result["code"] === 0) { - console.log(result["data"]); + // console.log(result["data"]); let data = result["data"]; for (let i = 0; i < data.length; i++) { if (data[i]["Type"] === 3) { diff --git a/src/views/system/manage-file.vue b/src/views/system/manage-file.vue index 019748e..da2ac01 100644 --- a/src/views/system/manage-file.vue +++ b/src/views/system/manage-file.vue @@ -21,7 +21,7 @@ -
+
{ diff --git a/src/views/system/slide-code-edit.vue b/src/views/system/slide-code-edit.vue index a8e3171..1c863b7 100644 --- a/src/views/system/slide-code-edit.vue +++ b/src/views/system/slide-code-edit.vue @@ -1,6 +1,6 @@