pinPage.vue 4.4 KB

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