import { ref, computed, inject } from 'vue'; import { GetTaskTeachingTypeList, GetTaskModeList, GetTeacherListByCourseID, GetCourseStudentList } from '@/api/select'; import { GetTreeNodeInfo_BookChapterStruct, GetTaskInfo, GetCSItem } from '@/api/course'; import { useRoute } from 'vue-router/composables'; import { Message } from 'element-ui'; import { fileUpload } from '@/api/app'; import { fileTypeSizeLimit } from '@/utils/validate'; import i18n from '@/locales/i18n'; const allStudent = { student_id: '', student_name: i18n.t('Key110') }; export function useInit(form, student_list, custom_student_list, { liveForm, basicForm, courseForm }) { const route = useRoute(); const { params, query } = route; const task_id = query.task_id; const is_template = 'is_template' in query ? JSON.parse(query.is_template) : false; const id = params.id; const cs_item_id = params.curItemID; const time_type = ref(params.time_type); let cs_item_begin_time = ref(''); let cs_item_end_time = ref(''); // 处理任务信息 function handleTaskInfo({ begin_time, end_time, time_type: tType, teaching_type, name, teacher_id, teacher_name, content, courseware_list, accessory_list, task_mode, is_enable_homework, is_enable_message, courseware_visible_mode, zhibo_record_mode, cs_item_begin_time: csItemBTime, cs_item_end_time: csItemETime, is_custom_student, custom_student_list: cusStudentList }) { if (begin_time.length > 0) { const begin = begin_time.split(' '); form.value.begin_date = begin[0]; const bTime = begin[1].split(':'); form.value.begin_date_hour = bTime[0]; form.value.begin_date_minute = bTime[1]; } if (end_time.length > 0) { const end = end_time.split(' '); form.value.end_date = end[0]; const eTime = end[1].split(':'); form.value.end_date_hour = eTime[0]; form.value.end_date_minute = eTime[1]; } cs_item_begin_time.value = csItemBTime; cs_item_end_time.value = csItemETime; time_type.value = tType; form.value.teaching_type = teaching_type; form.value.name = name; form.value.teacher_id = teacher_id; form.value.teacher_name = teacher_name; form.value.content = content; if (teaching_type === 10) { liveForm.value.coursewareInfo = courseware_list; liveForm.value.file_info_list = accessory_list; liveForm.value.courseware_visible_mode = courseware_visible_mode; liveForm.value.zhibo_record_mode = zhibo_record_mode; } if (teaching_type === 11) { courseForm.value.coursewareInfo = courseware_list; courseForm.value.file_info_list = accessory_list; courseForm.value.task_mode = task_mode; } if (teaching_type === 12) { basicForm.value.file_info_list = accessory_list; basicForm.value.is_enable_homework = is_enable_homework === 'true'; basicForm.value.is_enable_message = is_enable_message === 'true'; } if (is_custom_student === 'true') custom_student_list.value = cusStudentList; } let teacher_list = ref([]); // 课节教师列表 // 初始化时需要同步的请求 let initRequest = computed(() => { let list = []; let dataList = []; if (task_id) { list.push(GetTaskInfo({ id: task_id })); dataList.push('taskInfo'); } if (!is_template) { list.push(GetTeacherListByCourseID({ course_id: id })); list.push(GetCourseStudentList({ course_id: id, audit_status_list: [1], pay_status: 1 })); dataList.push('teacher_list', 'student_list'); } return { promiseList: list, dataList }; }); Promise.all(initRequest.value.promiseList).then((values) => { initRequest.value.dataList.forEach((item, i) => { if (item === 'taskInfo') return handleTaskInfo(values[i]); if (item === 'teacher_list') return (teacher_list.value = values[i].teacher_list); if (item === 'student_list') return (student_list.value = values[i].student_list); }); if (form.value.teacher_id.length > 0) { // 如果课程删除了任务中的老师,任务中的老师需要添加进教师列表 let isHas = teacher_list.value.some(({ teacher_id }) => { if (teacher_id === form.value.teacher_id) return true; }); if (!isHas) { teacher_list.value.push({ teacher_id: form.value.teacher_id, teacher_name: form.value.teacher_name }); } } if (student_list.value.length > 0 && custom_student_list.value.length <= 0) { custom_student_list.value.push(allStudent); } }); let type_list = ref([]); GetTaskTeachingTypeList().then(({ type_list: list }) => { type_list.value = list; }); let mode_list = ref([]); GetTaskModeList().then(({ mode_list: list }) => { mode_list.value = list; }); GetCSItem({ id: cs_item_id }).then(({ begin_time, end_time }) => { cs_item_begin_time.value = begin_time; cs_item_end_time.value = end_time; }); let hourArr = ref([]); let minuteArr = ref([]); for (let i = 0; i < 60; i++) { let item = String(i); if (i < 10) item = `0${item}`; if (i < 24) hourArr.value.push(item); minuteArr.value.push(item); } return { cs_item_begin_time, cs_item_end_time, time_type, teacher_list, type_list, mode_list, hourArr, minuteArr }; } /** * 表单及操作 */ export function useForm(liveForm, courseForm, basicForm) { const $t = inject('$t'); const route = useRoute(); const { query } = route; const is_template = 'is_template' in query ? JSON.parse(query.is_template) : false; let form = ref({ name: '', teacher_id: '', teacher_name: '', begin_date: '', begin_date_hour: '00', begin_date_minute: '00', end_date: '', end_date_hour: '00', end_date_minute: '00', teaching_type: 12, content: '' }); const validateTeacher = (rule, value, callback) => { if (form.value.teaching_type === 10 && !value && !is_template) { callback(new Error($t('Key382'))); } else { callback(); } }; const validateNmae = (rule, value, callback) => { if (value.length <= 0) { callback(new Error($t('Key384'))); } else { callback(); } }; let rules = ref({ name: { trigger: 'blur', validator: validateNmae }, teacher_id: { trigger: 'blur', validator: validateTeacher } }); function upload(file) { if (fileTypeSizeLimit(file.file.name, file.file.size)) { return Message.warning(`${file.file.name}文件大小超出限制`); } fileUpload('Open', file).then(({ file_info_list }) => { const type = form.value.teaching_type; if (type === 10) { liveForm.value.file_info_list = liveForm.value.file_info_list.concat(file_info_list); return; } if (type === 11) { courseForm.value.file_info_list = courseForm.value.file_info_list.concat(file_info_list); } if (type === 12) { basicForm.value.file_info_list = basicForm.value.file_info_list.concat(file_info_list); } }); } function closeFile(i, arr) { arr.splice(i, 1); } let student_list = ref([]); // 课节学生列表 let custom_student_list = ref([]); // 自定义学生列表 function selectStudent(i) { if (student_list.value.length <= 0) return; if (typeof i === 'string') { custom_student_list.value = [allStudent]; return; } const list = custom_student_list.value; const index = list.findIndex(({ student_id }) => student_id === student_list.value[i].student_id); if (index === -1) { list.push(student_list.value[i]); // 如果自定义学生列表中有【全部】,删除【全部】 list.some(({ student_id }) => student_id === allStudent.student_id) && list.splice( list.findIndex(({ student_id }) => student_id === allStudent.student_id), 1 ); } else { list.splice(index, 1); list.length <= 0 && custom_student_list.value.push(allStudent); } } function deleteStudentItem(id) { if (id.length <= 0) return; custom_student_list.value.splice( custom_student_list.value.findIndex(({ student_id }) => student_id === id), 1 ); if (custom_student_list.value.length <= 0 && student_list.value.length > 0) { custom_student_list.value.push(allStudent); } } return { form, rules, upload, closeFile, student_list, custom_student_list, selectStudent, deleteStudentItem }; } /** * 选择课件 * @param {Object} form */ export function useCourse(form, liveForm, courseForm) { const $t = inject('$t'); let dialogVisible = ref(false); function dialogClose() { dialogVisible.value = false; } /** * 选择课件 * @param {String} course_id 课件id * @param {String} previewGroupId 预览分组id */ function selectCourse(course_id, previewGroupId) { if (course_id.length === 0) { Message.warning($t('Key385')); return; } dialogVisible.value = false; let _form = form.value.teaching_type === 10 ? liveForm : courseForm; if (_form.value.coursewareInfo.some(({ courseware_id }) => courseware_id === course_id)) return; GetTreeNodeInfo_BookChapterStruct({ id: course_id }).then(({ name, id }) => { _form.value.coursewareInfo.push({ courseware_id: id, courseware_name: name, group_id_selected_info: previewGroupId }); }); } /** * 修改课件名称 * @param {Number} i * @param {String} type * @param {String} name */ function changeCourseName(i, type, name) { (type === 'live' ? liveForm.value : courseForm.value).coursewareInfo[i].courseware_name = name; (type === 'live' ? liveForm.value : courseForm.value).coursewareInfo[i].is_custom_name = 'true'; } /** * 删除课件 * @param {Number} i * @param {String} type */ function closeCourse(i, type) { (type === 'live' ? liveForm.value : courseForm.value).coursewareInfo.splice(i, 1); } return { dialogVisible, changeCourseName, dialogClose, selectCourse, closeCourse }; }