QuestionMixin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // 题目混入
  2. import QuestionBase from './QuestionBase.vue';
  3. import RichText from '@/components/common/RichText.vue';
  4. import AudioPlay from '@/views/exercise_questions/create/components/common/AudioPlay.vue';
  5. import { GetQuestionInfo, SaveQuestion } from '@/api/exercise';
  6. import {
  7. stemTypeList,
  8. scoreTypeList,
  9. switchOption,
  10. isEnable,
  11. questionNumberTypeList,
  12. computedQuestionNumber,
  13. fontSizeList,
  14. } from '@/views/exercise_questions/data/common';
  15. const mixin = {
  16. data() {
  17. return {
  18. fontSizeList,
  19. stemTypeList,
  20. scoreTypeList,
  21. switchOption,
  22. isEnable,
  23. questionNumberTypeList,
  24. computedQuestionNumber,
  25. };
  26. },
  27. provide: ['refreshPreviewData'],
  28. props: {
  29. questionId: {
  30. type: String,
  31. default: '',
  32. },
  33. isChild: {
  34. type: Boolean,
  35. default: false,
  36. },
  37. isChange: {
  38. type: Boolean,
  39. default: false,
  40. },
  41. },
  42. components: {
  43. QuestionBase,
  44. RichText,
  45. AudioPlay,
  46. },
  47. created() {
  48. // 题目的子题目保存
  49. if (this.isChild) {
  50. if (this.isChange) return;
  51. GetQuestionInfo({ question_id: this.questionId })
  52. .then(({ question }) => {
  53. if (!question.content) return;
  54. this.data = JSON.parse(question.content);
  55. this.refreshPreviewData();
  56. })
  57. .catch(() => {});
  58. }
  59. },
  60. watch: {
  61. 'data.property.score'(val) {
  62. if (val === undefined) return;
  63. this.data.answer.score = val;
  64. },
  65. 'data.property.score_type'(val) {
  66. if (val === undefined) return;
  67. this.data.answer.score_type = val;
  68. },
  69. data: {
  70. handler() {
  71. if (!this.isChild) return;
  72. this.$emit('updatePreviewData', this.data);
  73. },
  74. deep: true,
  75. immediate: true,
  76. },
  77. },
  78. methods: {
  79. saveChildQuestion() {
  80. SaveQuestion({
  81. question_id: this.questionId,
  82. type: this.data.type,
  83. additional_type: this.data.property.select_type || '',
  84. content: JSON.stringify(this.data),
  85. }).catch(() => {});
  86. },
  87. upload(file_id) {
  88. this.data.file_id_list.push(file_id);
  89. },
  90. deleteFile(file_id) {
  91. let index = this.data.file_id_list.indexOf(file_id);
  92. if (index !== -1) {
  93. this.data.file_id_list.splice(index, 1);
  94. }
  95. },
  96. /**
  97. * 设置题目内容
  98. * @param {object} content 题目内容
  99. */
  100. setQuestion(content) {
  101. this.data = content;
  102. },
  103. /**
  104. * 单独设置题号
  105. * @param {string} question_number 题号
  106. */
  107. setQuestionNumber(question_number) {
  108. this.data.property.question_number = question_number;
  109. },
  110. /**
  111. * 删除选项
  112. * @param {Number} i 索引
  113. */
  114. deleteOption(i) {
  115. this.data.option_list.splice(i, 1);
  116. },
  117. },
  118. };
  119. export default mixin;