feat: 简化代码

This commit is contained in:
手瓜一十雪 2024-11-27 11:35:51 +08:00
parent 0fe4911d01
commit e9fcdc7d2e

View File

@ -175,14 +175,7 @@ const operateType = ref<string>('');
// //
const configIndex = ref<number>(0); const configIndex = ref<number>(0);
// //
interface NetworkConfigType { const networkConfig: NetworkConfig & { [key: string]: any; } = {
[key: string]: any;
websocketClients: any[];
websocketServers: any[];
httpClients: any[];
httpServers: any[];
}
const networkConfig: NetworkConfigType = {
websocketClients: [], websocketClients: [],
websocketServers: [], websocketServers: [],
httpClients: [], httpClients: [],
@ -199,22 +192,12 @@ const WebConfg = ref(
['websocketClients', []], ['websocketClients', []],
]) ])
); );
interface TypeChType {
[key: string]: string;
httpServers: string;
httpClients: string;
websocketServers: string;
websocketClients: string;
}
const typeCh: Record<ComponentKey, string> = { const typeCh: Record<ComponentKey, string> = {
httpServers: 'HTTP 服务器', httpServers: 'HTTP 服务器',
httpClients: 'HTTP 客户端', httpClients: 'HTTP 客户端',
websocketServers: 'WebSocket 服务器', websocketServers: 'WebSocket 服务器',
websocketClients: 'WebSocket 客户端', websocketClients: 'WebSocket 客户端',
}; };
const getKeyByValue = (obj: TypeChType, value: string): string | undefined => {
return Object.entries(obj).find(([_, v]) => v === value)?.[0];
};
const cardConfig = ref<any>([]); const cardConfig = ref<any>([]);
const getComponent = (type: ComponentKey) => { const getComponent = (type: ComponentKey) => {
return componentMap[type]; return componentMap[type];
@ -229,7 +212,7 @@ const addConfig = () => {
const editConfig = (item: any) => { const editConfig = (item: any) => {
dialogTitle.value = '修改配置'; dialogTitle.value = '修改配置';
const type = getKeyByValue(typeCh, item.type); const type = typeCh[item.type as ComponentKey] ?? '';
if (type) { if (type) {
newTab.value = { name: item.name, data: item, type: type }; newTab.value = { name: item.name, data: item, type: type };
} }
@ -238,7 +221,7 @@ const editConfig = (item: any) => {
visibleBody.value = true; visibleBody.value = true;
}; };
const delConfig = (item: any) => { const delConfig = (item: any) => {
const type = getKeyByValue(typeCh, item.type); const type = typeCh[item.type as ComponentKey] ?? '';
if (type) { if (type) {
newTab.value = { name: item.name, data: item, type: type }; newTab.value = { name: item.name, data: item, type: type };
} }