MultiMediaMarkBody.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 { getJSON } 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 StudentAnswer {
  79. mainNumber: number;
  80. subNumber: string;
  81. subIndex: string;
  82. answer: Array<RichTextJSON> | null;
  83. }
  84. interface QuestionForRender {
  85. unionOrder: string;
  86. parentBody: RichTextJSON | null;
  87. body: RichTextJSON;
  88. studentAnswer: Array<RichTextJSON> | null;
  89. standardAnswer: Array<RichTextJSON> | null;
  90. score: number | null;
  91. totalScore: number;
  92. objective: boolean | null;
  93. options: Array<{ number: number; body: RichTextJSON }>;
  94. }
  95. let questions: QuestionForRender[] = $ref([]);
  96. async function updateStudentAnswerJSON() {
  97. return getJSON(store.currentTask?.jsonUrl as string);
  98. }
  99. function getDomByRichTextJSON(rt: Array<RichTextJSON> | RichTextJSON | null) {
  100. const node = document.createElement("div");
  101. if (!rt) return node;
  102. if (Array.isArray(rt)) {
  103. for (const r of rt) {
  104. node.appendChild(renderRichText(r));
  105. }
  106. } else {
  107. node.appendChild(renderRichText(rt));
  108. }
  109. return node;
  110. }
  111. watch(
  112. () => store.currentTask,
  113. async () => {
  114. questions.splice(0);
  115. if (!store.currentTask?.jsonUrl) return;
  116. const res = await updateStudentAnswerJSON();
  117. const stuAnswers: StudentAnswer[] = res.data;
  118. for (const ans of stuAnswers) {
  119. if (ans.answer && !Array.isArray(ans.answer)) {
  120. ans.answer = [ans.answer];
  121. }
  122. }
  123. // 查看原卷显示全部题目
  124. if (isSeePaper) {
  125. for (const questionBody of store.setting.subject.questions || []) {
  126. const questionForRender = {} as QuestionForRender;
  127. const [mainNumber, subNumber] = questionBody.unionOrder.split("-");
  128. const stuAns: StudentAnswer = stuAnswers.find(
  129. (v: StudentAnswer) =>
  130. questionBody.unionOrder ===
  131. [v.mainNumber, v.subNumber, v.subIndex]
  132. .filter((v) => typeof v !== "undefined")
  133. .join("-")
  134. ) || {
  135. mainNumber: +mainNumber,
  136. subNumber: subNumber,
  137. subIndex: "",
  138. answer: [],
  139. };
  140. const taskQuestion = (store.currentTask?.questionList || []).find(
  141. (v) =>
  142. [v.mainNumber, v.subNumber].join("-") === questionBody.unionOrder
  143. );
  144. questionForRender.unionOrder = questionBody.unionOrder;
  145. questionForRender.parentBody = questionBody.parentBody;
  146. questionForRender.body = questionBody.body;
  147. questionForRender.options = questionBody.options;
  148. questionForRender.objective = questionBody.objective;
  149. questionForRender.standardAnswer = questionBody.answer;
  150. questionForRender.studentAnswer = stuAns.answer;
  151. questionForRender.score = taskQuestion?.score || null;
  152. questionForRender.totalScore = taskQuestion?.maxScore || 0;
  153. questions.push(questionForRender);
  154. }
  155. } else {
  156. for (const taskQuestion of store.currentTask?.questionList || []) {
  157. const questionForRender = {} as QuestionForRender;
  158. const { mainNumber, subNumber } = taskQuestion;
  159. const questionBody = store.setting.subject.questions.find(
  160. (ques) => ques.unionOrder === `${mainNumber}-${subNumber}`
  161. );
  162. if (!questionBody) continue;
  163. const stuAns: StudentAnswer = stuAnswers.find(
  164. (v: StudentAnswer) =>
  165. questionBody.unionOrder ===
  166. [v.mainNumber, v.subNumber, v.subIndex]
  167. .filter((v) => typeof v !== "undefined")
  168. .join("-")
  169. ) || {
  170. mainNumber: +mainNumber,
  171. subNumber: subNumber,
  172. subIndex: "",
  173. answer: [],
  174. };
  175. questionForRender.unionOrder = questionBody.unionOrder;
  176. questionForRender.parentBody = questionBody.parentBody;
  177. questionForRender.body = questionBody.body;
  178. questionForRender.standardAnswer = questionBody.answer;
  179. questionForRender.studentAnswer = stuAns.answer;
  180. questionForRender.score = taskQuestion.score;
  181. questionForRender.totalScore = taskQuestion.maxScore;
  182. questions.push(questionForRender);
  183. }
  184. }
  185. },
  186. { immediate: true }
  187. );
  188. const indexToABCD = (index: number) => "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[index - 1];
  189. const renderObjective = (ans: any) => {
  190. if (typeof ans === "boolean") {
  191. return ans ? "A" : "B";
  192. } else if (Array.isArray(ans) && typeof ans[0] === "boolean") {
  193. return ans[0] ? "A" : "B";
  194. } else if (Array.isArray(ans) && typeof ans[0] === "number") {
  195. return (ans as unknown as number[]).map((v) => indexToABCD(v)).join("");
  196. } else if (Array.isArray(ans) && ans.length === 0) {
  197. return "";
  198. } else {
  199. console.log("错误的答案类型", JSON.stringify(ans));
  200. }
  201. };
  202. let viewer: Viewer = null as unknown as Viewer;
  203. onUpdated(() => {
  204. viewer && viewer.destroy();
  205. viewer = new Viewer(
  206. document.querySelector(".rich-text-question-container") as HTMLElement,
  207. // document.querySelector("#app") as HTMLElement,
  208. {
  209. // inline: true,
  210. viewed() {
  211. // viewer.zoomTo(1);
  212. },
  213. zIndex: 10000,
  214. }
  215. );
  216. });
  217. </script>
  218. <style scoped>
  219. .question {
  220. background-color: var(--app-container-bg-color);
  221. padding: 5px;
  222. border-radius: 5px;
  223. }
  224. .rich-text-question-container {
  225. background-color: transparent;
  226. font-size: 16px;
  227. }
  228. </style>
  229. <style>
  230. .rich-text-question-container img.inline {
  231. display: inline;
  232. }
  233. .rich-text-question-container img {
  234. cursor: pointer;
  235. }
  236. </style>