Switch frontend group and conversation requests to RESTful endpoints.

Update service API paths and message reply payload handling to match the backend route changes.

Made-with: Cursor
This commit is contained in:
2026-03-10 20:52:50 +08:00
parent be84c01abd
commit f10c9cc1d7
2 changed files with 105 additions and 148 deletions

View File

@@ -34,7 +34,7 @@ class MessageService {
page = 1,
pageSize = 20
): Promise<ConversationListResponse> {
const response = await api.get<ConversationListResponse>('/conversations/list', {
const response = await api.get<ConversationListResponse>('/conversations', {
page,
page_size: pageSize,
});
@@ -56,8 +56,7 @@ class MessageService {
private async refreshConversationDetailFromServer(id: string): Promise<ConversationDetailResponse> {
const response = await api.get<ConversationDetailResponse>(
'/conversations/get',
{ conversation_id: id }
`/conversations/${encodeURIComponent(id)}`
);
await saveConversationCache(response.data);
if (response.data.participants?.length) {
@@ -135,11 +134,11 @@ class MessageService {
/**
* 创建私聊会话
* POST /api/v1/conversations/create
* POST /api/v1/conversations
* @param userId 目标用户ID (UUID格式)
*/
async createConversation(userId: string): Promise<ConversationResponse> {
const response = await api.post<ConversationResponse>('/conversations/create', {
const response = await api.post<ConversationResponse>('/conversations', {
user_id: userId,
});
await saveConversationCache(response.data);
@@ -151,7 +150,7 @@ class MessageService {
/**
* 获取会话详情
* GET /api/v1/conversations/get?conversation_id=xxx
* GET /api/v1/conversations/:id
*/
async getConversationById(id: string): Promise<ConversationDetailResponse> {
const cached = await getConversationCache(id);
@@ -180,11 +179,10 @@ class MessageService {
/**
* 设置会话置顶
* POST /api/v1/conversations/set_pinned
* PUT /api/v1/conversations/:id/pinned
*/
async setConversationPinned(conversationId: string, isPinned: boolean): Promise<void> {
await api.post('/conversations/set_pinned', {
conversation_id: conversationId,
await api.put(`/conversations/${encodeURIComponent(conversationId)}/pinned`, {
is_pinned: isPinned,
});
}
@@ -193,7 +191,7 @@ class MessageService {
/**
* 获取消息列表(支持增量同步和历史消息加载)
* GET /api/v1/conversations/get_messages?conversation_id=xxx
* GET /api/v1/conversations/:id/messages
* @param conversationId 会话ID
* @param afterSeq 获取此序号之后的消息(增量同步,获取新消息)
* @param beforeSeq 获取此序号之前的历史消息(下拉加载更多,获取旧消息)
@@ -205,9 +203,7 @@ class MessageService {
beforeSeq?: number,
limit?: number
): Promise<MessageListResponse> {
const params: Record<string, any> = {
conversation_id: conversationId,
};
const params: Record<string, any> = {};
if (afterSeq !== undefined) {
params.after_seq = afterSeq;
}
@@ -220,7 +216,7 @@ class MessageService {
try {
const response = await api.get<any>(
'/conversations/get_messages',
`/conversations/${encodeURIComponent(conversationId)}/messages`,
params
);
@@ -255,8 +251,8 @@ class MessageService {
/**
* 发送消息(新格式)
* POST /api/v1/conversations/send_message
* Body: { detail_type, conversation_id, message }
* POST /api/v1/conversations/:id/messages
* Body: { detail_type, segments }
*/
async sendMessage(
conversationId: string,
@@ -271,32 +267,38 @@ class MessageService {
return this.sendMessageByAction(
detailType,
conversationId,
data.segments
data.segments,
data.reply_to_id
);
}
/**
* 通过 HTTP POST 发送消息(新格式)
* 请求格式: POST /api/v1/conversations/send_message
* Body: { "detail_type": "private", "conversation_id": "xxx", "segments": [...] }
* 请求格式: POST /api/v1/conversations/:id/messages
* Body: { "detail_type": "private", "segments": [...] }
* @param detailType 消息类型: 'private' 私聊, 'group' 群聊
* @param conversationId 会话ID
* @param message 消息段数组 [{"type": "text", "data": {"text": "xxx"}}]
* @param replyToId 回复的消息ID可选
* @returns 消息响应
*/
async sendMessageByAction(
detailType: 'private' | 'group',
conversationId: string,
message: MessageSegment[]
message: MessageSegment[],
replyToId?: string
): Promise<MessageResponse> {
try {
const body: Record<string, any> = {
detail_type: detailType,
segments: message,
};
if (replyToId) {
body.reply_to_id = replyToId;
}
const response = await api.post<MessageResponse>(
'/conversations/send_message',
{
detail_type: detailType,
conversation_id: conversationId,
segments: message,
}
`/conversations/${encodeURIComponent(conversationId)}/messages`,
body
);
return response.data;
} catch (error) {
@@ -393,14 +395,8 @@ class MessageService {
replyToId: string,
detailType: 'private' | 'group' = 'private'
): Promise<MessageResponse> {
// 添加回复段到消息开头
const replySegment: MessageSegment = {
type: 'reply',
data: {
id: replyToId,
},
};
return this.sendMessageByAction(detailType, conversationId, [replySegment, ...segments]);
// 使用 reply_to_id 参数发送回复消息
return this.sendMessageByAction(detailType, conversationId, segments, replyToId);
}
/**
@@ -426,13 +422,12 @@ class MessageService {
/**
* 标记已读
* POST /api/v1/conversations/mark_read
* POST /api/v1/conversations/:id/read
* @param conversationId 会话ID
* @param seq 已读到的消息序号
*/
async markAsRead(conversationId: string, seq: number): Promise<void> {
await api.post('/conversations/mark_read', {
conversation_id: conversationId,
await api.post(`/conversations/${encodeURIComponent(conversationId)}/read`, {
last_read_seq: Number(seq),
});
// 立即清零本地缓存中的 unread_count避免回到列表时仍显示旧红点
@@ -443,12 +438,10 @@ class MessageService {
/**
* 上报输入状态
* POST /api/v1/conversations/typing
* POST /api/v1/conversations/:id/typing
*/
async sendTyping(conversationId: string): Promise<void> {
await api.post('/conversations/typing', {
conversation_id: conversationId,
});
await api.post(`/conversations/${encodeURIComponent(conversationId)}/typing`);
}
/**