🔨 优化 sftp 回调逻辑.

This commit is contained in:
lijiahang
2024-02-08 16:30:16 +08:00
parent e7cf5f61ef
commit 3ff67204ab
6 changed files with 40 additions and 66 deletions

View File

@@ -1,14 +1,14 @@
spring: spring:
datasource: datasource:
druid: druid:
url: jdbc:mysql://116.62.194.246:3306/orion-ops-pro?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true url: jdbc:mysql://127.0.0.1:3306/orion-ops-pro?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true
username: root username: root
password: Data@123456 password: Data@123456
initial-size: 0 initial-size: 0
min-idle: 1 min-idle: 1
max-active: 5 max-active: 5
redis: redis:
host: 116.62.194.246 host: 127.0.0.1
port: 6379 port: 6379
database: 0 database: 0
password: lijiahang password: lijiahang

View File

@@ -11,8 +11,8 @@
<sftp-table-header class="sftp-table-header" /> <sftp-table-header class="sftp-table-header" />
<!-- 表格 --> <!-- 表格 -->
<sftp-table class="sftp-table-wrapper" <sftp-table class="sftp-table-wrapper"
:list="list" :list="fileList"
:loading="loading" /> :loading="tableLoading" />
</div> </div>
</template> </template>
<template #second v-if="editView"> <template #second v-if="editView">
@@ -33,7 +33,8 @@
import { onMounted, onUnmounted, ref } from 'vue'; import { onMounted, onUnmounted, ref } from 'vue';
import { useTerminalStore } from '@/store'; import { useTerminalStore } from '@/store';
import useLoading from '@/hooks/loading'; import useLoading from '@/hooks/loading';
import data from './data'; import mockData from './data';
import { Message } from '@arco-design/web-vue';
import SftpTable from './sftp-table.vue'; import SftpTable from './sftp-table.vue';
import SftpTableHeader from './sftp-table-header.vue'; import SftpTableHeader from './sftp-table-header.vue';
@@ -42,21 +43,38 @@
}>(); }>();
const { preference, sessionManager } = useTerminalStore(); const { preference, sessionManager } = useTerminalStore();
const { loading, setLoading } = useLoading(true); const { loading: tableLoading, setLoading: setTableLoading } = useLoading(true);
const session = ref<ISftpSession>(); const session = ref<ISftpSession>();
const currentPath = ref<string>(''); const currentPath = ref<string>('');
const list = ref<Array<SftpFile>>(data); const fileList = ref<Array<SftpFile>>(mockData);
const splitSize = ref(1); const splitSize = ref(1);
const editView = ref(true); const editView = ref(true);
// 连接成功回调
const connectCallback = () => {
setTableLoading(true);
session.value?.list(undefined);
};
// 接收列表回调
const resolveList = (result: string, path: string, list: Array<SftpFile>) => {
const success = !!Number.parseInt(result);
setLoading(false);
if (!success) {
Message.error('查询失败');
return;
}
currentPath.value = path;
fileList.value = list;
};
// 初始化会话 // 初始化会话
onMounted(async () => { onMounted(async () => {
// 创建终端处理器 // 创建终端处理器
session.value = await sessionManager.openSftp(props.tab, { session.value = await sessionManager.openSftp(props.tab, {
list, connectCallback,
currentPath, resolveList
setLoading
}); });
}); });

View File

@@ -1,29 +0,0 @@
import type { ISftpSession, ISftpSessionResolver, SftpDataRef, SftpFile } from '../types/terminal.type';
import { Message } from '@arco-design/web-vue';
// sftp 会话接收器实现
export default class SftpSessionResolver implements ISftpSessionResolver {
private readonly dataRef: SftpDataRef;
private readonly session: ISftpSession;
constructor(session: ISftpSession,
dataRef: SftpDataRef) {
this.session = session;
this.dataRef = dataRef;
}
// 接受文件列表响应
resolveList(result: string, path: string, list: Array<SftpFile>) {
const success = !!Number.parseInt(result);
this.dataRef.setLoading(false);
if (!success) {
Message.error('查询失败');
return;
}
this.dataRef.currentPath = path;
this.dataRef.list = list;
}
}

View File

