chat-api.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * 聊天相关API接口封装
  3. */
  4. const CHAT_BASE_URL = 'https://api.zhongruanke.cn/api/chat';
  5. /**
  6. * 统一的聊天请求封装
  7. */
  8. const chatRequest = (options) => {
  9. return new Promise((resolve, reject) => {
  10. uni.request({
  11. url: options.url,
  12. method: options.method || 'GET',
  13. data: options.data || {},
  14. header: {
  15. 'Content-Type': 'application/json'
  16. },
  17. success: (res) => {
  18. if (res.statusCode === 200) {
  19. resolve(res.data);
  20. } else {
  21. reject(res);
  22. }
  23. },
  24. fail: (err) => {
  25. console.error('请求失败:', err);
  26. reject(err);
  27. }
  28. });
  29. });
  30. };
  31. /**
  32. * 获取用户会话列表
  33. */
  34. export function getConversations(userId, pageNum = 1, pageSize = 20) {
  35. return chatRequest({
  36. url: `${CHAT_BASE_URL}/conversations?userId=${userId}&pageNum=${pageNum}&pageSize=${pageSize}`,
  37. method: 'GET'
  38. });
  39. }
  40. /**
  41. * 获取会话消息列表
  42. */
  43. export function getMessages(userId, targetUserId, page = 0, size = 20) {
  44. return chatRequest({
  45. url: `${CHAT_BASE_URL}/messages?userId=${userId}&targetUserId=${targetUserId}&page=${page}&size=${size}`,
  46. method: 'GET'
  47. });
  48. }
  49. /**
  50. * 标记消息为已读
  51. */
  52. export function markAsRead(userId, targetUserId) {
  53. return chatRequest({
  54. url: `${CHAT_BASE_URL}/read`,
  55. method: 'POST',
  56. data: {
  57. userId,
  58. targetUserId
  59. }
  60. });
  61. }
  62. /**
  63. * 获取未读消息总数
  64. */
  65. export function getUnreadCount(userId) {
  66. return chatRequest({
  67. url: `${CHAT_BASE_URL}/unread/count?userId=${userId}`,
  68. method: 'GET'
  69. });
  70. }
  71. /**
  72. * 删除会话
  73. */
  74. export function deleteConversation(userId, targetUserId) {
  75. return chatRequest({
  76. url: `${CHAT_BASE_URL}/conversation`,
  77. method: 'DELETE',
  78. data: {
  79. userId,
  80. targetUserId
  81. }
  82. });
  83. }
  84. /**
  85. * 检查用户在线状态
  86. */
  87. export function checkOnlineStatus(userId) {
  88. return chatRequest({
  89. url: `${CHAT_BASE_URL}/online/status?userId=${userId}`,
  90. method: 'GET'
  91. });
  92. }
  93. /**
  94. * 上传聊天图片/文件
  95. */
  96. export function uploadChatMedia(filePath) {
  97. return new Promise((resolve, reject) => {
  98. uni.uploadFile({
  99. url: 'https://api.zhongruanke.cn/api/upload', // 使用homePage服务的上传接口
  100. filePath: filePath,
  101. name: 'file',
  102. success: (res) => {
  103. try {
  104. const data = JSON.parse(res.data);
  105. resolve(data);
  106. } catch (e) {
  107. reject(e);
  108. }
  109. },
  110. fail: reject
  111. });
  112. });
  113. }
  114. /**
  115. * 查询消息状态(用于轮询确认)
  116. */
  117. export function getMessageStatus(messageId) {
  118. return chatRequest({
  119. url: `${CHAT_BASE_URL}/message/status?messageId=${messageId}`,
  120. method: 'GET'
  121. });
  122. }
  123. export default {
  124. getConversations,
  125. getMessages,
  126. markAsRead,
  127. getUnreadCount,
  128. deleteConversation,
  129. checkOnlineStatus,
  130. uploadChatMedia,
  131. getMessageStatus
  132. };