QuestionBody.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <div
  3. v-if="questionDetail"
  4. :key="examQuestion.questionId"
  5. class="question-body"
  6. >
  7. <QuestionContainer
  8. :node="questionDetail.container"
  9. :exam-question="examQuestion"
  10. />
  11. <!-- <div v-html="questionDetail.text"></div> -->
  12. <!-- <div v-html="questionDetail.audio"></div> -->
  13. <!-- <div
  14. v-for="{ src, name } in questionDetail.audio"
  15. :key="name"
  16. class="audio-container"
  17. >
  18. <div style="position: relative; margin-bottom: 2px;" class="audio-div">
  19. <QuestionAudio
  20. key="src"
  21. :name="name"
  22. :src="src"
  23. :play-count="
  24. examQuestion.limitedPlayTimes ? getAudioPlayedTimes(name) : 1
  25. "
  26. @played="played(name)"
  27. />
  28. <span v-if="examQuestion.limitedPlayTimes">
  29. (剩余播放次数:{{ getAudioPlayedTimes(name) }})
  30. </span>
  31. <div
  32. v-if="audioInPlay.has(name)"
  33. style="position: absolute;top: 0;right: 0;bottom: 0;left: 0;"
  34. ></div>
  35. </div>
  36. </div> -->
  37. </div>
  38. <div v-else>获取试题中...</div>
  39. </template>
  40. <script>
  41. // import QuestionAudio from "./QuestionAudio";
  42. import QuestionContainer from "./QuestionContainer";
  43. import { createNamespacedHelpers } from "vuex";
  44. const { mapState, mapMutations } = createNamespacedHelpers("examingHomeModule");
  45. export default {
  46. name: "QuestionBody",
  47. components: {
  48. // QuestionAudio,
  49. QuestionContainer,
  50. },
  51. props: {
  52. questionBody: { type: String, default: "" },
  53. examQuestion: {
  54. type: Object,
  55. default() {
  56. return {};
  57. },
  58. },
  59. },
  60. data() {
  61. return {
  62. questionDetail: null,
  63. audioPlayTimes: null,
  64. audioInPlay: new Set(),
  65. };
  66. },
  67. computed: {
  68. ...mapState(["allAudioPlayTimes"]),
  69. },
  70. watch: {
  71. questionBody() {
  72. this.parseQuestion();
  73. },
  74. examQuestion() {
  75. this.parseQuestion();
  76. },
  77. },
  78. mounted() {
  79. this.parseQuestion();
  80. // this.$nextTick(() => {
  81. // const ap = document.querySelectorAll(".audio-placeholder");
  82. // const as = document.querySelectorAll(".audio-container");
  83. // [...ap].forEach((v, i) => (v.innerHTML = [...as][i].innerHTML));
  84. // });
  85. },
  86. methods: {
  87. ...mapMutations(["updateExamQuestion", "updateQuestionAudioPlayTimes"]),
  88. async parseQuestion() {
  89. let question = {};
  90. const container = document.createElement("div");
  91. if (this.questionBody.includes("question-audio")) {
  92. question.text = this.questionBody.replace(
  93. /<a.*?name="([^"]*)".*?question-audio.*?url="([^"]*)".*?\/a>/g,
  94. "<span class='audio-placeholder' data-name='$1' data-src='$2'></span>"
  95. );
  96. // console.log(question.text);
  97. // 测试 v-html中含有directive是否生效。结论:不生效
  98. question.audio = [];
  99. let re;
  100. if (/<a.*?question-audio.*?\/a>/.test(this.questionBody)) {
  101. re = /name="([^"]*)".*?question-audio.*?url="([^"]*)"/g;
  102. }
  103. let array1;
  104. while ((array1 = re.exec(this.questionBody)) !== null) {
  105. question.audio.push({ name: array1[1], src: array1[2] });
  106. }
  107. } else {
  108. question.text = this.questionBody;
  109. question.audio = [];
  110. }
  111. container.innerHTML = question.text;
  112. question.container = container;
  113. this.questionDetail = question;
  114. },
  115. // played(name) {
  116. // if (!this.examQuestion.limitedPlayTimes) {
  117. // // 如果没有设置音频播放次数,可无限播放
  118. // return false;
  119. // }
  120. // console.log("开始播放");
  121. // // 正在播放中
  122. // this.updateQuestionAudioPlayTimes(name);
  123. // },
  124. // getAudioPlayedTimes(name) {
  125. // return Math.max(
  126. // this.examQuestion.limitedPlayTimes -
  127. // (this.allAudioPlayTimes.find(a => a.name === name) || { times: 0 })
  128. // .times,
  129. // 0
  130. // );
  131. // },
  132. },
  133. };
  134. </script>
  135. <style>
  136. .audio-div {
  137. display: inline-flex;
  138. align-items: center;
  139. }
  140. /* .question-body audio::-webkit-media-controls-timeline,
  141. .question-body audio::-webkit-media-controls-timeline-container {
  142. display: none;
  143. } */
  144. </style>