danxuanCeshi.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view>
  3. <view>12312312</view>
  4. <!-- 选项区域 -->
  5. <view v-for="(item,index) in data.contents" class="yingyu-danxuan-option-box" :key="index">
  6. <text class="option-change">{{item.number}}</text>
  7. <view v-if="item.placeholders.length ==1" @click="ceshiAA(item,index)">
  8. <rich-text :nodes="item.neirong" class="option-question"></rich-text>
  9. </view>
  10. <view v-else v-for="(item2,index2) in item.placeholders" @click="ceshiBB(item2,index2)">
  11. <rich-text :nodes="item.neirong[index2]" class="option-question"></rich-text>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script setup>
  17. import {
  18. ref,
  19. reactive,
  20. onMounted,
  21. watch
  22. } from 'vue';
  23. import {
  24. useQuestionTools
  25. } from "../useQuestionTools"
  26. import textReplaceIconVue from './textReplaceIcon.vue'
  27. let richTextNodes = [{
  28. name: 'div',
  29. attrs: {
  30. class: 'div-class',
  31. },
  32. children: [{
  33. type: 'text',
  34. text: 'Hello&nbsp;uni-app!'
  35. }]
  36. }]
  37. const {
  38. getLetterByIndex
  39. } = useQuestionTools();
  40. const props = defineProps({
  41. question: {
  42. type: Object,
  43. },
  44. showError: {
  45. type: Boolean,
  46. default: false
  47. },
  48. placeholders: { // 占位符
  49. type: Array,
  50. required: true
  51. },
  52. code: {
  53. type: String,
  54. }
  55. })
  56. const data = reactive({
  57. name: '', //题干数据
  58. contents: [], // 选项数据
  59. })
  60. let modifiedHtml = ''
  61. let index11 = ''
  62. let number = 0
  63. const iconHtml = ref('<p class="yingyu-canplay-img" data-index="0">喇叭1</p>')
  64. const dynamicContentRef = ref(null);
  65. watch(() => props.question, (val) => formatData(val), {
  66. immediate: true
  67. })
  68. function itemclick(event) {
  69. console.log('event', event);
  70. }
  71. function ceshiAA(item,index){
  72. console.log('item', item);
  73. console.log('index', index);
  74. }
  75. function ceshiBB(item,index){
  76. console.log('item2', item);
  77. console.log('index2', index);
  78. }
  79. function formatData(val) {
  80. if (val) {
  81. data.name = val.name;
  82. data.contents = val.optList.map((item, index) => {
  83. // console.log('item,',item);
  84. const iconHtml =
  85. `<img class="yingyu-canplay-img">`;; // 这里使用了一个简单的图标符号
  86. modifiedHtml = item.neirong.replace(/&&&/g, iconHtml);
  87. let regex = /<p[>]*>([\s\S]*?)<\/p>/g;
  88. let pTagCount = (modifiedHtml.match(/<p>/g) || []).length;
  89. let result;
  90. if (pTagCount === 1) {
  91. // 如果只有一个<p>标签,保持原样
  92. result = modifiedHtml;
  93. } else if (pTagCount > 1) {
  94. // 如果有多个<p>标签,转换成数组
  95. result = modifiedHtml.match(regex);
  96. } else {
  97. // 如果没有<p>标签,可以返回一个空数组或null,根据实际需求决定
  98. result = [];
  99. }
  100. // console.log('modifiedHtml', modifiedHtml);
  101. return {
  102. neirong: result,
  103. number: getLetterByIndex(index),
  104. placeholders: getPlaceholders(item.neirong)
  105. }
  106. })
  107. console.log('data', data);
  108. }
  109. }
  110. function getPlaceholders(html) {
  111. console.log('html',html);
  112. // 使用正则表达式匹配所有&&&,并返回一个数组,每个元素代表一个占位符
  113. return html.match(/&&&/g) || [];
  114. }
  115. let audioContext = null;
  116. let isPlaying = false;
  117. let currentIndex = -1;
  118. function initAudioContext() {
  119. if (!audioContext) {
  120. audioContext = uni.createInnerAudioContext();
  121. // 监听音频播放状态
  122. audioContext.onPlay(() => {
  123. isPlaying = true;
  124. console.log('音频开始播放');
  125. });
  126. audioContext.onPause(() => {
  127. isPlaying = false;
  128. console.log('音频暂停播放');
  129. });
  130. audioContext.onStop(() => {
  131. isPlaying = false;
  132. console.log('音频停止播放');
  133. });
  134. audioContext.onEnded(() => {
  135. isPlaying = false;
  136. console.log('音频播放结束');
  137. });
  138. }
  139. }
  140. function playAudio(src) {
  141. if (!audioContext) {
  142. initAudioContext();
  143. }
  144. audioContext.src = src;
  145. audioContext.play();
  146. }
  147. function audioCreat(item, index, placeholderIndex) {
  148. console.log('item', item);
  149. console.log('index', index);
  150. console.log('placeholderIndex', placeholderIndex);
  151. return
  152. // 如果 index 变化了,或者当前没有音频在播放,就播放新音频
  153. if (index !== currentIndex || !isPlaying) {
  154. // 如果之前有音频在播放,先暂停
  155. if (isPlaying) {
  156. audioContext.pause();
  157. }
  158. // 更新当前索引和播放状态
  159. currentIndex = index;
  160. playAudio(item.audio);
  161. } else {
  162. // 如果 index 没变化且音频正在播放,就暂停音频
  163. if (isPlaying) {
  164. audioContext.pause();
  165. } else {
  166. // 如果 index 没变化但音频没播放,就播放音频
  167. playAudio(item.audio);
  168. }
  169. }
  170. }
  171. </script>