pinPage.vue 4.2 KB

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