Files
worklist/frontend/js/api.js
2026-01-13 09:38:11 +00:00

151 lines
4.0 KiB
JavaScript
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.

// API调用模块
class WorkListAPI {
constructor() {
this.baseURL = '/api';
}
// 通用请求方法
async request(endpoint, options = {}) {
const url = `${this.baseURL}${endpoint}`;
const config = {
credentials: 'same-origin', // 包含cookies以支持session
headers: {
'Content-Type': 'application/json',
...options.headers
},
...options
};
try {
const response = await fetch(url, config);
if (!response.ok) {
// 尝试解析错误响应
let errorMessage = `HTTP错误: ${response.status}`;
try {
const errorData = await response.json();
// 使用后端返回的详细错误信息
errorMessage = errorData.detail || errorData.error || errorMessage;
} catch (parseError) {
// 如果无法解析JSON使用默认错误消息
console.error('无法解析错误响应:', parseError);
}
throw new Error(errorMessage);
}
return await response.json();
} catch (error) {
console.error('API请求失败:', error);
throw error;
}
}
// 认证API
async login(username, password) {
return this.request('/auth/login', {
method: 'POST',
body: JSON.stringify({ username, password })
});
}
async logout() {
return this.request('/auth/logout', {
method: 'POST'
});
}
async checkAuth() {
return this.request('/auth/check');
}
async register(username, password) {
return this.request('/auth/register', {
method: 'POST',
body: JSON.stringify({ username, password })
});
}
// 任务管理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();