107 lines
2.7 KiB
JavaScript
107 lines
2.7 KiB
JavaScript
/**
|
||
* 聊天接口模块
|
||
* Requirements: 7.1, 7.2, 7.3
|
||
*/
|
||
|
||
import { get, post } from './request'
|
||
|
||
/**
|
||
* 获取会话列表
|
||
*
|
||
* @returns {Promise<Object>} 会话列表
|
||
*/
|
||
export async function getSessions() {
|
||
const response = await get('/chat/sessions')
|
||
return response
|
||
}
|
||
|
||
/**
|
||
* 获取消息列表
|
||
* WHEN a user opens chat page, THE XiangYi_MiniApp SHALL call endpoint and display message history
|
||
* Requirements: 7.1
|
||
*
|
||
* @param {number} sessionId - 会话ID
|
||
* @param {number} [pageIndex=1] - 页码
|
||
* @param {number} [pageSize=20] - 每页数量
|
||
* @returns {Promise<Object>} 消息列表
|
||
*/
|
||
export async function getMessages(sessionId, pageIndex = 1, pageSize = 20) {
|
||
const response = await get('/chat/messages', { sessionId, pageIndex, pageSize })
|
||
return response
|
||
}
|
||
|
||
/**
|
||
* 发送消息
|
||
* WHEN a user sends a text message, THE XiangYi_MiniApp SHALL call endpoint
|
||
* Requirements: 7.2
|
||
*
|
||
* @param {Object} data - 消息数据
|
||
* @param {number} data.sessionId - 会话ID
|
||
* @param {number} data.receiverId - 接收者ID
|
||
* @param {number} data.messageType - 消息类型:1文本 2语音 3图片
|
||
* @param {string} data.content - 消息内容
|
||
* @returns {Promise<Object>} 发送结果
|
||
*/
|
||
export async function sendMessage(data) {
|
||
const response = await post('/chat/send', data)
|
||
return response
|
||
}
|
||
|
||
/**
|
||
* 请求交换微信
|
||
* WHEN a user clicks "交换微信" button, THE XiangYi_MiniApp SHALL call endpoint
|
||
* Requirements: 7.3
|
||
*
|
||
* @param {number} sessionId - 会话ID
|
||
* @param {number} receiverId - 接收者ID
|
||
* @returns {Promise<Object>} 交换请求结果
|
||
*/
|
||
export async function exchangeWeChat(sessionId, receiverId) {
|
||
const response = await post('/chat/exchangeWeChat', { sessionId, receiverId })
|
||
return response
|
||
}
|
||
|
||
/**
|
||
* 请求交换照片
|
||
*
|
||
* @param {number} sessionId - 会话ID
|
||
* @param {number} receiverId - 接收者ID
|
||
* @returns {Promise<Object>} 交换请求结果
|
||
*/
|
||
export async function exchangePhoto(sessionId, receiverId) {
|
||
const response = await post('/chat/exchangePhoto', { sessionId, receiverId })
|
||
return response
|
||
}
|
||
|
||
/**
|
||
* 响应交换请求
|
||
*
|
||
* @param {number} requestMessageId - 请求消息ID
|
||
* @param {boolean} accept - 是否接受
|
||
* @returns {Promise<Object>} 响应结果
|
||
*/
|
||
export async function respondExchange(requestMessageId, accept) {
|
||
const response = await post('/chat/respondExchange', { requestMessageId, accept })
|
||
return response
|
||
}
|
||
|
||
/**
|
||
* 获取未读消息数
|
||
*
|
||
* @returns {Promise<Object>} 未读消息数
|
||
*/
|
||
export async function getUnreadCount() {
|
||
const response = await get('/chat/unreadCount')
|
||
return response
|
||
}
|
||
|
||
export default {
|
||
getSessions,
|
||
getMessages,
|
||
sendMessage,
|
||
exchangeWeChat,
|
||
exchangePhoto,
|
||
respondExchange,
|
||
getUnreadCount
|
||
}
|