| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import axios from 'axios';
- import type { UserMessage, Message } from '../../share/types.ts';
- const API_BASE_URL = '/api';
- interface MessagesResponse {
- data: UserMessage[];
- pagination: {
- total: number;
- current: number;
- pageSize: number;
- totalPages: number;
- };
- }
- interface MessageResponse {
- data: Message;
- message?: string;
- }
- interface MessageCountResponse {
- count: number;
- }
- export const MessageAPI = {
- getMessages: async (params?: {
- page?: number,
- pageSize?: number,
- type?: string,
- status?: string,
- search?: string
- }): Promise<MessagesResponse> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/messages`, { params });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- sendMessage: async (data: {
- title: string,
- content: string,
- type: string,
- receiver_ids: number[]
- }): Promise<MessageResponse> => {
- try {
- const response = await axios.post(`${API_BASE_URL}/messages`, data);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- getUnreadCount: async (): Promise<MessageCountResponse> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/messages/count/unread`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- markAsRead: async (id: number): Promise<MessageResponse> => {
- try {
- const response = await axios.post(`${API_BASE_URL}/messages/${id}/read`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- deleteMessage: async (id: number): Promise<MessageResponse> => {
- try {
- const response = await axios.delete(`${API_BASE_URL}/messages/${id}`);
- return response.data;
- } catch (error) {
- throw error;
- }
- }
- };
|