pinPage.vue 4.5 KB

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