messages.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import axios from 'axios';
  2. import type { UserMessage, Message } from '../../share/types.ts';
  3. const API_BASE_URL = '/api';
  4. interface MessagesResponse {
  5. data: UserMessage[];
  6. pagination: {
  7. total: number;
  8. current: number;
  9. pageSize: number;
  10. totalPages: number;
  11. };
  12. }
  13. interface MessageResponse {
  14. data: Message;
  15. message?: string;
  16. }
  17. interface MessageCountResponse {
  18. count: number;
  19. }
  20. export const MessageAPI = {
  21. getMessages: async (params?: {
  22. page?: number,
  23. pageSize?: number,
  24. type?: string,
  25. status?: string,
  26. search?: string
  27. }): Promise<MessagesResponse> => {
  28. try {
  29. const response = await axios.get(`${API_BASE_URL}/messages`, { params });
  30. return response.data;
  31. } catch (error) {
  32. throw error;
  33. }
  34. },
  35. sendMessage: async (data: {
  36. title: string,
  37. content: string,
  38. type: string,
  39. receiver_ids: number[]
  40. }): Promise<MessageResponse> => {
  41. try {
  42. const response = await axios.post(`${API_BASE_URL}/messages`, data);
  43. return response.data;
  44. } catch (error) {
  45. throw error;
  46. }
  47. },
  48. getUnreadCount: async (): Promise<MessageCountResponse> => {
  49. try {
  50. const response = await axios.get(`${API_BASE_URL}/messages/count/unread`);
  51. return response.data;
  52. } catch (error) {
  53. throw error;
  54. }
  55. },
  56. markAsRead: async (id: number): Promise<MessageResponse> => {
  57. try {
  58. const response = await axios.post(`${API_BASE_URL}/messages/${id}/read`);
  59. return response.data;
  60. } catch (error) {
  61. throw error;
  62. }
  63. },
  64. deleteMessage: async (id: number): Promise<MessageResponse> => {
  65. try {
  66. const response = await axios.delete(`${API_BASE_URL}/messages/${id}`);
  67. return response.data;
  68. } catch (error) {
  69. throw error;
  70. }
  71. }
  72. };