This commit is contained in:
2025-12-30 09:03:29 +00:00
commit 32294ebec1
19 changed files with 3146 additions and 0 deletions

115
frontend/js/api.js Normal file
View File

@@ -0,0 +1,115 @@
// API调用模块
class WorkListAPI {
constructor() {
this.baseURL = '/api';
}
// 通用请求方法
async request(endpoint, options = {}) {
const url = `${this.baseURL}${endpoint}`;
const config = {
headers: {
'Content-Type': 'application/json',
...options.headers
},
...options
};
try {
const response = await fetch(url, config);
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.error || `HTTP错误: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('API请求失败:', error);
throw error;
}
}
// 任务管理API
async getTasks() {
return this.request('/tasks');
}
async createTask(taskData) {
return this.request('/tasks', {
method: 'POST',
body: JSON.stringify(taskData)
});
}
async updateTask(taskId, taskData) {
return this.request(`/tasks/${taskId}`, {
method: 'PUT',
body: JSON.stringify(taskData)
});
}
async deleteTask(taskId) {
return this.request(`/tasks/${taskId}`, {
method: 'DELETE'
});
}
async polishTaskDescription(taskId) {
return this.request(`/tasks/${taskId}/polish`, {
method: 'POST'
});
}
// 计时器API
async startTimer(taskId) {
return this.request('/timer/start', {
method: 'POST',
body: JSON.stringify({ task_id: taskId })
});
}
async stopTimer(taskId) {
return this.request('/timer/stop', {
method: 'POST',
body: JSON.stringify({ task_id: taskId })
});
}
async getTimerStatus(taskId) {
return this.request(`/timer/status/${taskId}`);
}
async getTimerStatuses(taskIds) {
return this.request('/timer/status/batch', {
method: 'POST',
body: JSON.stringify({ task_ids: taskIds })
});
}
// 报表API
async getDailyReport(date) {
const params = date ? `?date=${date}` : '';
return this.request(`/reports/daily${params}`);
}
async getSummaryReport(days = 7) {
return this.request(`/reports/summary?days=${days}`);
}
// 时间段历史API
async getTaskTimeHistory(taskId, days = 30, page = 1, perPage = 20) {
return this.request(`/tasks/${taskId}/time-history?days=${days}&page=${page}&per_page=${perPage}`);
}
async getAllTimeHistory(days = 7, taskId = null) {
const params = new URLSearchParams({ days: days });
if (taskId) {
params.append('task_id', taskId);
}
return this.request(`/time-history?${params.toString()}`);
}
}
// 创建全局API实例
const api = new WorkListAPI();