/** * 数据管理器 - 管理课程和游戏数据 */ class DataManager { constructor() { this.courses = []; this.games = []; this.courseGames = new Map(); this.isDataLoaded = false; } /** * 从后端加载数据 */ async loadDataFromBackend() { try { console.log('正在从后端加载数据...'); const result = await window.apiService.getCompleteData(); if (result.data) { this.courses = result.data.courses || []; this.games = result.data.games || []; this.courseGames = new Map(Object.entries(result.data.courseGames || {})); this.isDataLoaded = true; console.log('数据加载成功:', { courses: this.courses.length, games: this.games.length, courseGames: this.courseGames.size }); // 更新全局配置 this.updateGlobalConfig(); } } catch (error) { console.error('从后端加载数据失败:', error); // 如果后端加载失败,使用本地数据 this.loadLocalData(); } } /** * 加载本地数据(备用方案) */ loadLocalData() { console.log('使用本地数据...'); if (window.GLOBAL_CONFIG) { this.courses = this.extractCoursesFromConfig(window.GLOBAL_CONFIG); this.games = this.extractGamesFromConfig(window.GLOBAL_CONFIG); this.buildCourseGamesMapping(); this.isDataLoaded = true; } } /** * 从全局配置中提取课程数据 */ extractCoursesFromConfig(config) { const courses = []; const { classObject } = config; Object.entries(classObject).forEach(([category, coursesData]) => { Object.entries(coursesData).forEach(([courseId, courseData]) => { const course = { courseId: courseId, categoryId: this.getCategoryId(category), title: courseData.title, description: courseData.courseVideo?.title || '', gradeLevel: this.extractGradeLevel(courseId), semester: this.extractSemester(courseId), courseVideoSrc: courseData.courseVideo?.src || '', courseVideoTitle: courseData.courseVideo?.title || '', thankName: courseData.thank?.name || '', isActive: 1 }; courses.push(course); }); }); return courses; } /** * 从全局配置中提取游戏数据 */ extractGamesFromConfig(config) { const games = []; const { classObject } = config; Object.entries(classObject).forEach(([category, coursesData]) => { Object.entries(coursesData).forEach(([courseId, courseData]) => { if (courseData.play && Array.isArray(courseData.play)) { courseData.play.forEach((game, index) => { const gameData = { courseId: this.getCourseIdByCourseId(courseId), gameTitle: game.title, gameUrl: game.url, gameType: 'play', categoryId: this.getCategoryId(category), isActive: 1 }; games.push(gameData); }); } }); }); return games; } /** * 构建课程-游戏映射 */ buildCourseGamesMapping() { this.courseGames.clear(); this.games.forEach(game => { if (game.courseId) { if (!this.courseGames.has(game.courseId)) { this.courseGames.set(game.courseId, []); } this.courseGames.get(game.courseId).push(game); } }); } /** * 更新全局配置 */ updateGlobalConfig() { if (window.GLOBAL_CONFIG) { // 更新课程列表 this.updateCourseList(); // 更新游戏列表 this.updateGameList(); } } /** * 更新课程列表 */ updateCourseList() { if (!window.GLOBAL_CONFIG) return; const { courseList } = window.GLOBAL_CONFIG; // 按分类组织课程 const coursesByCategory = { live: [], language: [], math: [] }; this.courses.forEach(course => { const category = this.getCategoryName(course.categoryId); if (coursesByCategory[category]) { coursesByCategory[category].push({ id: course.courseId, name: course.courseVideoTitle, des: course.description }); } }); // 更新全局配置 Object.keys(courseList).forEach(category => { if (coursesByCategory[category]) { // 这里可以根据年级和学期进一步组织 courseList[category].forEach(gradeArray => { gradeArray.forEach(semesterArray => { semesterArray.length = 0; // 清空现有数据 semesterArray.push(...coursesByCategory[category]); }); }); } }); } /** * 更新游戏列表 */ updateGameList() { if (!window.GLOBAL_CONFIG) return; const { playList } = window.GLOBAL_CONFIG; // 按分类组织游戏 const gamesByCategory = { live: [], language: [], math: [] }; this.games.forEach(game => { const category = this.getCategoryName(game.categoryId); if (gamesByCategory[category]) { gamesByCategory[category].push({ title: game.gameTitle, url: game.gameUrl }); } }); // 更新全局配置 Object.keys(playList).forEach(category => { if (gamesByCategory[category]) { playList[category] = gamesByCategory[category]; } }); } /** * 获取分类ID */ getCategoryId(categoryName) { const categoryMap = { 'live': 1, 'language': 2, 'math': 3 }; return categoryMap[categoryName] || 1; } /** * 获取分类名称 */ getCategoryName(categoryId) { const categoryMap = { 1: 'live', 2: 'language', 3: 'math' }; return categoryMap[categoryId] || 'live'; } /** * 提取年级信息 */ extractGradeLevel(courseId) { // 从课程ID中提取年级信息 if (courseId.includes('1')) return 1; if (courseId.includes('2')) return 2; if (courseId.includes('3')) return 3; return 1; } /** * 提取学期信息 */ extractSemester(courseId) { // 从课程ID中提取学期信息 if (courseId.includes('x')) return '下'; return '上'; } /** * 根据课程ID获取数据库ID */ getCourseIdByCourseId(courseId) { const course = this.courses.find(c => c.courseId === courseId); return course ? course.id : null; } /** * 根据课程ID获取课程 */ getCourseByCourseId(courseId) { return this.courses.find(c => c.courseId === courseId); } /** * 根据课程ID获取游戏列表 */ getGamesByCourseId(courseId) { const course = this.getCourseByCourseId(courseId); if (course && this.courseGames.has(course.id)) { return this.courseGames.get(course.id); } return []; } /** * 根据游戏标题获取游戏 */ getGameByTitle(gameTitle) { return this.games.find(g => g.gameTitle === gameTitle); } /** * 初始化数据 */ async init() { if (!this.isDataLoaded) { await this.loadDataFromBackend(); } } /** * 获取所有课程 */ getAllCourses() { return this.courses; } /** * 获取所有游戏 */ getAllGames() { return this.games; } /** * 检查数据是否已加载 */ isLoaded() { return this.isDataLoaded; } } // 创建全局数据管理器实例 window.dataManager = new DataManager();