EditorQuestion.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <div class="page_dialog">
  3. <el-dialog
  4. :title="title"
  5. v-model="dialogVisible"
  6. :close-on-click-modal="false"
  7. :width="answerOption.questionType == 2 ? '850px' : '480px'"
  8. class="page_dialog"
  9. >
  10. <div class="padding_20" style="padding-bottom: 10px;">
  11. <div class="dialog_center">
  12. <el-form ref="formRef" :model="answerOption" label-width="120px">
  13. <el-form-item label="题目名称:">
  14. <el-input v-model="answerOption.questionName" placeholder="请输入"></el-input>
  15. </el-form-item>
  16. <el-form-item label="题目类型:">
  17. <el-select
  18. :popper-append-to-body="false"
  19. v-model="answerOption.questionType"
  20. @change="TypeChange"
  21. >
  22. <el-option :value="1" label="单选题"></el-option>
  23. <el-option :value="2" label="多选题"></el-option>
  24. <el-option :value="3" label="判断题" v-if="answerOption.optionsSize == 2"></el-option>
  25. </el-select>
  26. </el-form-item>
  27. <!-- 单选题标准答案 -->
  28. <el-form-item label="标准答案:" v-if="answerOption.questionType == 1">
  29. <el-select v-model="answerOption.standardAnswer" placeholder="请选择">
  30. <el-option
  31. :value="answerList[index]"
  32. :label="answerList[index]"
  33. v-for="(item, index) in answerOption.optionsSize"
  34. :key="index"
  35. ></el-option>
  36. </el-select>
  37. </el-form-item>
  38. <!-- 判断题标准答案 -->
  39. <el-form-item label="标准答案:" v-if="answerOption.questionType == 3">
  40. <el-select v-model="answerOption.standardAnswer" placeholder="请选择">
  41. <el-option value="T" label="T"></el-option>
  42. <el-option value="F" label="F"></el-option>
  43. </el-select>
  44. </el-form-item>
  45. <!-- 多选题标准答案 -->
  46. <el-form-item label="标准答案:" v-if="answerOption.questionType == 2">
  47. <el-checkbox-group v-model="mulAnswerList" @change="MulAnswerChange">
  48. <el-checkbox
  49. v-for="(item, index) in answerOption.optionsSize"
  50. :key="index"
  51. :label="answerList[index]"
  52. :value="answerList[index]"
  53. ></el-checkbox>
  54. </el-checkbox-group>
  55. </el-form-item>
  56. <el-form-item label="满分:">
  57. <el-input v-model="answerOption.fullScore" maxlength="2" placeholder="请输入"></el-input>
  58. </el-form-item>
  59. </el-form>
  60. </div>
  61. <!-- 多选题给分规则 -->
  62. <div class="dialog_group" v-if="answerOption.questionType == 2">
  63. <div class="radio_group_socre">
  64. <el-radio v-model="answerOption.multipleRule" label="1">
  65. <span class="radio_title">给分规则1:</span>全对得满分,少选得
  66. <el-input
  67. v-model="ruleOneScore"
  68. class="radio_score"
  69. maxlength="3"
  70. @input="ScoreChange('ruleOneScore')"
  71. ></el-input>分,选错不得分。
  72. </el-radio>
  73. </div>
  74. <div class="radio_group_socre">
  75. <el-radio v-model="answerOption.multipleRule" label="2">
  76. <span class="radio_title">给分规则2:</span>全对得满分,选对1个
  77. <el-input
  78. v-model="ruleTwoScore1"
  79. class="radio_score"
  80. @input="ScoreChange('ruleTwoScore1')"
  81. maxlength="3"
  82. ></el-input>分,
  83. 选对2个
  84. <el-input
  85. v-model="ruleTwoScore2"
  86. class="radio_score"
  87. @input="ScoreChange('ruleTwoScore2')"
  88. maxlength="3"
  89. ></el-input>分,
  90. 选对3个
  91. <el-input
  92. v-model="ruleTwoScore3"
  93. class="radio_score"
  94. @input="ScoreChange('ruleTwoScore3')"
  95. maxlength="3"
  96. ></el-input>分,选错不得分
  97. </el-radio>
  98. </div>
  99. <div class="radio_group_long_input">
  100. <el-radio v-model="answerOption.multipleRule" label="3">
  101. <span class="radio_title">给分规则3:</span>
  102. <el-input
  103. placeholder="A=1,B=2,C=3"
  104. v-model="ruleThreeStr"
  105. class="input_long_200"
  106. ></el-input>
  107. </el-radio>
  108. </div>
  109. </div>
  110. </div>
  111. <template #footer>
  112. <div class="dialog-footer">
  113. <el-button @click="dialogVisible = false">取 消</el-button>
  114. <el-button type="primary" @click="ConfirmEnter()">确 定</el-button>
  115. </div>
  116. </template>
  117. </el-dialog>
  118. </div>
  119. </template>
  120. <script setup>
  121. import { ref, reactive } from 'vue';
  122. import { ElMessage } from 'element-plus';
  123. const emit = defineEmits(['answerEnter']);
  124. const dialogVisible = ref(false);
  125. const title = ref('编辑');
  126. const formRef = ref(null);
  127. const mulAnswerList = ref([]); // 多选答案
  128. const answerList = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; // 答案列表
  129. // 标准答案编辑对象数据
  130. const answerOption = reactive({
  131. examSubjectId: '', // 考试科目id
  132. fullScore: 0, // 满分
  133. id: '', // id 修改id
  134. multipleRule: '', // 多选规则 类型 1 2 3
  135. multipleRuleDesc: null, // 多选题给分分布
  136. optionsSize: 4, // 答案数量
  137. standardAnswer: "", // 标准答案
  138. questionName: "", // 题目名称
  139. questionType: "1", // 题目类型 1 单选 2多选 3判断
  140. });
  141. // 给分规则输入框
  142. const ruleOneScore = ref(0);
  143. const ruleTwoScore1 = ref(0);
  144. const ruleTwoScore2 = ref(0);
  145. const ruleTwoScore3 = ref(0);
  146. const ruleThreeStr = ref('');
  147. // 初始化数据
  148. const InitData = (item) => {
  149. Object.assign(answerOption, {
  150. examSubjectId: item.examSubjectId,
  151. fullScore: item.fullScore, // 满分
  152. id: item.id,
  153. multipleRule: String(item.multipleRule || '1'),//默认第一个
  154. multipleRuleDesc: item.multipleRuleDesc,
  155. optionsSize: item.optionsSize,
  156. standardAnswer: item.standardAnswer,
  157. questionName: item.questionName,
  158. questionType: item.questionType,
  159. });
  160. console.log("打印编辑对象", answerOption);
  161. if (item.multipleRule == '1') {
  162. ruleOneScore.value = item.multipleRuleDesc || 0;
  163. } else if (item.multipleRule == '2') {
  164. const parts = item.multipleRuleDesc?.split(",") || [];
  165. ruleTwoScore1.value = parts[0] || 0;
  166. ruleTwoScore2.value = parts[1] || 0;
  167. ruleTwoScore3.value = parts[2] || 0;
  168. } else {
  169. ruleThreeStr.value = item.multipleRuleDesc || "";
  170. }
  171. const answerStr = item.standardAnswer?.replace(/\s+/g, '') || '';
  172. mulAnswerList.value = answerStr.includes(',') ? answerStr.split(',') : answerStr.split('');
  173. dialogVisible.value = true;
  174. };
  175. // 少选分数验证
  176. const ScoreChange = (valueKey) => {
  177. let val = 0;
  178. if (valueKey === 'ruleOneScore') val = ruleOneScore.value;
  179. else if (valueKey === 'ruleTwoScore1') val = ruleTwoScore1.value;
  180. else if (valueKey === 'ruleTwoScore2') val = ruleTwoScore2.value;
  181. else if (valueKey === 'ruleTwoScore3') val = ruleTwoScore3.value;
  182. if (val > answerOption.fullScore) {
  183. if (valueKey === 'ruleOneScore') ruleOneScore.value = 0;
  184. else if (valueKey === 'ruleTwoScore1') ruleTwoScore1.value = 0;
  185. else if (valueKey === 'ruleTwoScore2') ruleTwoScore2.value = 0;
  186. else if (valueKey === 'ruleTwoScore3') ruleTwoScore3.value = 0;
  187. ElMessage.warning('分数不能大于满分!');
  188. }
  189. };
  190. // 多选题答案选择事件
  191. const MulAnswerChange = () => {
  192. console.log("打印多选题答案", mulAnswerList.value);
  193. answerOption.standardAnswer = mulAnswerList.value.toString(); // 默认的是英文逗号
  194. console.log("answerOption", answerOption);
  195. };
  196. // 题目类型切换事件
  197. const TypeChange = () => {
  198. // 单选题
  199. if (answerOption.questionType == 1) {
  200. answerOption.multipleRuleDesc = "";
  201. answerOption.multipleRule = "";
  202. answerOption.standardAnswer = "";
  203. }
  204. // 多选
  205. else if (answerOption.questionType == 2) {
  206. answerOption.multipleRuleDesc = "";
  207. answerOption.multipleRule = "1"; // 给分规则默认1
  208. answerOption.standardAnswer = "";
  209. mulAnswerList.value = [];
  210. }
  211. // 判断题
  212. else if (answerOption.questionType == 3) {
  213. answerOption.multipleRuleDesc = "";
  214. answerOption.multipleRule = "";
  215. answerOption.standardAnswer = "";
  216. }
  217. console.log("切换题目类型打印", answerOption);
  218. };
  219. // 确定弹窗
  220. const ConfirmEnter = () => {
  221. // 给分规则1
  222. if (answerOption.multipleRule == '1') {
  223. answerOption.isMul = true;
  224. answerOption.multipleRuleDesc = ruleOneScore.value || 0;
  225. }
  226. // 给分规则2
  227. else if (answerOption.multipleRule == '2') {
  228. answerOption.isMul = true;
  229. answerOption.multipleRuleDesc = `${ruleTwoScore1.value},${ruleTwoScore2.value},${ruleTwoScore3.value}`;
  230. }
  231. // 给分规则3
  232. else if (answerOption.multipleRule == '3') {
  233. // 这里需要做下处理 this.ruleThreeStr 里的逗号需要转义成英文的逗号
  234. answerOption.multipleRuleDesc = ruleThreeStr.value.replace(/,/g, ',');
  235. if (validateMultipleQuestionsName(answerOption.multipleRuleDesc)) {
  236. answerOption.isMul = true;
  237. } else {
  238. ElMessage.warning('请填写正确的格式!');
  239. return;
  240. }
  241. }
  242. emit('answerEnter', { ...answerOption });
  243. dialogVisible.value = false;
  244. };
  245. // 校验格式是否为 字母=内容,字母=内容 的格式
  246. const validateMultipleQuestionsName = (value) => {
  247. console.log("校验格式", value);
  248. // 如果为空,直接返回true(或根据业务需求决定)
  249. if (!value || value.trim() === '') {
  250. return true;
  251. }
  252. // 分割成各个部分
  253. const parts = value.split(',');
  254. // 检查每个部分是否符合 字母=内容 的格式
  255. for (let part of parts) {
  256. // 去掉首尾空格
  257. part = part.trim();
  258. // 检查是否符合 字母=内容 的格式(支持多个字母)
  259. if (!/^[A-Za-z]+=.+$/.test(part)) {
  260. return false;
  261. }
  262. }
  263. return true;
  264. };
  265. // 暴露方法供父组件调用
  266. defineExpose({
  267. InitData
  268. });
  269. </script>
  270. <style lang="scss" scoped>
  271. /* 保持原有样式 */
  272. </style>