/** * @description 学习历史相关API * @author yslg * @since 2025-10-27 */ import { api } from '@/apis/index'; import type { TbLearningHistory, LearningHistoryVO, LearningStatisticsVO, ResultDomain, PageDomain, PageRequest } from '@/types'; /** * 学习历史API服务 */ export const learningHistoryApi = { /** * 记录学习历史 * @param learningHistory 学习历史数据 * @returns Promise> */ async recordLearningHistory(learningHistory: TbLearningHistory): Promise> { const response = await api.post('/study/history/record', learningHistory, { showLoading: false // 禁用 loading 动画,避免影响用户体验 } as any); return response.data; }, /** * 批量记录学习历史 * @param historyList 学习历史列表 * @returns Promise> */ async batchRecordLearningHistory(historyList: TbLearningHistory[]): Promise> { const response = await api.post('/study/history/batch-record', historyList); return response.data; }, /** * 查询学习历史列表 * @param filter 过滤条件 * @returns Promise> */ async getLearningHistories(filter?: Partial): Promise> { const response = await api.post('/study/history/list', filter || {}); return response.data; }, /** * 分页查询学习历史 * @param pageRequest 分页查询请求 * @returns Promise>> */ async getLearningHistoriesPage(pageRequest: PageRequest): Promise>> { const response = await api.post>('/study/history/page', pageRequest); return response.data; }, /** * 根据ID查询学习历史 * @param id 历史记录ID * @returns Promise> */ async getLearningHistoryById(id: string): Promise> { const response = await api.get(`/study/history/${id}`); return response.data; }, /** * 获取当前用户的学习历史 * @param filter 过滤条件 * @returns Promise> */ async getCurrentUserLearningHistories(filter?: Partial): Promise> { const response = await api.post('/study/history/my-histories', filter || {}); return response.data; }, /** * 获取当前用户最近的学习历史 * @param limit 限制数量(默认10) * @returns Promise> */ async getRecentLearningHistories(limit = 10): Promise> { const response = await api.get(`/study/history/recent?limit=${limit}`); return response.data; }, /** * 获取用户学习统计(按时间范围) * @param userId 用户ID * @param startTime 开始时间(Date对象) * @param endTime 结束时间(Date对象) * @returns Promise> */ async getUserLearningStatistics(userId: string, startTime: Date, endTime: Date): Promise> { const response = await api.get( `/study/history/statistics?userId=${userId}&startTime=${startTime.getTime()}&endTime=${endTime.getTime()}` ); return response.data; }, /** * 获取用户学习统计(按周期) * @param userId 用户ID * @param periodType 周期类型(day/week/month) * @returns Promise> */ async getUserLearningStatisticsByPeriod(userId: string, periodType: 'day' | 'week' | 'month'): Promise> { const response = await api.get(`/study/history/statistics/${userId}/${periodType}`); return response.data; }, /** * 获取当前用户的学习统计 * @param periodType 周期类型(day/week/month) * @returns Promise> */ async getCurrentUserLearningStatistics(periodType: 'day' | 'week' | 'month'): Promise> { const response = await api.get(`/study/history/my-statistics/${periodType}`); return response.data; }, /** * 删除学习历史 * @param id 历史记录ID * @returns Promise> */ async deleteLearningHistory(id: string): Promise> { const response = await api.delete(`/study/history/${id}`); return response.data; }, /** * 批量删除学习历史 * @param ids 历史记录ID列表 * @returns Promise> */ async batchDeleteLearningHistories(ids: string[]): Promise> { const response = await api.delete('/study/history/batch', ids); return response.data; }, /** * 简化记录方法 - 观看新闻/资源 * @param userId 用户ID * @param resourceId 资源ID * @param duration 学习时长(秒) * @returns Promise> */ async recordResourceView(userId: string, resourceId: string, duration: number): Promise> { const learningHistory: TbLearningHistory = { userID: userId, resourceType: 1, // 1资源/新闻 resourceID: resourceId, duration: duration, deviceType: 'web' }; return this.recordLearningHistory(learningHistory); }, /** * 简化记录方法 - 学习课程 * @param userId 用户ID * @param courseId 课程ID * @param chapterId 章节ID(可选) * @param nodeId 节点ID(可选) * @param duration 学习时长(秒) * @returns Promise> */ async recordCourseLearn( userId: string, courseId: string, chapterId?: string, nodeId?: string, duration?: number ): Promise> { const learningHistory: TbLearningHistory = { userID: userId, resourceType: nodeId ? 4 : (chapterId ? 3 : 2), // 2课程 3章节 4节点 resourceID: nodeId || chapterId || courseId, courseID: courseId, chapterID: chapterId, nodeID: nodeId, duration: duration || 0, deviceType: 'web' }; return this.recordLearningHistory(learningHistory); }, /** * 健康检查 * @returns Promise> */ async health(): Promise> { const response = await api.get('/study/history/health'); return response.data; } };