QuestionBody.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <div v-if="questionDetail" class="question-body" :key="examQuestionId">
  3. <div v-html="questionDetail.text"></div>
  4. <div v-html="questionDetail.audio"></div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: "QuestionBody",
  10. data() {
  11. return { questionDetail: null };
  12. },
  13. props: {
  14. questionBody: String,
  15. examQuestionId: String
  16. },
  17. mounted() {
  18. const audio = document.getElementsByTagName("audio")[0];
  19. audio &&
  20. audio.addEventListener("play", () => {
  21. console.log("开始播放");
  22. //FIXME: error
  23. this.$http
  24. .post("/api/exam_question_playtimes", {
  25. questionId: this.examQuestionId
  26. // mediaName: mediaName
  27. })
  28. .then(
  29. function() {
  30. console.log("保存播放次数成功");
  31. },
  32. function() {
  33. console.log("保存播放次数失败");
  34. }
  35. );
  36. });
  37. this.parseQuestion();
  38. },
  39. methods: {
  40. async parseQuestion() {
  41. let question = {};
  42. if (this.questionBody.includes("question-audio")) {
  43. const audioArray = this.questionBody.match(
  44. /<a.*?question-audio.*?\/a>/g
  45. );
  46. question.text = this.questionBody.replace(
  47. /<a.*?question-audio.*?\/a>/g,
  48. ""
  49. );
  50. question.audio = audioArray
  51. .join("")
  52. .replace(/<a/g, "<audio controls controlsList='nodownload'")
  53. .replace(/url=/g, "src=")
  54. .replace(/a>/g, "audio>");
  55. this.$http
  56. .get("/api/exam_question_playtimes", {
  57. params: {
  58. questionId: this.examQuestionId
  59. }
  60. })
  61. .then(res => {
  62. //FIXME: audio playtimes
  63. console.log(res.data);
  64. });
  65. } else {
  66. question.text = this.questionBody;
  67. question.audio = "";
  68. }
  69. this.questionDetail = question;
  70. }
  71. },
  72. computed: {},
  73. watch: {
  74. questionBody() {
  75. this.parseQuestion();
  76. }
  77. }
  78. };
  79. </script>
  80. <style >
  81. .question-body audio {
  82. width: 180px;
  83. }
  84. .question-body audio::-webkit-media-controls-timeline,
  85. .question-body audio::-webkit-media-controls-timeline-container {
  86. display: none;
  87. }
  88. </style>