MultiMediaMarkBody.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <div class="rich-text-question-container">
  3. <div v-for="(question, index) in questions" :key="index">
  4. <div class="question">
  5. <div class="tw-text-xl">
  6. <span :id="'q-' + question.unionOrder">题号:</span>
  7. <span>{{ question.unionOrder }}</span>
  8. </div>
  9. <div>
  10. <div class="tw-text-xl">题干:</div>
  11. <div v-html="getDomByRichTextJSON(question.parentBody)?.innerHTML" />
  12. <div v-html="getDomByRichTextJSON(question.body)?.innerHTML" />
  13. <template v-if="question.objective">
  14. <div v-if="question.options">
  15. <div
  16. v-for="(option, index2) in question.options"
  17. :key="index2"
  18. class="tw-flex tw-gap-1"
  19. >
  20. {{ indexToABCD(option.number) }}.
  21. <div v-html="getDomByRichTextJSON(option.body)?.innerHTML" />
  22. </div>
  23. </div>
  24. </template>
  25. </div>
  26. <div>
  27. <template v-if="question.objective">
  28. <span class="tw-text-blue-600">考生答案:</span
  29. >{{ renderObjective(question.studentAnswer) }}
  30. </template>
  31. <template v-else>
  32. <div class="tw-text-blue-600">
  33. 考生答案:(字数统计:{{
  34. getDomByRichTextJSON(question.studentAnswer)?.innerText
  35. .length ?? 0
  36. }})
  37. </div>
  38. <div
  39. v-html="getDomByRichTextJSON(question.studentAnswer)?.innerHTML"
  40. />
  41. </template>
  42. </div>
  43. <div>
  44. <template v-if="question.objective">
  45. <span class="tw-text-blue-600">标准答案:</span
  46. >{{ renderObjective(question.standardAnswer) }}
  47. </template>
  48. <template v-else>
  49. <div class="tw-text-blue-600">标准答案:</div>
  50. <div
  51. v-html="getDomByRichTextJSON(question.standardAnswer)?.innerHTML"
  52. />
  53. </template>
  54. </div>
  55. <div v-if="showScore(question)" style="color: blue">
  56. 得分 / 总分 :{{
  57. (question.score ?? "-") + " / " + question.totalScore
  58. }}
  59. </div>
  60. </div>
  61. <div style="margin-bottom: 20px"></div>
  62. </div>
  63. </div>
  64. </template>
  65. <script setup lang="ts">
  66. import { getStudentAnswerJSON } from "@/api/jsonMark";
  67. import { store } from "@/store/store";
  68. import { onUpdated, watch } from "vue";
  69. import { renderRichText } from "@/utils/renderJSON";
  70. import type { RichTextJSON } from "@/types";
  71. import "viewerjs/dist/viewer.css";
  72. import Viewer from "viewerjs";
  73. import { useRoute } from "vue-router";
  74. const route = useRoute();
  75. const isSeePaper = route.name === "StudentTrack";
  76. const showScore = (question: QuestionForRender) =>
  77. route.name !== "Mark" && question.totalScore;
  78. interface QuestionForRender {
  79. unionOrder: string;
  80. parentBody: RichTextJSON | null;
  81. body: RichTextJSON;
  82. studentAnswer: Array<RichTextJSON> | null;
  83. standardAnswer: Array<RichTextJSON> | null;
  84. score: number | null;
  85. totalScore: number;
  86. objective: boolean | null;
  87. options: Array<{ number: number; body: RichTextJSON }>;
  88. }
  89. let questions: QuestionForRender[] = $ref([]);
  90. async function updateStudentAnswerJSON() {
  91. return getStudentAnswerJSON(store.currentTask?.jsonUrl as string);
  92. }
  93. function getDomByRichTextJSON(rt: Array<RichTextJSON> | RichTextJSON | null) {
  94. const node = document.createElement("div");
  95. if (!rt) return node;
  96. if (Array.isArray(rt)) {
  97. for (const r of rt) {
  98. node.appendChild(renderRichText(r));
  99. }
  100. } else {
  101. node.appendChild(renderRichText(rt));
  102. }
  103. return node;
  104. }
  105. watch(
  106. () => store.currentTask,
  107. async () => {
  108. questions.splice(0);
  109. if (!store.currentTask?.jsonUrl) return;
  110. const res = await updateStudentAnswerJSON();
  111. const stuAnswers = res.data;
  112. for (const ans of stuAnswers) {
  113. if (ans.answer && !Array.isArray(ans.answer)) {
  114. ans.answer = [ans.answer];
  115. }
  116. }
  117. // 查看原卷显示全部题目
  118. if (isSeePaper) {
  119. for (const questionBody of store.setting.subject.questions || []) {
  120. const questionForRender = {} as QuestionForRender;
  121. const [mainNumber, subNumber] = questionBody.unionOrder.split("-");
  122. const stuAns = stuAnswers.find(
  123. (v) =>
  124. questionBody.unionOrder ===
  125. [v.mainNumber, v.subNumber, v.subIndex]
  126. .filter((v) => typeof v !== "undefined")
  127. .join("-")
  128. ) || {
  129. mainNumber: +mainNumber,
  130. subNumber: subNumber,
  131. subIndex: "",
  132. answer: [],
  133. };
  134. const taskQuestion = (store.currentTask?.questionList || []).find(
  135. (v) =>
  136. [v.mainNumber, v.subNumber].join("-") === questionBody.unionOrder
  137. );
  138. questionForRender.unionOrder = questionBody.unionOrder;
  139. questionForRender.parentBody = questionBody.parentBody;
  140. questionForRender.body = questionBody.body;
  141. questionForRender.options = questionBody.options;
  142. questionForRender.objective = questionBody.objective;
  143. questionForRender.standardAnswer = questionBody.answer;
  144. questionForRender.studentAnswer = stuAns.answer;
  145. questionForRender.score = taskQuestion?.score || null;
  146. questionForRender.totalScore = taskQuestion?.maxScore || 0;
  147. questions.push(questionForRender);
  148. }
  149. } else {
  150. for (const taskQuestion of store.currentTask?.questionList || []) {
  151. const questionForRender = {} as QuestionForRender;
  152. const { mainNumber, subNumber } = taskQuestion;
  153. const questionBody = store.setting.subject.questions.find(
  154. (ques) => ques.unionOrder === `${mainNumber}-${subNumber}`
  155. );
  156. if (!questionBody) continue;
  157. const stuAns = stuAnswers.find(
  158. (v) =>
  159. questionBody.unionOrder ===
  160. [v.mainNumber, v.subNumber, v.subIndex]
  161. .filter((v) => typeof v !== "undefined")
  162. .join("-")
  163. ) || {
  164. mainNumber: +mainNumber,
  165. subNumber: subNumber,
  166. subIndex: "",
  167. answer: [],
  168. };
  169. questionForRender.unionOrder = questionBody.unionOrder;
  170. questionForRender.parentBody = questionBody.parentBody;
  171. questionForRender.body = questionBody.body;
  172. questionForRender.standardAnswer = questionBody.answer;
  173. questionForRender.studentAnswer = stuAns.answer;
  174. questionForRender.score = taskQuestion.score;
  175. questionForRender.totalScore = taskQuestion.maxScore;
  176. questions.push(questionForRender);
  177. }
  178. }
  179. },
  180. { immediate: true }
  181. );
  182. const indexToABCD = (index: number) => "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[index - 1];
  183. const renderObjective = (ans: any) => {
  184. if (typeof ans === "boolean") {
  185. return ans ? "A" : "B";
  186. } else if (Array.isArray(ans) && typeof ans[0] === "boolean") {
  187. return ans[0] ? "A" : "B";
  188. } else if (Array.isArray(ans) && typeof ans[0] === "number") {
  189. return (ans as unknown as number[]).map((v) => indexToABCD(v)).join("");
  190. } else if (Array.isArray(ans) && ans.length === 0) {
  191. return "";
  192. } else {
  193. console.log("错误的答案类型", JSON.stringify(ans));
  194. }
  195. };
  196. let viewer: Viewer = null as unknown as Viewer;
  197. onUpdated(() => {
  198. viewer && viewer.destroy();
  199. viewer = new Viewer(
  200. document.querySelector(".rich-text-question-container") as HTMLElement,
  201. // document.querySelector("#app") as HTMLElement,
  202. {
  203. // inline: true,
  204. viewed() {
  205. // viewer.zoomTo(1);
  206. },
  207. zIndex: 10000,
  208. }
  209. );
  210. });
  211. </script>
  212. <style scoped>
  213. .question {
  214. background-color: var(--app-container-bg-color);
  215. padding: 5px;
  216. border-radius: 5px;
  217. }
  218. .rich-text-question-container {
  219. background-color: transparent;
  220. font-size: 16px;
  221. }
  222. </style>
  223. <style>
  224. .rich-text-question-container img.inline {
  225. display: inline;
  226. }
  227. .rich-text-question-container img {
  228. cursor: pointer;
  229. }
  230. </style>