TextQuestionView.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <template>
  2. <div v-if="isSyncState" :key="answerDivKey" class="question-view">
  3. <question-body
  4. :question-body="question.body"
  5. :exam-question="examQuestion"
  6. ></question-body>
  7. <div class="ops">
  8. <div class="score">({{ examQuestion.questionScore }}分)</div>
  9. </div>
  10. <div class="option">
  11. <!-- <textarea v-model="studentAnswer" maxlength="5000" type="text" /> -->
  12. <div v-if="!isAudioAnswerType">
  13. <div class="menu">
  14. <i-button type="info" class="text-ops" size="small" @click="textCopy">
  15. 复制
  16. </i-button>
  17. <i-button type="info" class="text-ops" size="small" @click="textCut">
  18. 剪切
  19. </i-button>
  20. <i-button
  21. type="info"
  22. class="text-ops"
  23. size="small"
  24. @click="textPaste"
  25. >
  26. 粘贴
  27. </i-button>
  28. <i-button type="info" class="text-ops" size="small" @click="textSup">
  29. 上标
  30. </i-button>
  31. <i-button
  32. type="info"
  33. class="text-ops"
  34. size="small"
  35. @click="undoTextSup"
  36. >
  37. 取消上标
  38. </i-button>
  39. <i-button type="info" class="text-ops" size="small" @click="textSub">
  40. 下标
  41. </i-button>
  42. <i-button
  43. type="info"
  44. class="text-ops"
  45. size="small"
  46. @click="undoTextSup"
  47. >
  48. 取消下标
  49. </i-button>
  50. </div>
  51. <div
  52. ref="answerDiv"
  53. ondragstart="return false"
  54. ondrop="return false"
  55. :contenteditable="true"
  56. class="stu-answer"
  57. @keydown="disableCtrl"
  58. @input="$event => textInput($event)"
  59. @blur="$event => textInput($event)"
  60. v-html="originalStudentAnswer"
  61. ></div>
  62. <div
  63. style="margin-top: -25px; margin-bottom: 25px; width: 100%; max-width: 500px;"
  64. >
  65. <div style="float: right; margin-right: 10px; ">
  66. {{ answerWordCount }}
  67. </div>
  68. </div>
  69. </div>
  70. <div v-if="shouldFetchQrCode && isAudioAnswerType">
  71. <div>
  72. <div v-if="qrValue" style="display: flex">
  73. <qrcode
  74. :value="qrValue"
  75. :options="{ width: 200 }"
  76. style="margin-left: -10px;"
  77. ></qrcode>
  78. <div style="margin-top: 10px;">
  79. <div>
  80. 请使用<span style="font-weight: 900; color: #1E90FF;">微信</span
  81. >扫描二维码后,在微信小程序上{{
  82. isAudioAnswerType ? "录音" : "拍照"
  83. }},并上传文件。
  84. </div>
  85. <div v-if="qrScanned" style="margin-top: 30px; font-size: 30px;">
  86. {{ examQuestion.studentAnswer ? "已上传" : "已扫描" }}
  87. <Icon type="md-checkmark" />
  88. </div>
  89. </div>
  90. </div>
  91. <div v-else>正在获取二维码...</div>
  92. </div>
  93. <div
  94. class="audio-answer audio-answer-line-height"
  95. style="margin-top: 20px ;"
  96. >
  97. <span class="audio-answer-line-height">答案:</span>
  98. <audio
  99. v-if="examQuestion.studentAnswer"
  100. class="audio-answer-line-height"
  101. controls
  102. controlsList="nodownload"
  103. :src="examQuestion.studentAnswer"
  104. />
  105. <span v-else class="audio-answer-line-height">未上传文件</span>
  106. </div>
  107. </div>
  108. <div v-if="canAttachPhotos" style="padding-top: 1px;">
  109. <UploadPhotos
  110. :default-list="
  111. photoAnswers.map(v => {
  112. return v;
  113. })
  114. "
  115. :qr-value="qrValue"
  116. :exam-question="examQuestion"
  117. style="margin-top: 20px; width: 350px;"
  118. @on-photo-added="photoAdded"
  119. @on-photo-removed="photoRemoved"
  120. @on-photos-reseted="photosReseted"
  121. />
  122. </div>
  123. <div class="reset" style="padding-top: 20px;">
  124. <i-button type="warning" size="large" @click="resetAnswer">
  125. 重置答案
  126. </i-button>
  127. <span v-if="examShouldShowAnswer()">
  128. &nbsp;&nbsp;&nbsp;<i-button
  129. type="info"
  130. size="large"
  131. @click="showAnswer"
  132. >
  133. 显示答案
  134. </i-button>
  135. </span>
  136. <div v-if="examShouldShowAnswer() && isShowAnswer">
  137. 正确答案:
  138. <div v-html="rightAnswerTransform"></div>
  139. </div>
  140. </div>
  141. </div>
  142. </div>
  143. </template>
  144. <script>
  145. import QuestionBody from "./QuestionBody";
  146. import { createNamespacedHelpers } from "vuex";
  147. const { mapMutations, mapGetters, mapState } = createNamespacedHelpers(
  148. "examingHomeModule"
  149. );
  150. import VueQrcode from "@chenfengyuan/vue-qrcode";
  151. import UploadPhotos from "./UploadPhotos";
  152. export default {
  153. name: "TextQuestionView",
  154. components: {
  155. QuestionBody,
  156. UploadPhotos,
  157. qrcode: VueQrcode,
  158. },
  159. props: {
  160. question: {
  161. type: Object,
  162. default() {
  163. return {};
  164. },
  165. },
  166. examQuestion: {
  167. type: Object,
  168. default() {
  169. return {};
  170. },
  171. },
  172. },
  173. data() {
  174. return {
  175. studentAnswer: this.examQuestion.studentAnswer,
  176. originalStudentAnswer: this.examQuestion.studentAnswer,
  177. isShowAnswer: false,
  178. answerDivKey: Math.random(),
  179. qrValue: "",
  180. qrScanned: false,
  181. };
  182. },
  183. computed: {
  184. ...mapState(["exam", "questionQrCode", "questionQrCodeScanned"]),
  185. isSyncState() {
  186. return this.examQuestion.order == this.$route.params.order;
  187. },
  188. rightAnswerTransform() {
  189. return this.question.rightAnswer.join("");
  190. },
  191. answerWordCount() {
  192. if (this.studentAnswer && this.$refs.answerDiv) {
  193. return this.$refs.answerDiv.innerText.replace(/\s+/g, "").length;
  194. } else {
  195. const ele = document.createElement("div");
  196. ele.innerHTML = this.studentAnswer;
  197. return ele.innerText.replace(/\s+/g, "").length;
  198. }
  199. },
  200. isAudioAnswerType() {
  201. return this.examQuestion.answerType === "SINGLE_AUDIO";
  202. },
  203. shouldFetchQrCode() {
  204. const shouldFetch = this.exam.WEIXIN_ANSWER_ENABLED;
  205. return shouldFetch;
  206. },
  207. canAttachPhotos() {
  208. return this.exam.WEIXIN_ANSWER_ENABLED && !this.isAudioAnswerType;
  209. },
  210. photoAnswers: {
  211. get() {
  212. if (!this.studentAnswer) return [];
  213. const ele = document.createElement("div");
  214. ele.innerHTML = this.studentAnswer;
  215. const imgs = ele.querySelectorAll(".photo-answer");
  216. // if()
  217. return [...imgs].map(e =>
  218. e.src.replace("?x-oss-process=image/resize,m_lfit,h_200,w_200", "")
  219. );
  220. },
  221. set(pSrcs) {
  222. let imageStr = pSrcs.map(
  223. v =>
  224. `<a href='${v}' target='_blank' ><img class='photo-answer' src='${v +
  225. "?x-oss-process=image/resize,m_lfit,h_200,w_200"}' /></a>`
  226. );
  227. const ele = document.createElement("div");
  228. ele.innerHTML = this.studentAnswer || "";
  229. const pEle = ele.querySelectorAll(".photo-answers-block");
  230. if (pEle) [...pEle].forEach(v => v.remove());
  231. // console.log(ele.innerHTML);
  232. if (!ele.innerHTML && pSrcs.length === 0) {
  233. // 完全为空则重置答案
  234. this.studentAnswer = null;
  235. // 更新answerDiv的内容
  236. this.originalStudentAnswer = null;
  237. } else {
  238. this.studentAnswer =
  239. ele.innerHTML +
  240. `<div class='photo-answers-block'>${imageStr.join("")}</div>`;
  241. // 更新answerDiv的内容
  242. this.originalStudentAnswer = this.studentAnswer;
  243. }
  244. },
  245. },
  246. },
  247. watch: {
  248. examQuestion() {
  249. // console.log(this.examQuestion.studentAnswer);
  250. this.studentAnswer = this.examQuestion.studentAnswer;
  251. },
  252. questionQrCode(value) {
  253. // console.log(this.examQuestion.studentAnswer);
  254. // console.log("watch", value);
  255. if (value.order === this.examQuestion.order) {
  256. this.qrValue = value.qrCode;
  257. }
  258. },
  259. questionQrCodeScanned(value) {
  260. // console.log(this.examQuestion.studentAnswer);
  261. // console.log("watch", value);
  262. if (value.order === this.examQuestion.order) {
  263. this.qrScanned = true;
  264. }
  265. },
  266. studentAnswer() {
  267. let realAnswer = null;
  268. if (this.studentAnswer) {
  269. // 如果有实际内容
  270. realAnswer = this.studentAnswer
  271. .replace(/<sup><\/sup>/gi, "")
  272. .replace(/<sub><\/sub>/gi, "")
  273. .replace(/<script/gi, "&lt;script")
  274. .replace(/script>/gi, "script&gt;");
  275. // .replace(/</gi, "&lt;")
  276. // .replace(/>/gi, "&gt;")
  277. // .replace(/&lt;div&gt;&lt;br&gt;&lt;\/div&gt;/gi, "<div><br></div>");
  278. }
  279. if (realAnswer !== this.examQuestion.studentAnswer) {
  280. this.updateExamQuestion({
  281. order: this.examQuestion.order,
  282. studentAnswer: realAnswer,
  283. });
  284. }
  285. },
  286. },
  287. created() {
  288. this.fetchQRCode();
  289. this.fetchQRCodeInterval = setInterval(() => {
  290. if (!this.qrValue) {
  291. this.fetchQRCode();
  292. } else {
  293. clearInterval(this.fetchQRCodeInterval);
  294. }
  295. }, 6 * 1000);
  296. },
  297. beforeDestroy() {
  298. clearTimeout(this.fetchQRCodeTimeout);
  299. clearInterval(this.fetchQRCodeInterval);
  300. },
  301. methods: {
  302. ...mapMutations(["updateExamQuestion"]),
  303. ...mapGetters(["examShouldShowAnswer"]),
  304. disableCtrl(e) {
  305. if (e.ctrlKey || e.metaKey || e.altKey) {
  306. // .ctrlKey tells that ctrl key was pressed.
  307. e.preventDefault();
  308. return false;
  309. }
  310. return true;
  311. },
  312. textCopy() {
  313. var selElm = getSelection();
  314. var selRange = selElm.getRangeAt(0);
  315. this.copyNode = selRange.cloneContents();
  316. },
  317. textCut() {
  318. var selElm = getSelection();
  319. var selRange = selElm.getRangeAt(0);
  320. this.copyNode = selRange.extractContents();
  321. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  322. },
  323. textPaste() {
  324. var selElm = getSelection();
  325. var selRange = selElm.getRangeAt(0);
  326. selRange.deleteContents();
  327. selRange.insertNode(this.copyNode.cloneNode(true));
  328. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  329. },
  330. textSup() {
  331. getSelection()
  332. .getRangeAt(0)
  333. .surroundContents(document.createElement("sup"));
  334. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  335. },
  336. undoTextSup() {
  337. getSelection().modify("extend", "left", "character");
  338. let selRange = getSelection().getRangeAt(0);
  339. var documentFragment = selRange.extractContents();
  340. var text = new Text(documentFragment.textContent);
  341. selRange.insertNode(text);
  342. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  343. },
  344. textSub() {
  345. getSelection()
  346. .getRangeAt(0)
  347. .surroundContents(document.createElement("sub"));
  348. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  349. },
  350. textInput($event) {
  351. // console.log($event.target.innerHTML);
  352. this.studentAnswer = $event.target.innerHTML;
  353. },
  354. showAnswer() {
  355. this.isShowAnswer = !this.isShowAnswer;
  356. },
  357. resetAnswer() {
  358. this.studentAnswer = null;
  359. this.originalStudentAnswer = null;
  360. // this.$nextTick(() => {
  361. // // Object.assign(this.$data, this.$options.data());
  362. // // this.$refs.answerDiv.$forceUpdate();
  363. // this.answerDivKey = Math.random();
  364. // });
  365. this.answerDivKey = Math.random();
  366. },
  367. async fetchQRCode() {
  368. if (this.shouldFetchQrCode) {
  369. const transferFileType =
  370. this.examQuestion.answerType === "SINGLE_AUDIO" ? "AUDIO" : "PIC";
  371. const examRecordDataId = this.$route.params.examRecordDataId;
  372. const response = await this.$http.post(
  373. "/api/ecs_oe_student/examControl/getQrCode",
  374. {
  375. examRecordDataId,
  376. order: +this.examQuestion.order,
  377. transferFileType,
  378. testEnv: false,
  379. }
  380. );
  381. this.qrValue = response.data;
  382. }
  383. },
  384. photoAdded(url) {
  385. // console.log(url);
  386. this.photoAnswers = [...this.photoAnswers, url];
  387. },
  388. photoRemoved(url) {
  389. // console.log("to remove: ", url, " from: ", this.photoAnswers);
  390. // console.log(
  391. // "this.photoAnswers.filter(v => v !== url)",
  392. // this.photoAnswers.filter(v => v !== url)
  393. // );
  394. this.photoAnswers = this.photoAnswers.filter(v => v !== url);
  395. // console.log(this.photoAnswers);
  396. },
  397. photosReseted(urls) {
  398. this.photoAnswers = [...urls];
  399. },
  400. },
  401. };
  402. </script>
  403. <style scoped>
  404. .question-view {
  405. display: grid;
  406. grid-row-gap: 10px;
  407. }
  408. .question-body {
  409. font-size: 18px;
  410. margin-bottom: 10px;
  411. }
  412. .ops {
  413. display: flex;
  414. align-items: flex-end;
  415. }
  416. .text-ops {
  417. margin: 0 5px 5px 0;
  418. }
  419. .stu-answer {
  420. width: 100%;
  421. max-width: 500px;
  422. min-height: 300px;
  423. border: 1px solid grey;
  424. }
  425. .audio-answer {
  426. font-size: 24px;
  427. line-height: 54px;
  428. }
  429. .audio-answer-line-height {
  430. line-height: 54px;
  431. vertical-align: text-bottom;
  432. }
  433. </style>
  434. <style>
  435. .photo-answers-block {
  436. display: none;
  437. }
  438. </style>