F1 告警静默规则 [done]: - 后端 alert_silence.go: CRUD + Toggle (onetime/daily/weekly 三模式) - 告警引擎 isSilenced 集成 (alert_engine.go) - 前端 SilenceRuleTab + SilenceDialog - 路由 /alerts/silences 已注册 F2 审计日志导出 [done]: - 重写 ExportAuditLogs: 真正的 CSV 导出 (encoding/csv + UTF-8 BOM) - 支持 module/action/username/startTime/endTime 筛选 - 导出限制 10000 条防溢出,自动记录审计日志 - JSON 格式向后兼容 (format=json) - 前端 exportLogs 传递查询参数 同时包含累积的前期工作: - 目录重构 internal/ -> controller/model/router (v0.55-v0.57) - 后端分层架构 dto/repository/service/utils - 审计模块全功能 (终端审计/操作审计/登录日志) - asciicast v2 录制格式 - SFTP 文件浏览器 - StatStrip 紧凑统计条 - 在线会话自动刷新 - LiveMonitor WebSocket 诊断修复 验证: go build ✅ | npm run build ✅ | Docker 部署 ✅ | 10 回归端点 ✅ | CSV 导出 ✅ | 静默 CRUD ✅
137 lines
4.3 KiB
TypeScript
137 lines
4.3 KiB
TypeScript
import { useUserStore } from '@/store';
|
|
|
|
const BASE_URL = '/api/v1';
|
|
|
|
// 后端统一响应格式
|
|
export interface ApiResponse<T = unknown> {
|
|
code: number;
|
|
message: string;
|
|
data: T;
|
|
}
|
|
|
|
// 列表响应格式
|
|
export interface ListResponse<T = unknown> {
|
|
list: T[];
|
|
total: number;
|
|
}
|
|
|
|
type Json = unknown;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async function request<T = any>(
|
|
method: string,
|
|
url: string,
|
|
body?: Json,
|
|
params?: Record<string, string | number | boolean | undefined>
|
|
): Promise<T> {
|
|
let fullUrl = `${BASE_URL}${url}`;
|
|
if (params) {
|
|
const searchParams = new URLSearchParams();
|
|
Object.entries(params).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
searchParams.append(key, String(value));
|
|
}
|
|
});
|
|
const qs = searchParams.toString();
|
|
if (qs) fullUrl += '?' + qs;
|
|
}
|
|
|
|
const token = useUserStore.getState().token || localStorage.getItem('token');
|
|
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
if (token) {
|
|
headers['Authorization'] = 'Bearer ' + token;
|
|
}
|
|
|
|
const response = await fetch(fullUrl, {
|
|
method,
|
|
headers,
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
useUserStore.getState().logout();
|
|
window.location.href = '/login';
|
|
throw new Error('登录已过期,请重新登录');
|
|
}
|
|
|
|
const json: ApiResponse<T> = await response.json();
|
|
|
|
if (json.code && json.code !== 200) {
|
|
throw new Error(json.message || '请求失败');
|
|
}
|
|
|
|
return (json.data !== undefined ? json.data : json) as T;
|
|
}
|
|
|
|
export const http = {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
get: <T = any>(url: string, params?: Record<string, string | number | boolean | undefined>) =>
|
|
request<T>('GET', url, undefined, params),
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
post: <T = any>(url: string, body?: Json) =>
|
|
request<T>('POST', url, body),
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
put: <T = any>(url: string, body?: Json) =>
|
|
request<T>('PUT', url, body),
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
delete: <T = any>(url: string, body?: Json) =>
|
|
request<T>('DELETE', url, body),
|
|
|
|
getRaw: async (url: string): Promise<string> => {
|
|
const tk = useUserStore.getState().token;
|
|
const baseUrl = import.meta.env.VITE_API_BASE_URL || '';
|
|
const resp = await fetch(baseUrl + '/api/v1' + url, { headers: { Authorization: 'Bearer ' + tk } });
|
|
if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
|
return resp.text();
|
|
},
|
|
|
|
putRaw: async (url: string, body: string, contentType: string): Promise<unknown> => {
|
|
const tk = useUserStore.getState().token;
|
|
const baseUrl = import.meta.env.VITE_API_BASE_URL || '';
|
|
const resp = await fetch(baseUrl + '/api/v1' + url, {
|
|
method: 'PUT',
|
|
headers: { Authorization: 'Bearer ' + tk, 'Content-Type': contentType },
|
|
body,
|
|
});
|
|
if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
|
return resp.json();
|
|
},
|
|
|
|
download: async (url: string, filename?: string): Promise<void> => {
|
|
const tk = useUserStore.getState().token;
|
|
const baseUrl = import.meta.env.VITE_API_BASE_URL || '';
|
|
const resp = await fetch(baseUrl + '/api/v1' + url, {
|
|
headers: { Authorization: 'Bearer ' + tk },
|
|
});
|
|
if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
|
const blob = await resp.blob();
|
|
const a = document.createElement('a');
|
|
a.href = URL.createObjectURL(blob);
|
|
const disp = resp.headers.get('Content-Disposition') || '';
|
|
const m = disp.match(/filename="?(.+?)"?$/);
|
|
a.download = filename || (m ? m[1] : 'download');
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(a.href);
|
|
},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
upload: async <T = any>(url: string, formData: FormData): Promise<T> => {
|
|
const tk = useUserStore.getState().token;
|
|
const baseUrl = import.meta.env.VITE_API_BASE_URL || '';
|
|
const resp = await fetch(baseUrl + '/api/v1' + url, {
|
|
method: 'POST',
|
|
headers: { Authorization: 'Bearer ' + tk },
|
|
body: formData,
|
|
});
|
|
if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
|
return resp.json();
|
|
},
|
|
};
|
|
|
|
export default http;
|