message.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import axios from 'axios';
  2. import type {
  3. MessageType, MessageStatus, UserMessage
  4. } from '../../share/types.ts';
  5. const API_BASE_URL = '/api';
  6. // 消息API响应类型
  7. export interface MessageResponse {
  8. message: string;
  9. data?: any;
  10. }
  11. export interface MessagesResponse {
  12. data: UserMessage[];
  13. pagination: {
  14. total: number;
  15. current: number;
  16. pageSize: number;
  17. totalPages: number;
  18. };
  19. }
  20. export interface UnreadCountResponse {
  21. count: number;
  22. }
  23. // 消息API
  24. export const MessageAPI = {
  25. // 获取消息列表
  26. getMessages: async (params?: {
  27. page?: number,
  28. pageSize?: number,
  29. type?: MessageType,
  30. status?: MessageStatus
  31. }): Promise<MessagesResponse> => {
  32. try {
  33. const response = await axios.get(`${API_BASE_URL}/messages`, { params });
  34. return response.data;
  35. } catch (error) {
  36. throw error;
  37. }
  38. },
  39. // 获取消息详情
  40. getMessage: async (id: number): Promise<MessageResponse> => {
  41. try {
  42. const response = await axios.get(`${API_BASE_URL}/messages/${id}`);
  43. return response.data;
  44. } catch (error) {
  45. throw error;
  46. }
  47. },
  48. // 发送消息
  49. sendMessage: async (data: {
  50. title: string,
  51. content: string,
  52. type: MessageType,
  53. receiver_ids: number[]
  54. }): Promise<MessageResponse> => {
  55. try {
  56. const response = await axios.post(`${API_BASE_URL}/messages`, data);
  57. return response.data;
  58. } catch (error) {
  59. throw error;
  60. }
  61. },
  62. // 删除消息(软删除)
  63. deleteMessage: async (id: number): Promise<MessageResponse> => {
  64. try {
  65. const response = await axios.delete(`${API_BASE_URL}/messages/${id}`);
  66. return response.data;
  67. } catch (error) {
  68. throw error;
  69. }
  70. },
  71. // 获取未读消息数量
  72. getUnreadCount: async (): Promise<UnreadCountResponse> => {
  73. try {
  74. const response = await axios.get(`${API_BASE_URL}/messages/count/unread`);
  75. return response.data;
  76. } catch (error) {
  77. throw error;
  78. }
  79. },
  80. // 标记消息为已读
  81. markAsRead: async (id: number): Promise<MessageResponse> => {
  82. try {
  83. const response = await axios.post(`${API_BASE_URL}/messages/${id}/read`);
  84. return response.data;
  85. } catch (error) {
  86. throw error;
  87. }
  88. }
  89. };