Files
Toolbox/apps/app_calendar_reminder/api.ts
T
yangxiangyuan 154b79c8f1 feat: 新增短链接工具,优化日历提醒功能
1. 新增完整短链接工具服务,包括前端页面、后端API与数据库支持
2. 新增移动端日历提醒工具与鉴权配置
3. 优化日历事件编辑与展示:
   - 添加事件循环重复设置
   - 新增全天事件支持
   - 时间选择器自动对齐15分钟间隔
   - 保存状态加载动画
   - 事件卡片显示重复规则
4. 更新API基础地址与路由配置
5. 新增SSL证书文件与相关工具静态资源
2026-07-28 16:23:37 +08:00

104 lines
3.1 KiB
TypeScript
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.
import axios from 'axios';
import { Event, ApiResponse } from './types';
const API_BASE_URL = 'https://traesite.umersoft.com:8975/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));
}
},
};