duoxuan.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <view v-if="question" class="phone-duoxuan-box">
  3. <view class="phone-shiti-question">
  4. <view class="question-num">{{question.onlyNum}}、</view>
  5. <!-- 题干区域 -->
  6. <rich-text :nodes="data.name"></rich-text>
  7. </view>
  8. <!-- 选项区域 -->
  9. <view v-for="(item,index) in data.contents" class="duoxuan-option-box" :class="formatClass(index)" :key="index">
  10. <text class="option-change" @click="onSelect(index)">{{item.number}}</text>
  11. <rich-text :nodes="item.label" class="option-question"></rich-text>
  12. </view>
  13. </view>
  14. </template>
  15. <script setup>
  16. import {
  17. ref,
  18. reactive,
  19. watch
  20. } from 'vue';
  21. import {
  22. useQuestionTools
  23. } from "./useQuestionTools"
  24. const {
  25. getLetterByIndex,
  26. haveSameElements
  27. } = useQuestionTools();
  28. const props = defineProps({
  29. question: {
  30. type: Object,
  31. },
  32. showError: {
  33. type: Boolean,
  34. default: false
  35. }
  36. })
  37. const data = reactive({
  38. name: '', //题干数据
  39. contents: [], // 选项数据
  40. })
  41. watch(() => props.question, (val) => formatData(val), {
  42. immediate: true
  43. })
  44. function formatClass(index) {
  45. if (props.showError) {
  46. return {
  47. active_right: props.question.result.some(item => item == index),
  48. showError: !props.question.result.some(item => item == index)
  49. }
  50. } else {
  51. return {
  52. active: props.question.reply.some(item => item == index)
  53. }
  54. }
  55. }
  56. function formatData(val) {
  57. if (val) {
  58. data.name = val.name;
  59. data.contents = val.content.map((item, index) => {
  60. return {
  61. label: item,
  62. number: getLetterByIndex(index)
  63. }
  64. })
  65. }
  66. }
  67. function onSelect(index) {
  68. if (props.showError) {
  69. return;
  70. }
  71. if (props.question.reply) {
  72. if (props.question.reply.some(item => item == index)) {
  73. props.question.reply = props.question.reply.filter(item => item != index);
  74. } else {
  75. props.question.reply.push(index);
  76. }
  77. }
  78. }
  79. </script>