useTaskWatch.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { watch } from "vue";
  2. import { useMarkStore } from "@/store";
  3. export default function useTaskWatch() {
  4. const markStore = useMarkStore();
  5. watch(
  6. () => markStore.currentTask,
  7. () => {
  8. // 初始化 task.markResult ,始终保证 task 下有 markResult
  9. // 1. 评卷时,如果没有 markResult ,则初始化一个 markResult 给它
  10. // 1. 回评时,先清空它的 markResult ,然后初始化一个 markResult 给它
  11. if (!markStore.currentTask) return;
  12. const task = markStore.currentTask;
  13. if (task.previous && task.markResult) {
  14. task.markResult = undefined;
  15. }
  16. if (!task.markResult) {
  17. // 管理后台可能不设置 questionList, 而且它不用 markResult
  18. if (!task.questionList) {
  19. task.questionList = [];
  20. // return;
  21. }
  22. // 初始化 __index
  23. task.questionList.forEach((q, i, ar) => (ar[i].__index = i));
  24. task.__markStartTime = Date.now();
  25. const statusValue = markStore.setting.statusValue;
  26. const { examId, studentId, paperNumber } = task;
  27. task.markResult = {
  28. examId,
  29. paperNumber,
  30. studentId,
  31. spent: 0,
  32. statusValue,
  33. questionList: task.questionList,
  34. markerTrackList: task.questionList
  35. .map((q) =>
  36. q.headerTrackList && q.headerTrackList.length
  37. ? q.headerTrackList
  38. : q.markerTrackList
  39. )
  40. .flat(),
  41. markerTagList: task.questionList
  42. .map((q) => q.markerTagList || [])
  43. .flat(),
  44. scoreList: task.questionList.map((q) => q.markerScore),
  45. markerScore: null, // 后期通过 scoreList 自动更新
  46. };
  47. task.markResult.markerTrackList.forEach((t) => {
  48. if (t.unanswered) {
  49. t.score = -0;
  50. }
  51. });
  52. }
  53. }
  54. );
  55. // 唯一根据 scoreList 自动更新 markerScore
  56. watch(
  57. () => markStore.currentTask?.markResult.scoreList,
  58. () => {
  59. if (!markStore.currentTask) return;
  60. const scoreList = markStore.currentTask.markResult.scoreList.filter(
  61. (v) => v !== null
  62. );
  63. const result =
  64. scoreList.length === 0
  65. ? null
  66. : scoreList.reduce((acc, v) => (acc += Math.round(v * 1000)), 0) /
  67. 1000;
  68. markStore.currentTask.markResult.markerScore = result;
  69. },
  70. { deep: true }
  71. );
  72. }