import apiClient from './index.js' /** * 通用的获取所有分页数据的函数 * @param {Function} apiCall - API调用函数 * @param {Object} params - 查询参数 * @returns {Promise} 所有页面的数据数组 */ const fetchAllPages = async (apiCall, params = {}) => { let allData = [] let page = 1 let hasNext = true console.log('Starting to fetch all pages for API call...') while (hasNext) { try { const currentParams = { ...params, page, page_size: 100 } // 每页100条,减少请求次数 console.log(`Fetching page ${page} with params:`, currentParams) const response = await apiCall(currentParams) if (response && response.results) { // 分页格式响应 allData = [...allData, ...response.results] hasNext = !!response.next console.log(`Page ${page}: Got ${response.results.length} items, total so far: ${allData.length}, hasNext: ${hasNext}`) } else if (Array.isArray(response)) { // 非分页格式响应(向后兼容) allData = response hasNext = false console.log('Non-paginated response detected, got all data at once:', allData.length, 'items') } else { console.log('Unexpected response format, stopping pagination') hasNext = false } page++ // 安全检查:防止无限循环 if (page > 100) { console.warn('Reached maximum page limit (100), stopping pagination') break } } catch (error) { console.error(`Error fetching page ${page}:`, error) throw error } } console.log(`Finished fetching all pages. Total items: ${allData.length}`) return allData } // API端点定义 const apiEndpoints = { STORIES: { LIST: '/api/stories/', DETAIL: (storyId) => `/api/stories/${storyId}/`, UPDATE: (storyId) => `/api/stories/${storyId}/`, DELETE: (storyId) => `/api/stories/${storyId}/` } } /** * 单页API调用函数(内部使用) * @param {Object} params - 查询参数(包含分页参数) * @returns {Promise} API响应 */ const getStoriesPage = async (params = {}) => { const response = await apiClient.get(apiEndpoints.STORIES.LIST, { params }) return response.data } /** * 获取所有系列任务 * @param {Object} params - 查询参数 * @returns {Promise} 系列任务列表(完整数据,已处理分页) */ export const getStories = async (params = {}) => { try { // 构建查询参数,过滤掉null和undefined值 const queryParams = {} Object.entries(params).forEach(([key, value]) => { if (value !== null && value !== undefined) { queryParams[key] = value } }) console.log('Stories API request params:', queryParams) // 使用通用分页函数获取所有数据 const allStories = await fetchAllPages(getStoriesPage, queryParams) console.log('Stories API final result:', { totalCount: allStories.length, sampleData: allStories.slice(0, 3) // 显示前3条数据作为样例 }) return allStories } catch (error) { console.error('Failed to fetch stories:', error) throw error } } /** * 根据ID获取系列任务 * @param {number} storyId - 系列任务ID * @returns {Promise} 系列任务详情 */ export const getStoryById = async (storyId) => { try { const response = await apiClient.get(apiEndpoints.STORIES.DETAIL(storyId)) console.log('Story detail API response:', response.data) return response.data } catch (error) { console.error('Failed to fetch story detail:', error) // 提供更详细的错误信息 if (error.response) { // 服务器响应了错误状态码 throw new Error(`获取系列任务详情失败: ${error.response.status} - ${error.response.statusText}`) } else if (error.request) { // 请求已发出但没有收到响应 throw new Error('网络错误: 无法连接到服务器') } else { // 其他错误 throw new Error(`请求配置错误: ${error.message}`) } } } /** * 创建新的系列任务 * @param {Object} storyData - 系列任务数据 * @returns {Promise} 创建的系列任务 */ export const createStory = async (storyData) => { try { const response = await apiClient.post(apiEndpoints.STORIES.LIST, storyData) console.log('Create story API response:', response.data) return response.data } catch (error) { console.error('Failed to create story:', error) // 提供更详细的错误信息 if (error.response) { // 服务器响应了错误状态码 throw new Error(`创建系列任务失败: ${error.response.status} - ${error.response.statusText}`) } else if (error.request) { // 请求已发出但没有收到响应 throw new Error('网络错误: 无法连接到服务器') } else { // 其他错误 throw new Error(`请求配置错误: ${error.message}`) } } } /** * 更新系列任务 * @param {number} storyId - 系列任务ID * @param {Object} storyData - 更新的系列任务数据 * @returns {Promise} 更新后的系列任务 */ export const updateStory = async (storyId, storyData) => { try { const response = await apiClient.patch(apiEndpoints.STORIES.UPDATE(storyId), storyData) console.log('Update story API response:', response.data) return response.data } catch (error) { console.error('Failed to update story:', error) // 提供更详细的错误信息 if (error.response) { // 服务器响应了错误状态码 throw new Error(`更新系列任务失败: ${error.response.status} - ${error.response.statusText}`) } else if (error.request) { // 请求已发出但没有收到响应 throw new Error('网络错误: 无法连接到服务器') } else { // 其他错误 throw new Error(`请求配置错误: ${error.message}`) } } } /** * 删除系列任务 * @param {number} storyId - 系列任务ID * @returns {Promise} 删除结果 */ export const deleteStory = async (storyId) => { try { const response = await apiClient.delete(apiEndpoints.STORIES.DELETE(storyId)) console.log('Delete story API response:', response.data) return response.data } catch (error) { console.error('Failed to delete story:', error) // 提供更详细的错误信息 if (error.response) { // 服务器响应了错误状态码 throw new Error(`删除系列任务失败: ${error.response.status} - ${error.response.statusText}`) } else if (error.request) { // 请求已发出但没有收到响应 throw new Error('网络错误: 无法连接到服务器') } else { // 其他错误 throw new Error(`请求配置错误: ${error.message}`) } } }