123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <template>
- <view v-html="formattedText" :myflag="myflag" :question="question" @click.native="handleClick"
- :change:myflag="YY.updateFlag" :change:question="YY.watchQuestionChange"></view>
- </template>
- <script>
- import {
- debounce
- } from '@/utils/common';
- export default {
- props: {
- placeholders: { // 占位符
- type: Array,
- required: true
- },
- text: {
- type: String,
- },
- question: {
- type: Object,
- },
- code: {
- type: String
- }
- },
- data() {
- return {
- myflag: 0,
- isplaying: null,
- isFirst: -1,
- }
- },
- computed: {
- // 计算属性,用于生成填空后的文本
- formattedText() {
- let result = this.text;
- this.placeholders.forEach((placeholder, index) => {
- // 使用正则表达式全局替换占位符
- const regex = new RegExp(`\\${placeholder}`, 'g');
- if (!this.isplaying) {
- const cId = this.code ? `t_${this.question.stId}_${index}_${this.code}`:`t_${this.question.stId}_${index}`
- result = result.replace(regex,
- `<view class="yingyu-canplay-img" id="${cId}"></view>`
- );
- } else if (this.isplaying && this.question.stId == this.isplaying.stId && this.isplaying.index == index) {
- const cId = this.code ? `t_${this.question.stId}_${index}_${this.code}`:`t_${this.question.stId}_${index}`
- result = result.replace(regex,
- `<view class="yingyu-playing-img" id="${cId}"></view>`
- );
- } else {
- const cId = this.code ? `t_${this.question.stId}_${index}_${this.code}`:`t_${this.question.stId}_${index}`
- result = result.replace(regex,
- `<view class="yingyu-canplay-img" id="${cId}"></view>`
- );
- }
- });
- this.myflag++;
- return result;
- },
- },
- methods: {
- handleClick() {
- this.$emit('@itemclick')
- },
- audioClick(data) {
- if (this.isFirst >= 0) {
- uni.$emit('do-yy-audio-stop', data);
- this.isFirst = -1
- return;
- }
- if (this.isplaying) {
- uni.$emit('do-yy-audio-stop', data);
- this.isFirst++;
- return;
- }
- // 初次播放
- if (!this.isplaying) {
- uni.$emit('do-yy-audio-play', data);
- this.isFirst++;
- return;
- }
- }
- },
- created() {
- // 切换不同播放音频时 重置状态
- uni.$on('reset-playing-status', (data) => {
- if (this.isplaying) {
- if (data.stId == this.isplaying.stId && data.index == this.isplaying.index) {
- this.isplaying = null;
- }
- }
- })
- // 音频播放
- uni.$on('yy-audio-playing', (data) => {
- if (this.isplaying) {
- // 存在播放实例 并且播放非同一音频
- if (this.isplaying.stId != data.value.stId) {
- this.isplaying = null;
- }
- } else {
- // 不存在播放实例
- this.isplaying = data;
- }
- })
- // 音频异常 重置音频
- uni.$on('yy-audio-error', (data) => {
- this.isplaying = null;
- })
- // 音频自然播放结束 重置音频
- uni.$on('yy-audio-end', (data) => {
- this.isplaying = null;
- })
- // 音频销毁 重置音频
- uni.$on('destory-stop', (data) => {
- this.isplaying = null;
- })
- // 试题切换 重置音频
- uni.$on('swiper-change', () => {
- this.isplaying = null;
- })
- // 解析关闭 重置音频
- uni.$on('question-jiexi-close', () => {
- this.isplaying = null;
- })
- uni.$on('unitTest-submit', () => {
- this.isplaying = null;
- })
- }
- }
- </script>
- <script module="YY" lang="renderjs">
- export default {
- props: {
- placeholders: { // 占位符
- type: Array,
- required: true
- },
- code: {
- type: String
- }
- },
- data() {
- return {
- myQ: null,
- callbacks: []
- }
- },
- methods: {
- debounce(func, wait) {
- let timeout;
- return function(...args) {
- // 清除之前的定时器
- clearTimeout(timeout);
- // 设置新的定时器
- timeout = setTimeout(() => {
- func.apply(this, args);
- }, wait);
- };
- },
- updateFlag() {
- this.initListener(this.myQ)
- },
- updateFn({
- stId,
- index
- }) {
- const url = this.myQ.audioList[index]
- this.$ownerInstance.callMethod('audioClick', {
- stId,
- index,
- url
- });
- },
- watchQuestionChange(newValue, oldValue, ownerInstance, instance) {
- if (newValue) {
- this.myQ = newValue;
- this.initListener(newValue)
- }
- },
- initListener(question) {
- if (!question) {
- return;
- }
- question.placeholders.forEach((item, index) => {
- const cId = this.code ? `t_${question.stId}_${index}_${this.code}` :
- `t_${question.stId}_${index}`
- const dom = document.getElementById(cId)
- if (dom) {
- const qaindex = this.callbacks.findIndex(item => item.index === index)
- if (qaindex > -1) {
- dom && dom.removeEventListener('click', this.callbacks[qaindex].callback);
- this.callbacks = this.callbacks.filter(item => item.index != qaindex);
- }
- const callbackFn = this.debounce(() => {
- this.updateFn({
- stId: question.stId,
- index
- })
- }, 100)
- this.callbacks.push({
- index: index,
- callback: callbackFn
- })
- dom && dom.addEventListener('click', this.callbacks.find(cIte => cIte.index === index)
- .callback)
- }
- })
- },
- },
- }
- </script>
- <style>
- </style>
|