ModifyOptionalQuestionRule.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <el-dialog
  3. v-model="visible"
  4. :title="title"
  5. width="800px"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. top="10vh"
  9. append-to-body
  10. @close="handleClose"
  11. @open="modalBeforeOpen"
  12. >
  13. <el-steps :active="currentStep" finish-status="success">
  14. <el-step title="设置规则" />
  15. <el-step title="选择试题" />
  16. </el-steps>
  17. <div style="margin-top: 20px">
  18. <!-- 第一步:设置规则 -->
  19. <div v-if="currentStep === 0">
  20. <OptionalRuleForm ref="ruleFormRef" v-model="ruleFormData" />
  21. </div>
  22. <!-- 第二步:选择试题 -->
  23. <div v-if="currentStep === 1">
  24. <OptionalQuestionSelect
  25. ref="questionSelectRef"
  26. v-model="selectedQuestions"
  27. :subject-id="props.rowData?.subjectId"
  28. />
  29. </div>
  30. </div>
  31. <template #footer>
  32. <div class="dialog-footer">
  33. <el-button @click="close">取消</el-button>
  34. <el-button v-if="currentStep > 0" @click="prevStep">上一步</el-button>
  35. <el-button v-if="currentStep < 1" type="primary" @click="nextStep">
  36. 下一步
  37. </el-button>
  38. <el-button
  39. v-if="currentStep === 1"
  40. type="primary"
  41. :loading="loading"
  42. @click="confirm"
  43. >
  44. 保存
  45. </el-button>
  46. </div>
  47. </template>
  48. </el-dialog>
  49. </template>
  50. <script setup lang="ts">
  51. import { ref, computed, reactive } from 'vue';
  52. import { ElMessage } from 'element-plus';
  53. import { saveOptionalQuestionRule } from '@/api/subject';
  54. import type {
  55. OptionalRuleItem,
  56. OptionalRuleUpdateParam,
  57. } from '@/api/types/subject';
  58. import useModal from '@/hooks/modal';
  59. import useLoading from '@/hooks/loading';
  60. import { objModifyAssign } from '@/utils/utils';
  61. import OptionalRuleForm from './OptionalRuleForm.vue';
  62. import OptionalQuestionSelect from './OptionalQuestionSelect.vue';
  63. defineOptions({
  64. name: 'ModifyOptionalQuestionRule',
  65. });
  66. /* modal */
  67. const { visible, open, close } = useModal();
  68. defineExpose({ open, close });
  69. interface Props {
  70. rowData: OptionalRuleItem;
  71. }
  72. const props = withDefaults(defineProps<Props>(), {
  73. rowData: () => {},
  74. });
  75. const emit = defineEmits(['modified']);
  76. const isEdit = computed(() => !!props.rowData?.id);
  77. const title = computed(() => `${isEdit.value ? '编辑' : '新增'}选做题规则`);
  78. const currentStep = ref(0);
  79. const ruleFormRef = ref();
  80. const questionSelectRef = ref();
  81. // 规则表单数据
  82. const initialRuleFormData = {
  83. ruleSelecCount: 1,
  84. ruleTotalCount: 1,
  85. scoreRule: 'MAX_SCORE' as const,
  86. };
  87. const ruleFormData = reactive({ ...initialRuleFormData });
  88. // 选中的试题
  89. const selectedQuestions = ref<number[]>([]);
  90. const handleClose = () => {
  91. currentStep.value = 0;
  92. objModifyAssign(ruleFormData, initialRuleFormData);
  93. selectedQuestions.value = [];
  94. };
  95. // 下一步
  96. const nextStep = async () => {
  97. if (currentStep.value === 0) {
  98. // 验证第一步表单
  99. const valid = await ruleFormRef.value?.validate().catch(() => false);
  100. if (!valid) return;
  101. }
  102. currentStep.value++;
  103. };
  104. // 上一步
  105. const prevStep = () => {
  106. currentStep.value--;
  107. };
  108. // 提交
  109. const { loading, setLoading } = useLoading();
  110. const confirm = async () => {
  111. // 验证第二步
  112. const valid = await questionSelectRef.value?.validate().catch(() => false);
  113. if (!valid) return;
  114. setLoading(true);
  115. const params: OptionalRuleUpdateParam = {
  116. ...ruleFormData,
  117. questionStructureIds: selectedQuestions.value,
  118. };
  119. if (props.rowData?.id) {
  120. params.id = props.rowData.id;
  121. }
  122. let res = true;
  123. await saveOptionalQuestionRule(params).catch(() => {
  124. res = false;
  125. });
  126. setLoading(false);
  127. if (!res) return;
  128. ElMessage.success(`${title.value}成功!`);
  129. emit('modified');
  130. close();
  131. };
  132. /* init modal */
  133. function modalBeforeOpen() {
  134. if (props.rowData?.id) {
  135. // 编辑模式
  136. objModifyAssign(ruleFormData, {
  137. ruleSelecCount: props.rowData.ruleSelecCount,
  138. ruleTotalCount: props.rowData.ruleTotalCount,
  139. scoreRule: props.rowData.scoreRule,
  140. });
  141. selectedQuestions.value = props.rowData.questionStructureIds || [];
  142. } else {
  143. // 新增模式
  144. objModifyAssign(ruleFormData, initialRuleFormData);
  145. selectedQuestions.value = [];
  146. }
  147. currentStep.value = 0;
  148. }
  149. </script>