refactor(frontend): 重构数据解析逻辑
- 新增 StopAndParseCapture 函数,整合停止抓包和解析数据流程 -重构 ReadRawJsonFile 函数,返回解析后的 ParsedResult 对象 - 优化数据解析流程,提高代码可读性和性能 - 调整前端 CapturePage组件,使用新的解析接口
This commit is contained in:
@@ -4,12 +4,10 @@ import {DownloadOutlined, PlayCircleOutlined, SettingOutlined, StopOutlined} fro
|
||||
import '../App.css';
|
||||
import {
|
||||
ExportData,
|
||||
GetCapturedData,
|
||||
GetNetworkInterfaces,
|
||||
ParseData,
|
||||
ReadRawJsonFile,
|
||||
StartCapture,
|
||||
StopCapture
|
||||
StopAndParseCapture
|
||||
} from '../../wailsjs/go/service/App';
|
||||
import {useCaptureStore} from '../store/useCaptureStore';
|
||||
|
||||
@@ -146,68 +144,24 @@ function CapturePage() {
|
||||
setCapturedData([]);
|
||||
try {
|
||||
setLoading(true);
|
||||
const stopResult = await safeApiCall(
|
||||
() => StopCapture(),
|
||||
'停止抓包失败'
|
||||
// 新接口:直接停止抓包并解析
|
||||
const parsedData = await safeApiCall(
|
||||
() => StopAndParseCapture(),
|
||||
'停止抓包并解析数据失败'
|
||||
);
|
||||
if (stopResult === undefined) {
|
||||
setIsCapturing(false);
|
||||
console.log("解析数据:"+JSON.stringify(parsedData))
|
||||
if (!parsedData || !Array.isArray((parsedData as CaptureResult).items)) {
|
||||
setParsedData({ items: [], heroes: [] } as CaptureResult);
|
||||
showMessage('error', '解析数据失败');
|
||||
return;
|
||||
}
|
||||
setIsCapturing(false);
|
||||
const data = await safeApiCall(
|
||||
() => GetCapturedData(),
|
||||
'获取抓包数据失败'
|
||||
);
|
||||
if (!data || data.length === 0) {
|
||||
setCapturedData([]);
|
||||
showMessage('warning', '未捕获到数据');
|
||||
return;
|
||||
}
|
||||
setCapturedData(data);
|
||||
if (data && data.length > 0) {
|
||||
data.forEach((item, idx) => {
|
||||
let hexStr = '';
|
||||
if (/^[0-9a-fA-F\s]+$/.test(item)) {
|
||||
hexStr = item;
|
||||
} else {
|
||||
hexStr = Array.from(item).map(c => c.charCodeAt(0).toString(16).padStart(2, '0')).join(' ');
|
||||
}
|
||||
console.log(`抓包数据[${idx}]:`, hexStr);
|
||||
});
|
||||
}
|
||||
if (data.length > 0) {
|
||||
const parsed = await safeApiCall(
|
||||
() => ParseData(data),
|
||||
'解析数据失败'
|
||||
);
|
||||
if (!parsed) {
|
||||
setParsedData({ items: [], heroes: [] });
|
||||
showMessage('error', '解析数据失败');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const parsedData = JSON.parse(parsed);
|
||||
if (!parsedData || !Array.isArray(parsedData.items)) {
|
||||
setParsedData({ items: [], heroes: [] });
|
||||
showMessage('error', '解析数据失败');
|
||||
return;
|
||||
}
|
||||
setParsedData(parsedData);
|
||||
showMessage('success', '数据处理完成');
|
||||
} catch (parseError) {
|
||||
console.error('解析JSON失败:', parseError);
|
||||
setParsedData({ items: [], heroes: [] });
|
||||
showMessage('error', '解析数据失败');
|
||||
}
|
||||
} else {
|
||||
showMessage('warning', '未捕获到数据');
|
||||
}
|
||||
setParsedData(parsedData as CaptureResult);
|
||||
showMessage('success', '数据处理完成');
|
||||
} catch (error) {
|
||||
console.error('停止抓包时发生未知错误:', error);
|
||||
setIsCapturing(false);
|
||||
setCapturedData([]);
|
||||
setParsedData({ items: [], heroes: [] });
|
||||
setParsedData({ items: [], heroes: [] } as CaptureResult);
|
||||
setLoading(false);
|
||||
showMessage('error', '抓包失败,已重置状态');
|
||||
return;
|
||||
@@ -269,8 +223,7 @@ function CapturePage() {
|
||||
const fetchParsedDataFromBackend = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const raw = await ReadRawJsonFile();
|
||||
const json = JSON.parse(raw);
|
||||
const json = await ReadRawJsonFile();
|
||||
console.log('已加载本地解析数据:', json);
|
||||
const safeData = {
|
||||
items: Array.isArray(json.items) ? json.items : [],
|
||||
|
||||
@@ -34,6 +34,20 @@ export namespace model {
|
||||
this.is_loopback = source["is_loopback"];
|
||||
}
|
||||
}
|
||||
export class ParsedResult {
|
||||
items: any[];
|
||||
heroes: any[];
|
||||
|
||||
static createFrom(source: any = {}) {
|
||||
return new ParsedResult(source);
|
||||
}
|
||||
|
||||
constructor(source: any = {}) {
|
||||
if ('string' === typeof source) source = JSON.parse(source);
|
||||
this.items = source["items"];
|
||||
this.heroes = source["heroes"];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
4
frontend/wailsjs/go/service/App.d.ts
vendored
4
frontend/wailsjs/go/service/App.d.ts
vendored
@@ -12,8 +12,10 @@ export function GetNetworkInterfaces():Promise<Array<model.NetworkInterface>>;
|
||||
|
||||
export function ParseData(arg1:Array<string>):Promise<string>;
|
||||
|
||||
export function ReadRawJsonFile():Promise<string>;
|
||||
export function ReadRawJsonFile():Promise<model.ParsedResult>;
|
||||
|
||||
export function StartCapture(arg1:string):Promise<void>;
|
||||
|
||||
export function StopAndParseCapture():Promise<model.ParsedResult>;
|
||||
|
||||
export function StopCapture():Promise<void>;
|
||||
|
||||
@@ -30,6 +30,10 @@ export function StartCapture(arg1) {
|
||||
return window['go']['service']['App']['StartCapture'](arg1);
|
||||
}
|
||||
|
||||
export function StopAndParseCapture() {
|
||||
return window['go']['service']['App']['StopAndParseCapture']();
|
||||
}
|
||||
|
||||
export function StopCapture() {
|
||||
return window['go']['service']['App']['StopCapture']();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user