From e10a82ff355e5ff2983d3ffb4679c378b0451287 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Wed, 27 Mar 2024 19:13:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=88=E7=AB=AF=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E7=BB=84=E4=BB=B6=E6=B7=BB=E5=8A=A0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=9B=91=E5=90=AC=E5=92=8C=E5=8F=91=E9=80=81=E5=8F=A5?= =?UTF-8?q?=E6=9F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/TerminalSSH/index.vue | 35 ++++++++++++++++++++++++- src/components/TerminalTelnet/index.vue | 23 ++++++++++++++-- src/views/tool/terminal/index.vue | 1 + 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/components/TerminalSSH/index.vue b/src/components/TerminalSSH/index.vue index 35cc4ce0..f03056a9 100644 --- a/src/components/TerminalSSH/index.vue +++ b/src/components/TerminalSSH/index.vue @@ -6,7 +6,7 @@ import 'xterm/css/xterm.css'; import { RESULT_CODE_ERROR } from '@/constants/result-constants'; import { OptionsType, WS } from '@/plugins/ws-websocket'; const ws = new WS(); -const emit = defineEmits(['connect', 'close']); +const emit = defineEmits(['connect', 'close', 'message']); const props = defineProps({ /**终端ID,必传 */ id: { @@ -28,6 +28,16 @@ const props = defineProps({ type: Number, default: 40, }, + /**禁止输入 */ + disable: { + type: Boolean, + default: false, + }, + /**初始发送命令 */ + initCmd: { + type: [String, Boolean], + default: false, + }, }); /**终端输入DOM节点实例对象 */ @@ -53,6 +63,7 @@ function handleRanderXterm(container: HTMLElement | undefined) { scrollback: 1000, scrollSensitivity: 15, tabStopWidth: 4, + disableStdin: props.disable, // 禁止输入 }); // 挂载 xterm.open(container); @@ -143,6 +154,14 @@ function wsOpen(ev: any) { hostId: props.hostId, id: props.id, }); + // 初始发送命令 + if (typeof props.initCmd === 'string') { + ws.send({ + requestId: `ssh_${props.hostId}`, + type: 'ssh', + data: `${props.initCmd}\n`, + }); + } }); } @@ -177,6 +196,7 @@ function wsClose(code: number) { /**接收消息后回调 */ function wsMessage(res: Record) { + emit('message', res); // console.log('wsMessage', res); const { code, requestId, data } = res; if (code === RESULT_CODE_ERROR) { @@ -211,6 +231,19 @@ onMounted(() => { onBeforeUnmount(() => { ws.close(); }); + +// 给组件设置属性 ref="xxxTerminal" +// setup内使用 const xxxTerminal = ref(); +defineExpose({ + /**发送方法 */ + send: (data: string) => { + ws.send({ + requestId: `ssh_${props.hostId}`, + type: 'ssh', + data: `${data}\n`, + }); + }, +});