import { StudentObjectiveInfo } from "@/types"; import { studentObjectiveConfirmData } from "@/api/checkPage"; import { message } from "ant-design-vue"; import { onMounted } from "vue"; import { useCheckObjectiveStore } from "@/store"; export default function useStudent() { const checkObjectiveStore = useCheckObjectiveStore(); const currentIndex = $computed(() => checkObjectiveStore.studentIds.indexOf(checkObjectiveStore.currentStudentId) ); const isFirst = $computed(() => currentIndex === 0); const isLast = $computed( () => currentIndex === checkObjectiveStore.studentIds.length - 1 ); const isMultiStudent = $computed( () => checkObjectiveStore.studentIds.length > 1 ); const totalScore = $computed(() => { if (!checkObjectiveStore.student) return 0; return checkObjectiveStore.student.objectiveScore || 0; }); const curImageUrl = $computed(() => checkObjectiveStore.student ? checkObjectiveStore.student.sheetUrls[checkObjectiveStore.currentImage] ?.url : "" ); const answersComputed = $computed(() => { let mains = checkObjectiveStore.student?.answers.map((v) => ({ mainTitle: "", mainNumber: v.mainNumber, subs: [v], })); const mSet = new Set(); mains = mains?.filter((v) => { if (!mSet.has(v.mainNumber)) { mSet.add(v.mainNumber); v.subs = []; return true; } }); mains?.forEach((v) => { v.mainTitle = checkObjectiveStore.student?.titles[v.mainNumber] ?? ""; v.subs = checkObjectiveStore.student?.answers.filter( (v2) => v2.mainNumber === v.mainNumber ) ?? []; }); return mains; }); async function getNextStudent() { if (isLast) { void message.warning("已经是最后一份!"); return; } await getStudent(checkObjectiveStore.studentIds[currentIndex + 1]); } async function getPreviousStudent() { if (isFirst) { void message.warning("已经是第一份!"); return; } await getStudent(checkObjectiveStore.studentIds[currentIndex - 1]); } async function getStudent(studentId: string) { let dataError = false; const res = await studentObjectiveConfirmData(studentId).catch(() => { dataError = true; }); if (dataError) { void message.error(res.message, 24 * 60 * 60); throw new Error("取学生信息出错: " + res.message); } checkObjectiveStore.student = res.data as StudentObjectiveInfo; checkObjectiveStore.currentStudentId = studentId; checkObjectiveStore.currentImage = 0; checkObjectiveStore.browsedImageIndexes = [0]; checkObjectiveStore.answerMap = {}; checkObjectiveStore.student?.answers.forEach((item) => { checkObjectiveStore.answerMap[`${item.mainNumber}_${item.subNumber}`] = { answer: item.answer, isRight: item.answer === item.standardAnswer, }; }); } async function updateCurStudent() { await getStudent(checkObjectiveStore.studentIds[currentIndex]); } onMounted(async () => { if (checkObjectiveStore.studentIds.length === 0) { void message.info("没有需要处理的考生,请返回。"); return; } await getNextStudent(); }); return { currentIndex, isFirst, isLast, isMultiStudent, totalScore, curImageUrl, answersComputed, getNextStudent, getPreviousStudent, updateCurStudent, }; }