mx-question.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <view class="mx-question tabs-swiper-content">
  3. <mx-question-content ref="q" v-model="userData.answer" :disabled="!allowAnswer" :sys-answer="sysAnswerIf"
  4. v-model:attachments="userData.attachments" :question="question" class="p-40"/>
  5. <mx-question-parse v-if="!allowAnswer" :question="question" class="p-40"/>
  6. </view>
  7. </template>
  8. <script setup>
  9. import {computed, watch, ref, nextTick} from 'vue'
  10. import {createPropDefine} from "@/utils";
  11. import {useInjectQuestionService} from "@/components/mx-question/useQuestionInjection";
  12. import {useInjectPaperService} from "@/components/mx-paper/usePaperInjection";
  13. import {array} from "@/uni_modules/uv-ui-tools/libs/function/test";
  14. import MxQuestionParse from "@/components/mx-question/components/mx-question-parse.vue";
  15. import {sleep} from "@/uni_modules/uv-ui-tools/libs/function";
  16. const props = defineProps({
  17. question: createPropDefine({}, Object),
  18. autoNext: createPropDefine(true, Boolean),
  19. // 自动随机答题,有时候题太多了,测试不方便,注意随时关闭 !important.
  20. autoAnswer: createPropDefine(false, Boolean)
  21. })
  22. const q = ref(null)
  23. const {isRadio, tryGoNext, index, current, questions, hasNext, allowAnswer} = useInjectPaperService()
  24. const {getUserData, pushChunk, isAnswered, isScored} = useInjectQuestionService()
  25. const userData = computed(() => getUserData(props.question) || {})
  26. const sysAnswerIf = computed(() => allowAnswer.value ? '' : array(props.question.answers) ? props.question.answers[0] : '')
  27. watch(index, async () => {
  28. if (!props.autoAnswer) return
  29. if (isAnswered(current)) return
  30. await nextTick()
  31. q.value?.tryRandomAnswer()
  32. }, {immediate: true})
  33. watch([() => userData.value.answer, () => userData.value.attachments], async () => {
  34. pushChunk(userData.value)
  35. // 自动前往一下题
  36. if (props.autoNext && isRadio(props.question) && hasNext.value) {
  37. const next = questions.value[index.value + 1]
  38. if (!isAnswered(next)) {
  39. await sleep(300)
  40. tryGoNext()
  41. }
  42. }
  43. })
  44. watch(() => userData.value.score, async () => {
  45. pushChunk(userData.value)
  46. // 自动前往下一题
  47. if (props.autoNext && hasNext.value) {
  48. const next = questions.value[index.value + 1]
  49. if (!isScored(next)) {
  50. await sleep(300)
  51. tryGoNext()
  52. }
  53. }
  54. })
  55. </script>
  56. <style scoped>
  57. </style>