duoxuan.vue 2.0 KB

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