pinPage.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. <view class="clean-btn" v-if="isAlreadyAnswer" @click="handleReset"></view>
  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. <view class="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}}</view>
  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/word.js"
  44. import {
  45. getUserIdentity,
  46. } from "@/utils/common.js"
  47. import {resultAudioPlayer} from "./useAudioRightWrong"
  48. const resultAudioPlayerD = new resultAudioPlayer();
  49. const emits = defineEmits(['play-audio'])
  50. const userCode = getUserIdentity();
  51. const props = defineProps({
  52. activeWord: {
  53. type: Object,
  54. },
  55. activeWords: {
  56. type: Array
  57. },
  58. })
  59. function handlePlay(opt) {
  60. emits('play-audio', opt)
  61. }
  62. const data = reactive({
  63. list: [],
  64. randomList: [],
  65. selectList: [],
  66. result: false, // 正确性
  67. isAll: false, // 是否全答
  68. indexArr: [],
  69. })
  70. onLoad(() => {
  71. initItem()
  72. })
  73. const wordLength = computed(() => props.activeWord.name.length)
  74. const isAlreadyAnswer = computed(() => {
  75. return data.selectList.some(item => item != '')
  76. })
  77. function isSelect(item,index) {
  78. return data.indexArr.some(ii => ii == index)
  79. }
  80. function handleReset() {
  81. data.list.forEach((item, index) => {
  82. data.selectList[index] = ''
  83. })
  84. data.result = false;
  85. data.isAll = false;
  86. data.indexArr = [];
  87. }
  88. function shuffleArray(array) {
  89. for (let i = array.length - 1; i > 0; i--) {
  90. const j = Math.floor(Math.random() * (i + 1));
  91. [array[i], array[j]] = [array[j], array[i]]; // ES6解构赋值交换元素
  92. }
  93. return array;
  94. }
  95. function randomClone(arr) {
  96. const clone = [...arr];
  97. return shuffleArray(clone); // 复用方法一的洗牌算法
  98. }
  99. // 初始化 单词列表
  100. function initItem() {
  101. data.list = props.activeWord.chaifenPin;
  102. data.randomList = randomClone(data.list);
  103. data.list.forEach((item, index) => {
  104. data.selectList[index] = ''
  105. })
  106. }
  107. function handleSelect(word,mIndex) {
  108. if (data.indexArr.some(ii => ii == mIndex)) {
  109. // 已选过 禁止再选
  110. return;
  111. }
  112. // 覆盖状态
  113. let status = false;
  114. data.selectList.forEach((item, index) => {
  115. // 无值 无修改
  116. if (!item && !status) {
  117. // 第一项空值覆盖
  118. data.selectList[index] = word;
  119. // 以有控制覆盖
  120. status = true;
  121. // 更新已选择的下标
  122. data.indexArr.push(mIndex);
  123. }
  124. })
  125. // 校验正确性
  126. checkIsRight();
  127. if (data.selectList.some(item => item == '')) {
  128. data.isAll = false;
  129. } else {
  130. data.isAll = true;
  131. if (data.isAll && !data.result) {
  132. resultAudioPlayerD.play('wrong','pin')
  133. }
  134. }
  135. }
  136. function checkIsRight() {
  137. if (data.list.join('') === data.selectList.join('')) {
  138. // 正确
  139. data.result = true;
  140. resultAudioPlayerD.play('right','pin')
  141. noticeBackDb();
  142. } else {
  143. data.result = false;
  144. }
  145. }
  146. function noticeBackDb() {
  147. if (userCode == 'Visitor') {
  148. // 游客不更新后台
  149. return;
  150. }
  151. httpApi.getWordZhangwo({
  152. type: 1,
  153. wordId: props.activeWord.id
  154. })
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. </style>