feat: 同步dev分支并保留当前修改
This commit is contained in:
@@ -294,12 +294,57 @@ class PostService {
|
||||
params: CursorPaginationRequest = {}
|
||||
): Promise<CursorPaginationResponse<Post>> {
|
||||
try {
|
||||
const response = await api.get<CursorPaginationResponse<Post>>('/posts', {
|
||||
cursor: params.cursor,
|
||||
page_size: params.page_size,
|
||||
post_type: params.post_type,
|
||||
});
|
||||
return response.data;
|
||||
// 构建请求参数
|
||||
// 后端使用 `tab` 参数区分帖子类型,使用 `cursor` 参数启用游标分页
|
||||
// 始终传递 cursor 参数(空字符串表示首次请求),确保使用游标分页
|
||||
const requestParams: Record<string, any> = {
|
||||
cursor: params.cursor || '', // 空字符串表示首次请求
|
||||
page_size: params.page_size,
|
||||
};
|
||||
|
||||
// 将 post_type 映射为后端的 tab 参数
|
||||
if (params.post_type) {
|
||||
requestParams.tab = params.post_type;
|
||||
}
|
||||
|
||||
const response = await api.get<any>('/posts', requestParams);
|
||||
|
||||
const data = response.data;
|
||||
|
||||
// 兼容两种 API 响应格式:
|
||||
// 1. 游标分页格式:{ items, next_cursor, prev_cursor, has_more }
|
||||
// 2. 传统分页格式:{ list, total, page, page_size, total_pages }
|
||||
if (data && typeof data === 'object') {
|
||||
// 游标分页格式
|
||||
if (Array.isArray(data.items)) {
|
||||
return {
|
||||
items: data.items,
|
||||
next_cursor: data.next_cursor ?? null,
|
||||
prev_cursor: data.prev_cursor ?? null,
|
||||
has_more: data.has_more ?? false,
|
||||
};
|
||||
}
|
||||
// 传统分页格式 - 转换为游标格式
|
||||
if (Array.isArray(data.list)) {
|
||||
const currentPage = data.page || 1;
|
||||
const totalPages = data.total_pages || 1;
|
||||
return {
|
||||
items: data.list,
|
||||
// 使用页码作为游标
|
||||
next_cursor: currentPage < totalPages ? String(currentPage + 1) : null,
|
||||
prev_cursor: currentPage > 1 ? String(currentPage - 1) : null,
|
||||
has_more: currentPage < totalPages,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 默认返回空数据
|
||||
return {
|
||||
items: [],
|
||||
next_cursor: null,
|
||||
prev_cursor: null,
|
||||
has_more: false,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('获取帖子列表失败:', error);
|
||||
return {
|
||||
@@ -322,11 +367,41 @@ class PostService {
|
||||
params: CursorPaginationRequest = {}
|
||||
): Promise<CursorPaginationResponse<Post>> {
|
||||
try {
|
||||
const response = await api.get<CursorPaginationResponse<Post>>('/posts/search', {
|
||||
const response = await api.get<any>('/posts/search', {
|
||||
...params,
|
||||
keyword: query,
|
||||
});
|
||||
return response.data;
|
||||
|
||||
const data = response.data;
|
||||
|
||||
// 兼容两种 API 响应格式
|
||||
if (data && typeof data === 'object') {
|
||||
if (Array.isArray(data.items)) {
|
||||
return {
|
||||
items: data.items,
|
||||
next_cursor: data.next_cursor ?? null,
|
||||
prev_cursor: data.prev_cursor ?? null,
|
||||
has_more: data.has_more ?? false,
|
||||
};
|
||||
}
|
||||
if (Array.isArray(data.list)) {
|
||||
const currentPage = data.page || 1;
|
||||
const totalPages = data.total_pages || 1;
|
||||
return {
|
||||
items: data.list,
|
||||
next_cursor: currentPage < totalPages ? String(currentPage + 1) : null,
|
||||
prev_cursor: currentPage > 1 ? String(currentPage - 1) : null,
|
||||
has_more: currentPage < totalPages,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
items: [],
|
||||
next_cursor: null,
|
||||
prev_cursor: null,
|
||||
has_more: false,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('搜索帖子失败:', error);
|
||||
return {
|
||||
@@ -349,11 +424,41 @@ class PostService {
|
||||
params: CursorPaginationRequest = {}
|
||||
): Promise<CursorPaginationResponse<Post>> {
|
||||
try {
|
||||
const response = await api.get<CursorPaginationResponse<Post>>(
|
||||
const response = await api.get<any>(
|
||||
`/users/${userId}/posts`,
|
||||
params
|
||||
);
|
||||
return response.data;
|
||||
|
||||
const data = response.data;
|
||||
|
||||
// 兼容两种 API 响应格式
|
||||
if (data && typeof data === 'object') {
|
||||
if (Array.isArray(data.items)) {
|
||||
return {
|
||||
items: data.items,
|
||||
next_cursor: data.next_cursor ?? null,
|
||||
prev_cursor: data.prev_cursor ?? null,
|
||||
has_more: data.has_more ?? false,
|
||||
};
|
||||
}
|
||||
if (Array.isArray(data.list)) {
|
||||
const currentPage = data.page || 1;
|
||||
const totalPages = data.total_pages || 1;
|
||||
return {
|
||||
items: data.list,
|
||||
next_cursor: currentPage < totalPages ? String(currentPage + 1) : null,
|
||||
prev_cursor: currentPage > 1 ? String(currentPage - 1) : null,
|
||||
has_more: currentPage < totalPages,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
items: [],
|
||||
next_cursor: null,
|
||||
prev_cursor: null,
|
||||
has_more: false,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('获取用户帖子列表失败:', error);
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user