import { watch } from "vue"; import { useMarkStore } from "@/store"; export default function useTaskWatch() { const markStore = useMarkStore(); watch( () => markStore.currentTask, () => { // 初始化 task.markResult ,始终保证 task 下有 markResult // 1. 评卷时,如果没有 markResult ,则初始化一个 markResult 给它 // 1. 回评时,先清空它的 markResult ,然后初始化一个 markResult 给它 if (!markStore.currentTask) return; const task = markStore.currentTask; if (task.previous && task.markResult) { task.markResult = undefined; } if (!task.markResult) { // 管理后台可能不设置 questionList, 而且它不用 markResult if (!task.questionList) { task.questionList = []; // return; } // 初始化 __index task.questionList.forEach((q, i, ar) => (ar[i].__index = i)); task.__markStartTime = Date.now(); const statusValue = markStore.setting.statusValue; const { examId, studentId, paperNumber } = task; task.markResult = { examId, paperNumber, studentId, spent: 0, statusValue, questionList: task.questionList, markerTrackList: task.questionList .map((q) => q.headerTrackList && q.headerTrackList.length ? q.headerTrackList : q.markerTrackList ) .flat(), markerTagList: task.questionList .map((q) => q.markerTagList || []) .flat(), scoreList: task.questionList.map((q) => q.markerScore), markerScore: null, // 后期通过 scoreList 自动更新 }; task.markResult.markerTrackList.forEach((t) => { if (t.unanswered) { t.score = -0; } }); } } ); // 唯一根据 scoreList 自动更新 markerScore watch( () => markStore.currentTask?.markResult.scoreList, () => { if (!markStore.currentTask) return; const scoreList = markStore.currentTask.markResult.scoreList.filter( (v) => v !== null ); const result = scoreList.length === 0 ? null : scoreList.reduce((acc, v) => (acc += Math.round(v * 1000)), 0) / 1000; markStore.currentTask.markResult.markerScore = result; }, { deep: true } ); }