QuestionAnswer.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div class="question-answer">
  3. <template v-if="quesAnswer">
  4. <div v-if="IS_SELECT_QUESTION || IS_BOOL_QUESTION">{{ quesAnswer }}</div>
  5. <div v-else-if="IS_FILL_QUESTION">
  6. <p v-for="(cont, cindex) in quesAnswer" :key="cindex">
  7. <span style="margin-right: 5px">({{ cindex + 1 }})</span>
  8. <rich-text :text-json="cont"></rich-text>
  9. </p>
  10. </div>
  11. <rich-text
  12. v-else-if="IS_TEXT_QUESTION"
  13. :text-json="quesAnswer"
  14. ></rich-text>
  15. <div v-else></div>
  16. </template>
  17. </div>
  18. </template>
  19. <script>
  20. // import { deepCopy } from "@/plugins/utils";
  21. export default {
  22. name: "QuestionAnswer",
  23. props: {
  24. data: {
  25. type: Object,
  26. default() {
  27. return {
  28. questionType: "",
  29. quesAnswer: "",
  30. };
  31. },
  32. },
  33. },
  34. data() {
  35. return {
  36. optionNames: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  37. quesAnswer: "",
  38. };
  39. },
  40. computed: {
  41. IS_SELECT_QUESTION() {
  42. return (
  43. this.data.questionType === "SINGLE_ANSWER_QUESTION" ||
  44. this.data.questionType === "MULTIPLE_ANSWER_QUESTION"
  45. );
  46. },
  47. IS_BOOL_QUESTION() {
  48. return this.data.questionType === "BOOL_ANSWER_QUESTION";
  49. },
  50. IS_FILL_QUESTION() {
  51. return this.data.questionType === "FILL_BLANK_QUESTION";
  52. },
  53. IS_TEXT_QUESTION() {
  54. return this.data.questionType === "TEXT_ANSWER_QUESTION";
  55. },
  56. },
  57. watch: {
  58. "data.quesAnswer": {
  59. handler() {
  60. this.initData();
  61. },
  62. },
  63. },
  64. mounted() {
  65. this.initData();
  66. },
  67. methods: {
  68. initData() {
  69. if (!this.data.quesAnswer) return;
  70. if (this.IS_BOOL_QUESTION) {
  71. this.quesAnswer = this.data.quesAnswer === "true" ? "正确" : "错误";
  72. return;
  73. }
  74. let quesAnswer = null;
  75. try {
  76. quesAnswer = JSON.parse(this.data.quesAnswer);
  77. } catch (error) {
  78. console.error("答案格式错误");
  79. }
  80. if (!quesAnswer) return;
  81. if (this.IS_SELECT_QUESTION) {
  82. this.quesAnswer = quesAnswer
  83. .map((item) => this.optionNames[item - 1])
  84. .join("");
  85. } else if (this.IS_FILL_QUESTION) {
  86. this.quesAnswer = quesAnswer;
  87. } else if (this.IS_TEXT_QUESTION) {
  88. this.quesAnswer = quesAnswer[0];
  89. }
  90. },
  91. },
  92. };
  93. </script>