Files
assist/src/web/static/js/services/api.js
zhaojie 2026007045 refactor: 移除冗余文件并优化代码结构
- 删除多个不再使用的脚本和配置文件,包括 `auto_push.bat`, `check_and_fix_users.py`, `init.sql` 等。
- 新增 `git_push.bat` 和 `git_push.sh` 脚本以简化 Git 推送流程。
- 更新 `README.md` 以反映最新的功能和结构变化。
- 优化前端代码,添加新的页面和组件,提升用户体验。

此提交旨在清理项目结构并增强代码可维护性。
2025-12-08 00:53:23 +08:00

161 lines
3.8 KiB
JavaScript

/**
* 统一API服务
* 提供所有API调用的统一接口
*/
class ApiService {
constructor() {
this.baseURL = '';
this.defaultTimeout = 30000; // 30秒超时
}
async request(endpoint, options = {}) {
const url = `${this.baseURL}${endpoint}`;
const config = {
headers: {
'Content-Type': 'application/json',
...options.headers
},
timeout: options.timeout || this.defaultTimeout,
...options
};
try {
const response = await fetch(url, config);
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || `HTTP ${response.status}: ${response.statusText}`);
}
return data;
} catch (error) {
console.error(`API请求失败: ${endpoint}`, error);
throw error;
}
}
// 健康检查相关
async getHealth() {
return this.request('/api/health');
}
// 预警相关
async getAlerts() {
return this.request('/api/alerts');
}
async createAlert(alertData) {
return this.request('/api/alerts', {
method: 'POST',
body: JSON.stringify(alertData)
});
}
async updateAlert(alertId, alertData) {
return this.request(`/api/alerts/${alertId}`, {
method: 'PUT',
body: JSON.stringify(alertData)
});
}
async deleteAlert(alertId) {
return this.request(`/api/alerts/${alertId}`, {
method: 'DELETE'
});
}
// 规则相关
async getRules() {
return this.request('/api/rules');
}
async createRule(ruleData) {
return this.request('/api/rules', {
method: 'POST',
body: JSON.stringify(ruleData)
});
}
async updateRule(ruleId, ruleData) {
return this.request(`/api/rules/${ruleId}`, {
method: 'PUT',
body: JSON.stringify(ruleData)
});
}
async deleteRule(ruleId) {
return this.request(`/api/rules/${ruleId}`, {
method: 'DELETE'
});
}
// 监控相关
async getMonitorStatus() {
return this.request('/api/monitor/status');
}
async startMonitoring() {
return this.request('/api/monitor/start', {
method: 'POST'
});
}
async stopMonitoring() {
return this.request('/api/monitor/stop', {
method: 'POST'
});
}
// Agent相关
async getAgentStatus() {
return this.request('/api/agent/status');
}
async toggleAgent(enabled) {
return this.request('/api/agent/toggle', {
method: 'POST',
body: JSON.stringify({ enabled })
});
}
async getAgentHistory(limit = 50) {
return this.request(`/api/agent/action-history?limit=${limit}`);
}
// 车辆数据相关
async getVehicleData(params = {}) {
const queryString = new URLSearchParams(params).toString();
return this.request(`/api/vehicle/data?${queryString}`);
}
async addVehicleData(data) {
return this.request('/api/vehicle/data', {
method: 'POST',
body: JSON.stringify(data)
});
}
// 对话相关
async createChatSession(data) {
return this.request('/api/chat/session', {
method: 'POST',
body: JSON.stringify(data)
});
}
async sendChatMessage(data) {
return this.request('/api/chat/message', {
method: 'POST',
body: JSON.stringify(data)
});
}
async getChatHistory(sessionId) {
return this.request(`/api/chat/history/${sessionId}`);
}
}
// 创建全局API服务实例
const apiService = new ApiService();