Files
smart-cropx-ui/src/app/(app)/central-config/monitor/operation-log/components/operationLogApi.ts
2025-11-10 09:19:56 +08:00

212 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* filekorolheader: 操作日志API - 操作日志相关接口调用
* 功能:获取操作日志列表、统计、导出等功能
* 路径:/central-config/monitor/operation-log/components/operationLogApi
* 规范遵循crop-x/docs/开发项目规范.md使用SDK生成的API接口
*/
import {
listOperationLogsApiV1LogsOperationOperationLogsGet,
getOperationStatisticsApiV1LogsOperationOperationLogsStatisticsGet,
exportOperationLogsApiV1LogsOperationOperationLogsExportGet
} from '@/lib/api/sdk.gen';
// 操作日志接口
export interface OperationLog {
id: string;
created_at: string;
updated_at: string;
username: string;
user_id: string | null;
operation_type: string;
module: string;
action: string;
request_method: string;
request_url: string;
request_headers: any | null;
request_body: any | null;
request_params: any | null;
response_status: number;
response_body: any | null;
error_message: string | null;
processing_time: number;
}
// 分页参数接口
export interface OperationLogsQueryParams {
page?: number;
size?: number;
username?: string;
module?: string;
action?: string;
operation_type?: string;
response_status?: number;
start_time?: string;
end_time?: string;
sort_order?: 'asc' | 'desc';
order_by?: string;
}
// 分页状态接口
export interface PaginationState {
page: number;
size: number;
total: number;
totalPages?: number;
hasNext?: boolean;
hasPrev?: boolean;
}
// 统计数据接口
export interface OperationLogStatistics {
total_operations: number;
successful_operations: number;
failed_operations: number;
unique_users: number;
success_rate: number;
average_processing_time: number;
}
/**
* 获取操作日志列表
*/
export const fetchOperationLogs = async (params: OperationLogsQueryParams = {}) => {
try {
// Get token from localStorage
const storedUser = localStorage.getItem('user');
let headers = {};
if (storedUser) {
const userData = JSON.parse(storedUser);
if (userData.token) {
headers = {
'Authorization': `Bearer ${userData.token}`
};
}
}
const response = await listOperationLogsApiV1LogsOperationOperationLogsGet({
headers,
query: {
page: params.page || 1,
size: params.size || 10,
username: params.username,
module: params.module,
action: params.action,
operation_type: params.operation_type,
response_status: params.response_status,
start_time: params.start_time,
end_time: params.end_time,
sort_order: params.sort_order || 'desc',
order_by: params.order_by || 'created_at',
}
});
return {
data: response.data?.data || [],
page: response.data?.page || 1,
size: response.data?.size || 10,
total: response.data?.total || 0,
totalPages: response.data?.total_pages || 0,
hasNext: response.data?.has_next || false,
hasPrev: response.data?.has_prev || false,
};
} catch (error) {
console.error('Failed to fetch operation logs:', error);
throw error;
}
};
/**
* 获取操作统计信息
*/
export const fetchOperationStatistics = async () => {
try {
// Get token from localStorage
const storedUser = localStorage.getItem('user');
let headers = {};
if (storedUser) {
const userData = JSON.parse(storedUser);
if (userData.token) {
headers = {
'Authorization': `Bearer ${userData.token}`
};
}
}
const response = await getOperationStatisticsApiV1LogsOperationOperationLogsStatisticsGet({
headers
});
return response.data;
} catch (error) {
console.error('Failed to fetch operation statistics:', error);
throw error;
}
};
/**
* 导出操作日志
*/
export const exportOperationLogs = async (params: OperationLogsQueryParams = {}) => {
try {
// Get token from localStorage
const storedUser = localStorage.getItem('user');
let headers = {};
if (storedUser) {
const userData = JSON.parse(storedUser);
if (userData.token) {
headers = {
'Authorization': `Bearer ${userData.token}`
};
}
}
const response = await exportOperationLogsApiV1LogsOperationOperationLogsExportGet({
headers,
query: {
username: params.username,
module: params.module,
action: params.action,
operation_type: params.operation_type,
response_status: params.response_status,
start_time: params.start_time,
end_time: params.end_time,
}
});
return response.data;
} catch (error) {
console.error('Failed to export operation logs:', error);
throw error;
}
};
/**
* 转换操作日志数据 - 适配组件使用
*/
export const transformOperationLogData = (log: any): OperationLog => ({
id: log.id,
created_at: log.created_at,
updated_at: log.updated_at,
username: log.username,
user_id: log.user_id,
operation_type: log.operation_type,
module: log.module,
action: log.action,
request_method: log.request_method,
request_url: log.request_url,
request_headers: log.request_headers,
request_body: log.request_body,
request_params: log.request_params,
response_status: log.response_status,
response_body: log.response_body,
error_message: log.error_message,
processing_time: log.processing_time,
});
/**
* 批量转换操作日志数据
*/
export const transformOperationLogsList = (logs: any[]): OperationLog[] => {
return logs.map(transformOperationLogData);
};