TextQuestionView.vue 14 KB

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