messages.ts 1.7 KB

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