From 3e0529cf87b457c88b2788cd3676d242fefa3a9b Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 17 Oct 2024 15:06:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=88=E7=AB=AFSSH=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E8=B0=83=E6=95=B4=E5=8F=82=E6=95=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=A4=96=E9=83=A8=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/TerminalSSHView/index.vue | 27 ++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/components/TerminalSSHView/index.vue b/src/components/TerminalSSHView/index.vue index 33af459b..c5e7d73f 100644 --- a/src/components/TerminalSSHView/index.vue +++ b/src/components/TerminalSSHView/index.vue @@ -13,6 +13,11 @@ const props = defineProps({ type: String, required: true, }, + /**ws连接地址,必传 如/ws/view */ + url: { + type: String, + required: true, + }, /**网元类型,必传 */ neType: { type: String, @@ -33,6 +38,11 @@ const props = defineProps({ type: Number, default: 40, }, + /**ws发送requestId前缀 如ssh_id */ + prefix: { + type: String, + default: 'ssh', + }, }); /**终端输入DOM节点实例对象 */ @@ -148,13 +158,18 @@ function wsMessage(res: Record) { if (parts.length > 0) { let text = parts[parts.length - 1]; // 找到最后输出标记 - const lestIndex = text.lastIndexOf('\u001b[?2004h\u001b]0;'); + let lestIndex = text.lastIndexOf('\u001b[?2004h\u001b]0;'); if (lestIndex !== -1) { text = text.substring(0, lestIndex); } - if (text === '' || text === '\r\n' || text.startsWith("^C\r\n") ) { + if (text === '' || text === '\r\n' || text.startsWith('^C\r\n')) { return; } + // 是否还有最后输出标记 + lestIndex = text.lastIndexOf('\u001b[?2004h'); + if (lestIndex !== -1) { + text = text.substring(0, lestIndex); + } // console.log({ parts, text }); terminal.value.write(text); return; @@ -168,7 +183,7 @@ onMounted(() => { if (props.neType && props.neId) { // 建立链接 const options: OptionsType = { - url: '/ws/view', + url: props.url, params: { neType: props.neType, neId: props.neId, @@ -185,7 +200,7 @@ onMounted(() => { }); onBeforeUnmount(() => { - ws.close(); + if (ws.state() === WebSocket.OPEN) ws.close(); }); // 给组件设置属性 ref="xxxTerminal" @@ -200,7 +215,7 @@ defineExpose({ /**发送命令 */ send: (type: string, data: Record) => { ws.send({ - requestId: `ssh_${props.id}`, + requestId: `${props.prefix}_${props.id}`, type, data, }); @@ -208,7 +223,7 @@ defineExpose({ /**模拟按下 Ctrl+C */ ctrlC: () => { ws.send({ - requestId: `ssh_${props.id}`, + requestId: `${props.prefix}_${props.id}`, type: 'ctrl-c', }); },