import store from '@/store'; const exam = { namespaced: true, state: { started: false, // 考试状态 usedTime: 0, // 考试当前所用时间 totalTime: 0, // 考试总时间 timeInterval: null, // 计时器 suspend: false, // 暂停 ruleList: [], // 考试规则列表 courseDetail: {}, // 课程详情 courseList: [] // 课程列表 }, getters: { started: (state) => { return state.started; }, usedTime: (state) => { return state.usedTime; }, totalTime: (state) => { return state.totalTime; }, suspend: (state) => { return state.suspend; } }, mutations: { setStarted: (state, started) => { state.started = started; }, setSuspend: (state, suspend) => { state.suspend = suspend; }, setUsedTime: (state, usedTime) => { state.usedTime = usedTime; }, setTotalTime: (state, totalTime) => { state.totalTime = totalTime; }, countUsedTime: (state) => { if (state.timeInterval) { clearInterval(state.timeInterval); state.timeInterval = null; } state.timeInterval = setInterval(() => { if (!state.suspend) { state.usedTime++; } }, 1000); }, stopCountTime: (state) => { if (state.timeInterval) { clearInterval(state.timeInterval); state.timeInterval = null; } }, setRuleList: (state, ruleList) => { state.ruleList = ruleList; }, setCourseDetail: (state, courseDetail) => { state.courseDetail = courseDetail; }, setCourse: (state, courseList) => { state.courseList = courseList; } }, actions: { start({ commit }) { commit('setStarted', true); commit('setSuspend', false); commit('setUsedTime', 0); commit('countUsedTime'); }, over({ commit }) { commit('setStarted', false); commit('setSuspend', true); commit('setUsedTime', 0); commit('stopCountTime'); }, isOver() { const trainingList = store.getters['trainingList/trainingList']; const doList = trainingList.filter(elem => { if (elem.usedTime !== undefined) return true; }); if (doList.length >= trainingList.length) { return true; } }, setSuspend({ commit }, suspend) { commit('setSuspend', suspend); }, setUsedTime({ state }, usedTime) { if (usedTime) { state.usedTime = usedTime; } }, setTotalTime({ commit }, totalTime) { if (totalTime) { commit('setTotalTime', totalTime); } }, countUsedTime({ commit }) { commit('countUsedTime'); }, setRuleList({ commit }, ruleList) { commit('setRuleList', ruleList); }, setCourseDetail({ commit }, courseDetail) { commit('setCourseDetail', courseDetail); }, setCourse({ commit }, course) { commit('setCourse', course); } } }; export default exam;