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', }); },