useStudent.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { StudentObjectiveInfo } from "@/types";
  2. import { studentObjectiveConfirmData } from "@/api/checkPage";
  3. import { message } from "ant-design-vue";
  4. import { onMounted } from "vue";
  5. import { useCheckObjectiveStore } from "@/store";
  6. export default function useStudent() {
  7. const checkObjectiveStore = useCheckObjectiveStore();
  8. const currentIndex = $computed(() =>
  9. checkObjectiveStore.studentIds.indexOf(checkObjectiveStore.currentStudentId)
  10. );
  11. const isFirst = $computed(() => currentIndex === 0);
  12. const isLast = $computed(
  13. () => currentIndex === checkObjectiveStore.studentIds.length - 1
  14. );
  15. const isMultiStudent = $computed(
  16. () => checkObjectiveStore.studentIds.length > 1
  17. );
  18. const totalScore = $computed(() => {
  19. if (!checkObjectiveStore.student) return 0;
  20. return checkObjectiveStore.student.objectiveScore || 0;
  21. });
  22. const curImageUrl = $computed(() =>
  23. checkObjectiveStore.student
  24. ? checkObjectiveStore.student.sheetUrls[checkObjectiveStore.currentImage]
  25. ?.url
  26. : ""
  27. );
  28. const answersComputed = $computed(() => {
  29. let mains = checkObjectiveStore.student?.answers.map((v) => ({
  30. mainTitle: "",
  31. mainNumber: v.mainNumber,
  32. subs: [v],
  33. }));
  34. const mSet = new Set();
  35. mains = mains?.filter((v) => {
  36. if (!mSet.has(v.mainNumber)) {
  37. mSet.add(v.mainNumber);
  38. v.subs = [];
  39. return true;
  40. }
  41. });
  42. mains?.forEach((v) => {
  43. v.mainTitle = checkObjectiveStore.student?.titles[v.mainNumber] ?? "";
  44. v.subs =
  45. checkObjectiveStore.student?.answers.filter(
  46. (v2) => v2.mainNumber === v.mainNumber
  47. ) ?? [];
  48. });
  49. return mains;
  50. });
  51. async function getNextStudent() {
  52. if (isLast) {
  53. void message.warning("已经是最后一份!");
  54. return;
  55. }
  56. await getStudent(checkObjectiveStore.studentIds[currentIndex + 1]);
  57. }
  58. async function getPreviousStudent() {
  59. if (isFirst) {
  60. void message.warning("已经是第一份!");
  61. return;
  62. }
  63. await getStudent(checkObjectiveStore.studentIds[currentIndex - 1]);
  64. }
  65. async function getStudent(studentId: string) {
  66. let dataError = false;
  67. const res = await studentObjectiveConfirmData(studentId).catch(() => {
  68. dataError = true;
  69. });
  70. if (dataError) {
  71. void message.error(res.message, 24 * 60 * 60);
  72. throw new Error("取学生信息出错: " + res.message);
  73. }
  74. checkObjectiveStore.student = res.data as StudentObjectiveInfo;
  75. checkObjectiveStore.currentStudentId = studentId;
  76. checkObjectiveStore.currentImage = 0;
  77. checkObjectiveStore.browsedImageIndexes = [0];
  78. checkObjectiveStore.answerMap = {};
  79. checkObjectiveStore.student?.answers.forEach((item) => {
  80. checkObjectiveStore.answerMap[`${item.mainNumber}_${item.subNumber}`] = {
  81. answer: item.answer,
  82. isRight: item.answer === item.standardAnswer,
  83. };
  84. });
  85. }
  86. async function updateCurStudent() {
  87. await getStudent(checkObjectiveStore.studentIds[currentIndex]);
  88. }
  89. onMounted(async () => {
  90. if (checkObjectiveStore.studentIds.length === 0) {
  91. void message.info("没有需要处理的考生,请返回。");
  92. return;
  93. }
  94. await getNextStudent();
  95. });
  96. return {
  97. currentIndex,
  98. isFirst,
  99. isLast,
  100. isMultiStudent,
  101. totalScore,
  102. curImageUrl,
  103. answersComputed,
  104. getNextStudent,
  105. getPreviousStudent,
  106. updateCurStudent,
  107. };
  108. }