pinPage.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!-- 单词区 && 音标区:最多14位,超过换行-->
  2. <!-- 单音节最长:swimming 多音节最长:transportation -->
  3. <template>
  4. <!-- 显示区 -->
  5. <!-- <selectTypesVue activeSelect="2"></selectTypesVue>-->
  6. <view class="ezy-border-body">
  7. <view class="words-pin-box">
  8. <!-- 拼读区 -->
  9. <!-- 单词字母多余6个需要追加 class:pin-small-words-box -->
  10. <view class="pin-words-box"
  11. :class="{'pin-small-words-box': wordLength > 6, 'isAll': data.isAll, 'pin-right-words-box': data.isAll && data.result, 'pin-error-words-box': data.isAll && !data.result}">
  12. <view class="words-item" v-for="item in data.selectList">{{item}}</view>
  13. </view>
  14. <view class="pin-body-box">
  15. <!-- 清空按钮 -->
  16. <ezyActiveVue class="ezy-btn-active clean-btn" v-if="isAlreadyAnswer" @click="handleReset"></ezyActiveVue>
  17. <!-- 提示 -->
  18. <view class="pin-tip" v-else>提示:请点击页面下方字母,选择正确答案。</view>
  19. <!-- 解释区-->
  20. <view class="pin-words-explain-box">
  21. <view class="words-explain-item" v-for="item in activeWord.jianyi" :key="item">{{item}}</view>
  22. </view>
  23. <audioOneVue @play-audio="handlePlay" :activeWord="activeWord"></audioOneVue>
  24. </view>
  25. <!-- 选择区 -->
  26. <view class="pin-words-box pin-words-change-box" :class="{'pin-small-words-box': wordLength>6}">
  27. <ezyActiveVue class="ezy-btn-active words-item words-change-item" v-for="(item,index) in data.randomList" :key="index"
  28. :class="{disabled: isSelect(item,index)}" @click="handleSelect(item,index)">{{item}}</ezyActiveVue>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script setup>
  34. import selectTypesVue from './selectTypes.vue';
  35. import audioOneVue from './audioOne.vue';
  36. import {
  37. reactive,
  38. computed,
  39. } from 'vue';
  40. import {
  41. onLoad
  42. } from "@dcloudio/uni-app"
  43. import * as httpApi from "@/api/chaojidanci.js"
  44. import {resultAudioPlayer} from "./useAudioRightWrong"
  45. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  46. const resultAudioPlayerD = new resultAudioPlayer();
  47. const emits = defineEmits(['play-audio'])
  48. const props = defineProps({
  49. activeWord: {
  50. type: Object,
  51. },
  52. activeWords: {
  53. type: Array
  54. },
  55. })
  56. function handlePlay(opt) {
  57. emits('play-audio', opt)
  58. }
  59. const data = reactive({
  60. list: [],
  61. randomList: [],
  62. selectList: [],
  63. result: false, // 正确性
  64. isAll: false, // 是否全答
  65. indexArr: [],
  66. })
  67. onLoad(() => {
  68. initItem()
  69. })
  70. const wordLength = computed(() => props.activeWord.name.length)
  71. const isAlreadyAnswer = computed(() => {
  72. return data.selectList.some(item => item != '')
  73. })
  74. function isSelect(item,index) {
  75. return data.indexArr.some(ii => ii == index)
  76. }
  77. function handleReset() {
  78. data.list.forEach((item, index) => {
  79. data.selectList[index] = ''
  80. })
  81. data.result = false;
  82. data.isAll = false;
  83. data.indexArr = [];
  84. }
  85. function shuffleArray(array) {
  86. for (let i = array.length - 1; i > 0; i--) {
  87. const j = Math.floor(Math.random() * (i + 1));
  88. [array[i], array[j]] = [array[j], array[i]]; // ES6解构赋值交换元素
  89. }
  90. return array;
  91. }
  92. function randomClone(arr) {
  93. const clone = [...arr];
  94. return shuffleArray(clone); // 复用方法一的洗牌算法
  95. }
  96. // 初始化 单词列表
  97. function initItem() {
  98. data.list = props.activeWord.chaifenPin;
  99. data.randomList = randomClone(data.list);
  100. data.list.forEach((item, index) => {
  101. data.selectList[index] = ''
  102. })
  103. }
  104. function handleSelect(word,mIndex) {
  105. emits('stop-audio');
  106. if (data.indexArr.some(ii => ii == mIndex)) {
  107. // 已选过 禁止再选
  108. return;
  109. }
  110. // 覆盖状态
  111. let status = false;
  112. data.selectList.forEach((item, index) => {
  113. // 无值 无修改
  114. if (!item && !status) {
  115. // 第一项空值覆盖
  116. data.selectList[index] = word;
  117. // 以有控制覆盖
  118. status = true;
  119. // 更新已选择的下标
  120. data.indexArr.push(mIndex);
  121. }
  122. })
  123. // 校验正确性
  124. checkIsRight();
  125. if (data.selectList.some(item => item == '')) {
  126. data.isAll = false;
  127. } else {
  128. data.isAll = true;
  129. if (data.isAll && !data.result) {
  130. resultAudioPlayerD.play('wrong','pin')
  131. }
  132. }
  133. }
  134. function checkIsRight() {
  135. if (data.list.join('') === data.selectList.join('')) {
  136. // 正确
  137. data.result = true;
  138. resultAudioPlayerD.play('right','pin')
  139. noticeBackDb();
  140. } else {
  141. data.result = false;
  142. }
  143. }
  144. function noticeBackDb() {
  145. httpApi.getWordZhangwo({
  146. type: 1,
  147. wordId: props.activeWord.id
  148. })
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. </style>