205 lines
6.7 KiB
TypeScript
205 lines
6.7 KiB
TypeScript
|
|
/**
|
|||
|
|
* @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<ResultDomain<TbLearningHistory>>
|
|||
|
|
*/
|
|||
|
|
async recordLearningHistory(learningHistory: TbLearningHistory): Promise<ResultDomain<TbLearningHistory>> {
|
|||
|
|
const response = await api.post<TbLearningHistory>('/study/history/record', learningHistory, {
|
|||
|
|
showLoading: false // 禁用 loading 动画,避免影响用户体验
|
|||
|
|
} as any);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 批量记录学习历史
|
|||
|
|
* @param historyList 学习历史列表
|
|||
|
|
* @returns Promise<ResultDomain<boolean>>
|
|||
|
|
*/
|
|||
|
|
async batchRecordLearningHistory(historyList: TbLearningHistory[]): Promise<ResultDomain<boolean>> {
|
|||
|
|
const response = await api.post<boolean>('/study/history/batch-record', historyList);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 查询学习历史列表
|
|||
|
|
* @param filter 过滤条件
|
|||
|
|
* @returns Promise<ResultDomain<LearningHistoryVO[]>>
|
|||
|
|
*/
|
|||
|
|
async getLearningHistories(filter?: Partial<TbLearningHistory>): Promise<ResultDomain<LearningHistoryVO[]>> {
|
|||
|
|
const response = await api.post<LearningHistoryVO[]>('/study/history/list', filter || {});
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 分页查询学习历史
|
|||
|
|
* @param pageRequest 分页查询请求
|
|||
|
|
* @returns Promise<ResultDomain<PageDomain<LearningHistoryVO>>>
|
|||
|
|
*/
|
|||
|
|
async getLearningHistoriesPage(pageRequest: PageRequest<TbLearningHistory>): Promise<ResultDomain<PageDomain<LearningHistoryVO>>> {
|
|||
|
|
const response = await api.post<PageDomain<LearningHistoryVO>>('/study/history/page', pageRequest);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 根据ID查询学习历史
|
|||
|
|
* @param id 历史记录ID
|
|||
|
|
* @returns Promise<ResultDomain<TbLearningHistory>>
|
|||
|
|
*/
|
|||
|
|
async getLearningHistoryById(id: string): Promise<ResultDomain<TbLearningHistory>> {
|
|||
|
|
const response = await api.get<TbLearningHistory>(`/study/history/${id}`);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取当前用户的学习历史
|
|||
|
|
* @param filter 过滤条件
|
|||
|
|
* @returns Promise<ResultDomain<LearningHistoryVO[]>>
|
|||
|
|
*/
|
|||
|
|
async getCurrentUserLearningHistories(filter?: Partial<TbLearningHistory>): Promise<ResultDomain<LearningHistoryVO[]>> {
|
|||
|
|
const response = await api.post<LearningHistoryVO[]>('/study/history/my-histories', filter || {});
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取当前用户最近的学习历史
|
|||
|
|
* @param limit 限制数量(默认10)
|
|||
|
|
* @returns Promise<ResultDomain<LearningHistoryVO[]>>
|
|||
|
|
*/
|
|||
|
|
async getRecentLearningHistories(limit = 10): Promise<ResultDomain<LearningHistoryVO[]>> {
|
|||
|
|
const response = await api.get<LearningHistoryVO[]>(`/study/history/recent?limit=${limit}`);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取用户学习统计(按时间范围)
|
|||
|
|
* @param userId 用户ID
|
|||
|
|
* @param startTime 开始时间(Date对象)
|
|||
|
|
* @param endTime 结束时间(Date对象)
|
|||
|
|
* @returns Promise<ResultDomain<LearningStatisticsVO>>
|
|||
|
|
*/
|
|||
|
|
async getUserLearningStatistics(userId: string, startTime: Date, endTime: Date): Promise<ResultDomain<LearningStatisticsVO>> {
|
|||
|
|
const response = await api.get<LearningStatisticsVO>(
|
|||
|
|
`/study/history/statistics?userId=${userId}&startTime=${startTime.getTime()}&endTime=${endTime.getTime()}`
|
|||
|
|
);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取用户学习统计(按周期)
|
|||
|
|
* @param userId 用户ID
|
|||
|
|
* @param periodType 周期类型(day/week/month)
|
|||
|
|
* @returns Promise<ResultDomain<LearningStatisticsVO>>
|
|||
|
|
*/
|
|||
|
|
async getUserLearningStatisticsByPeriod(userId: string, periodType: 'day' | 'week' | 'month'): Promise<ResultDomain<LearningStatisticsVO>> {
|
|||
|
|
const response = await api.get<LearningStatisticsVO>(`/study/history/statistics/${userId}/${periodType}`);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取当前用户的学习统计
|
|||
|
|
* @param periodType 周期类型(day/week/month)
|
|||
|
|
* @returns Promise<ResultDomain<LearningStatisticsVO>>
|
|||
|
|
*/
|
|||
|
|
async getCurrentUserLearningStatistics(periodType: 'day' | 'week' | 'month'): Promise<ResultDomain<LearningStatisticsVO>> {
|
|||
|
|
const response = await api.get<LearningStatisticsVO>(`/study/history/my-statistics/${periodType}`);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 删除学习历史
|
|||
|
|
* @param id 历史记录ID
|
|||
|
|
* @returns Promise<ResultDomain<boolean>>
|
|||
|
|
*/
|
|||
|
|
async deleteLearningHistory(id: string): Promise<ResultDomain<boolean>> {
|
|||
|
|
const response = await api.delete<boolean>(`/study/history/${id}`);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 批量删除学习历史
|
|||
|
|
* @param ids 历史记录ID列表
|
|||
|
|
* @returns Promise<ResultDomain<boolean>>
|
|||
|
|
*/
|
|||
|
|
async batchDeleteLearningHistories(ids: string[]): Promise<ResultDomain<boolean>> {
|
|||
|
|
const response = await api.delete<boolean>('/study/history/batch', ids);
|
|||
|
|
return response.data;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 简化记录方法 - 观看新闻/资源
|
|||
|
|
* @param userId 用户ID
|
|||
|
|
* @param resourceId 资源ID
|
|||
|
|
* @param duration 学习时长(秒)
|
|||
|
|
* @returns Promise<ResultDomain<TbLearningHistory>>
|
|||
|
|
*/
|
|||
|
|
async recordResourceView(userId: string, resourceId: string, duration: number): Promise<ResultDomain<TbLearningHistory>> {
|
|||
|
|
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<ResultDomain<TbLearningHistory>>
|
|||
|
|
*/
|
|||
|
|
async recordCourseLearn(
|
|||
|
|
userId: string,
|
|||
|
|
courseId: string,
|
|||
|
|
chapterId?: string,
|
|||
|
|
nodeId?: string,
|
|||
|
|
duration?: number
|
|||
|
|
): Promise<ResultDomain<TbLearningHistory>> {
|
|||
|
|
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<ResultDomain<string>>
|
|||
|
|
*/
|
|||
|
|
async health(): Promise<ResultDomain<string>> {
|
|||
|
|
const response = await api.get<string>('/study/history/health');
|
|||
|
|
return response.data;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|