104 lines
3.1 KiB
TypeScript
104 lines
3.1 KiB
TypeScript
import axios from 'axios';
|
||
import { Event, ApiResponse } from './types';
|
||
|
||
const API_BASE_URL = 'http://47.101.56.101:8976/api/app_calendar';
|
||
|
||
const api = axios.create({
|
||
baseURL: API_BASE_URL,
|
||
timeout: 10000,
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
},
|
||
});
|
||
|
||
export const formatError = (error: any): string => {
|
||
if (error.response) {
|
||
// The request was made and the server responded with a status code
|
||
// that falls out of the range of 2xx
|
||
return `服务器响应 ${error.response.status}: ${error.response.statusText}\n${JSON.stringify(error.response.data)}`;
|
||
} else if (error.request) {
|
||
// The request was made but no response was received
|
||
return `服务器无响应: 无法连接到 ${API_BASE_URL}\n请检查服务器是否已启动以及网络连接`;
|
||
} else if (error.message) {
|
||
// Something happened in setting up the request that triggered an Error
|
||
return `请求错误: ${error.message}`;
|
||
} else {
|
||
return `未知错误: ${JSON.stringify(error)}`;
|
||
}
|
||
};
|
||
|
||
export const getEvents = async (date?: Date): Promise<Event[]> => {
|
||
const year = date ? date.getFullYear() : undefined;
|
||
const month = date ? date.getMonth() + 1 : undefined;
|
||
return eventApi.getEvents(year, month);
|
||
};
|
||
|
||
export const eventApi = {
|
||
async getEvents(year?: number, month?: number): Promise<Event[]> {
|
||
try {
|
||
// 拉取更多月份的数据(为了包含所有可能的循环事件)
|
||
const requests: Promise<any>[] = [];
|
||
|
||
if (year && month) {
|
||
// 拉取前 6 个月和当前月的数据
|
||
for (let i = 6; i >= 0; i--) {
|
||
let m = month - i;
|
||
let y = year;
|
||
while (m < 1) {
|
||
m += 12;
|
||
y--;
|
||
}
|
||
requests.push(api.get<ApiResponse<Event[]>>('/events', { params: { year: y, month: m } }));
|
||
}
|
||
} else {
|
||
// 如果没指定年月,拉取所有
|
||
requests.push(api.get<ApiResponse<Event[]>>('/events'));
|
||
}
|
||
|
||
const responses = await Promise.all(requests);
|
||
|
||
// 合并并去重(按 id)
|
||
const allEvents: Event[] = [];
|
||
const seenIds = new Set<string>();
|
||
|
||
for (const response of responses) {
|
||
const events = response.data.data || [];
|
||
for (const event of events) {
|
||
if (!seenIds.has(event.id)) {
|
||
seenIds.add(event.id);
|
||
allEvents.push(event);
|
||
}
|
||
}
|
||
}
|
||
|
||
return allEvents;
|
||
} catch (error) {
|
||
throw new Error(formatError(error));
|
||
}
|
||
},
|
||
|
||
async createEvent(event: Omit<Event, 'id' | 'createdAt' | 'updatedAt'>): Promise<void> {
|
||
try {
|
||
await api.post('/events', event);
|
||
} catch (error) {
|
||
throw new Error(formatError(error));
|
||
}
|
||
},
|
||
|
||
async updateEvent(id: string, event: Partial<Event>): Promise<void> {
|
||
try {
|
||
await api.put('/events', { ...event, id }, { params: { id } });
|
||
} catch (error) {
|
||
throw new Error(formatError(error));
|
||
}
|
||
},
|
||
|
||
async deleteEvent(id: string): Promise<void> {
|
||
try {
|
||
await api.delete('/events', { params: { id } });
|
||
} catch (error) {
|
||
throw new Error(formatError(error));
|
||
}
|
||
},
|
||
};
|