@@ -1,6 +1,5 @@
import type { ISftpSession, ISftpSessionResolver, ITerminalChannel, SftpDataRef } from '../types/terminal.type'; import type { ISftpSession, ISftpSessionResolver, ITerminalChannel } from '../types/terminal.type';
import { InputProtocol } from '../types/terminal.protocol'; import { InputProtocol } from '../types/terminal.protocol';
import SftpSessionResolver from './sftp-session-resolver';
// sftp 会话实现 // sftp 会话实现
export default class SftpSession implements ISftpSession { export default class SftpSession implements ISftpSession {
@@ -13,8 +12,6 @@ export default class SftpSession implements ISftpSession {
public resolver: ISftpSessionResolver; public resolver: ISftpSessionResolver;
private dataRef: SftpDataRef;
private readonly channel: ITerminalChannel; private readonly channel: ITerminalChannel;
constructor(hostId: number, constructor(hostId: number,
@@ -24,27 +21,23 @@ export default class SftpSession implements ISftpSession {
this.sessionId = sessionId; this.sessionId = sessionId;
this.channel = channel; this.channel = channel;
this.connected = false; this.connected = false;
this.dataRef = undefined as unknown as SftpDataRef;
this.resolver = undefined as unknown as ISftpSessionResolver; this.resolver = undefined as unknown as ISftpSessionResolver;
} }
// 初始化 // 初始化
init(dataRef: SftpDataRef): void { init(resolver: ISftpSessionResolver): void {
this.dataRef = dataRef; this.resolver = resolver;
// 处理器
this.resolver = new SftpSessionResolver(this, dataRef);
} }
// 设置已连接 // 设置已连接
connect(): void { connect(): void {
this.connected = true; this.connected = true;
// 加载 home 目录文件数据 // 连接回调
this.list(undefined); this.resolver.connectCallback();
} }
// 查询文件列表 // 查询文件列表
list(path: string | undefined) { list(path: string | undefined) {
this.dataRef.setLoading(true);
this.channel.send(InputProtocol.SFTP_LIST, { this.channel.send(InputProtocol.SFTP_LIST, {
sessionId: this.sessionId, sessionId: this.sessionId,
path path

View File

@@ -1,9 +1,9 @@
import type { import type {
ISftpSession, ISftpSession,
ISftpSessionResolver,
ITerminalChannel, ITerminalChannel,
ITerminalSession, ITerminalSession,
ITerminalSessionManager, ITerminalSessionManager,
SftpDataRef,
TerminalTabItem, TerminalTabItem,
XtermDomRef XtermDomRef
} from '../types/terminal.type'; } from '../types/terminal.type';
@@ -62,7 +62,7 @@ export default class TerminalSessionManager implements ITerminalSessionManager {
} }
// 打开 sftp 会话 // 打开 sftp 会话
async openSftp(tab: TerminalTabItem, dataRef: SftpDataRef): Promise<ISftpSession> { async openSftp(tab: TerminalTabItem, resolver: ISftpSessionResolver): Promise<ISftpSession> {
const sessionId = tab.key; const sessionId = tab.key;
const hostId = tab.hostId as number; const hostId = tab.hostId as number;
// 初始化客户端 // 初始化客户端
@@ -74,7 +74,7 @@ export default class TerminalSessionManager implements ITerminalSessionManager {
this.channel this.channel
); );
// 初始化 // 初始化
session.init(dataRef); session.init(resolver);
// 添加会话 // 添加会话
this.sessions[sessionId] = session; this.sessions[sessionId] = session;
// 发送会话初始化请求 // 发送会话初始化请求

View File

@@ -145,7 +145,7 @@ export interface ITerminalSessionManager {
// 打开 ssh 会话 // 打开 ssh 会话
openSsh: (tab: TerminalTabItem, domRef: XtermDomRef) => Promise<ISshSession>; openSsh: (tab: TerminalTabItem, domRef: XtermDomRef) => Promise<ISshSession>;
// 打开 sftp 会话 // 打开 sftp 会话
openSftp: (tab: TerminalTabItem, dataRef: SftpDataRef) => Promise<ISftpSession>; openSftp: (tab: TerminalTabItem, resolver: ISftpSessionResolver) => Promise<ISftpSession>;
// 获取终端会话 // 获取终端会话
getSession: <T extends ITerminalSession>(sessionId: string) => T; getSession: <T extends ITerminalSession>(sessionId: string) => T;
// 关闭终端会话 // 关闭终端会话
@@ -199,16 +199,6 @@ export interface XtermAddons {
image: ImageAddon; image: ImageAddon;
} }
// sftp 数据引用
export interface SftpDataRef {
// 文件列表
list: any;
// 当前路径
currentPath: any;
// 设置加载状态
setLoading: (loading: boolean) => void;
}
// 终端会话定义 // 终端会话定义
export interface ITerminalSession { export interface ITerminalSession {
hostId: number; hostId: number;
@@ -304,13 +294,15 @@ export interface ISftpSession extends ITerminalSession {
resolver: ISftpSessionResolver; resolver: ISftpSessionResolver;
// 初始化 // 初始化
init: (dataRef: SftpDataRef) => void; init: (resolver: ISftpSessionResolver) => void;
// 查询文件列表 // 查询文件列表
list: (path: string | undefined) => void; list: (path: string | undefined) => void;
} }
// sftp 会话接收器定义 // sftp 会话接收器定义
export interface ISftpSessionResolver { export interface ISftpSessionResolver {
// 连接后回调
connectCallback: () => void;
// 接受文件列表响应 // 接受文件列表响应
resolveList: (result: string, path: string, list: Array<SftpFile>) => void; resolveList: (result: string, path: string, list: Array<SftpFile>) => void;
} }