统一使用index.ts中的全局axios配置 移除了所有重复的API_BASE_URL定义 简化了所有API调用路径格式 提高了代码一致性和可维护性 确保所有API功能保持正常
98 lines
2.1 KiB
TypeScript
98 lines
2.1 KiB
TypeScript
import axios from 'axios';
|
|
import type {
|
|
MessageType, MessageStatus, UserMessage
|
|
} from '../../share/types.ts';
|
|
|
|
// 消息API响应类型
|
|
export interface MessageResponse {
|
|
message: string;
|
|
data?: any;
|
|
}
|
|
|
|
export interface MessagesResponse {
|
|
data: UserMessage[];
|
|
pagination: {
|
|
total: number;
|
|
current: number;
|
|
pageSize: number;
|
|
totalPages: number;
|
|
};
|
|
}
|
|
|
|
export interface UnreadCountResponse {
|
|
count: number;
|
|
}
|
|
|
|
// 消息API
|
|
export const MessageAPI = {
|
|
// 获取消息列表
|
|
getMessages: async (params?: {
|
|
page?: number,
|
|
pageSize?: number,
|
|
type?: MessageType,
|
|
status?: MessageStatus
|
|
}): Promise<MessagesResponse> => {
|
|
try {
|
|
const response = await axios.get('/messages', { params });
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 获取消息详情
|
|
getMessage: async (id: number): Promise<MessageResponse> => {
|
|
try {
|
|
const response = await axios.get(`/messages/${id}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 发送消息
|
|
sendMessage: async (data: {
|
|
title: string,
|
|
content: string,
|
|
type: MessageType,
|
|
receiver_ids: number[]
|
|
}): Promise<MessageResponse> => {
|
|
try {
|
|
const response = await axios.post('/messages', data);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 删除消息(软删除)
|
|
deleteMessage: async (id: number): Promise<MessageResponse> => {
|
|
try {
|
|
const response = await axios.delete(`/messages/${id}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 获取未读消息数量
|
|
getUnreadCount: async (): Promise<UnreadCountResponse> => {
|
|
try {
|
|
const response = await axios.get('/messages/count/unread');
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 标记消息为已读
|
|
markAsRead: async (id: number): Promise<MessageResponse> => {
|
|
try {
|
|
const response = await axios.post(`/messages/${id}/read`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
};
|