pinPage.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <div>
  3. <!-- 单词区 -->
  4. <selectWordsVue :active-words="activeWords" :activeWord="activeWord"></selectWordsVue>
  5. <!-- 显示区 -->
  6. <selectTypesVue activeSelect="2"></selectTypesVue>
  7. <!-- 拼读区 -->
  8. <view>
  9. <view v-for="item in data.selectList">
  10. <view>内容:{{item}}</view>
  11. </view>
  12. </view>
  13. <!-- 解释区 -->
  14. <view>
  15. {{activeWord.jianyi.join(';')}}
  16. </view>
  17. <!-- 音标区 -->
  18. <view></view>
  19. <!-- 图片区 -->
  20. <view>
  21. {{data.result ? '正确': '错误'}}
  22. </view>
  23. <!-- 选择区 -->
  24. <view>
  25. <view v-for="item in data.randomList" @click="handleSelect(item)">
  26. <view>内容:{{item}}</view>
  27. </view>
  28. </view>
  29. </div>
  30. </template>
  31. <script setup>
  32. import selectWordsVue from './selectWords.vue';
  33. import selectTypesVue from './selectTypes.vue';
  34. import {
  35. reactive,
  36. computed
  37. } from 'vue';
  38. import {onLoad} from "@dcloudio/uni-app"
  39. import * as httpApi from "@/api/word.js"
  40. import {
  41. getUserIdentity,
  42. } from "@/utils/common.js"
  43. const userCode = getUserIdentity();
  44. const props = defineProps({
  45. activeWord: {
  46. type: Object,
  47. },
  48. activeWords: {
  49. type: Array
  50. },
  51. })
  52. const data = reactive({
  53. list: [],
  54. randomList: [],
  55. selectList: [],
  56. result: false, // 正确性
  57. })
  58. onLoad(() => {
  59. initItem()
  60. })
  61. setTimeout(() => {
  62. console.log('activeWord', props.activeWord)
  63. })
  64. function shuffleArray(array) {
  65. for (let i = array.length - 1; i > 0; i--) {
  66. const j = Math.floor(Math.random() * (i + 1));
  67. [array[i], array[j]] = [array[j], array[i]]; // ES6解构赋值交换元素
  68. }
  69. return array;
  70. }
  71. function randomClone(arr) {
  72. const clone = [...arr];
  73. return shuffleArray(clone); // 复用方法一的洗牌算法
  74. }
  75. // 初始化 单词列表
  76. function initItem() {
  77. data.list = props.activeWord.chaifen;
  78. data.randomList = randomClone(data.list);
  79. data.list.forEach((item, index) => {
  80. data.selectList[index] = ''
  81. })
  82. }
  83. function handleSelect(word) {
  84. if (data.selectList.find(item => item == word)) {
  85. data.selectList[data.selectList.findIndex(item => item == word)] = '';
  86. // 校验正确性
  87. checkIsRight();
  88. return;
  89. }
  90. // 覆盖状态
  91. let status = false;
  92. data.selectList.forEach((item, index) => {
  93. // 无值 无修改
  94. if (!item && !status) {
  95. // 第一项空值覆盖
  96. data.selectList[index] = word;
  97. // 以有控制覆盖
  98. status = true;
  99. }
  100. })
  101. // 校验正确性
  102. checkIsRight();
  103. }
  104. function checkIsRight() {
  105. if (data.list.join('') === data.selectList.join('')) {
  106. // 正确
  107. data.result = true;
  108. noticeBackDb();
  109. } else {
  110. data.result = false;
  111. }
  112. }
  113. function noticeBackDb() {
  114. if (userCode == 'Visitor') {
  115. // 游客不更新后台
  116. return;
  117. }
  118. httpApi.getWordZhangwo({
  119. type: 1,
  120. wordId: props.activeWord.id
  121. })
  122. }
  123. </script>
  124. <style lang="scss" scoped>
  125. </style>