message.ts 2.1 KB

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