form.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <div class="app-container">
  3. <a-form
  4. :model="form"
  5. ref="formRef"
  6. :label-col="{ span: 4 }"
  7. :wrapper-col="{ span: 16 }"
  8. :rules="rules"
  9. :loading="formLoading"
  10. >
  11. <!-- <a-form-item label="年级" name="level" :rules="rules.level">
  12. <a-select v-model:value="form.level" placeholder="请选择年级" @change="levelChange">
  13. <a-select-option v-for="item in levelEnum" :key="item.key" :value="item.key">
  14. {{ item.value }}
  15. </a-select-option>
  16. </a-select>
  17. </a-form-item>
  18. <a-form-item label="学科" name="subjectId" :rules="rules.subjectId">
  19. <a-select v-model:value="form.subjectId" placeholder="请选择学科">
  20. <a-select-option v-for="item in subjectFilter" :key="item.id" :value="item.id">
  21. {{ item.name + ' ( ' + item.levelName + ' )' }}
  22. </a-select-option>
  23. </a-select>
  24. </a-form-item> -->
  25. <a-form-item label="试卷类型" name="paperType" :rules="rules.paperType">
  26. <a-select v-model:value="form.paperType" placeholder="请选择试卷类型">
  27. <a-select-option v-for="item in paperTypeEnum" :key="item.key" :value="item.key">
  28. {{ item.value }}
  29. </a-select-option>
  30. </a-select>
  31. </a-form-item>
  32. <a-form-item label="时间限制" v-if="form.paperType === 4">
  33. <a-range-picker
  34. v-model:value="form.limitDateTime"
  35. show-time
  36. format="YYYY-MM-DD HH:mm:ss"
  37. :placeholder="['开始日期', '结束日期']"
  38. />
  39. </a-form-item>
  40. <a-form-item label="试卷名称" name="name" :rules="rules.name">
  41. <a-input v-model:value="form.name" placeholder="请输入试卷名称" />
  42. </a-form-item>
  43. <template v-for="(titleItem, index) in form.titleItems" :key="index">
  44. <a-form-item :label="'标题' + (index + 1)" required>
  45. <a-input v-model:value="titleItem.name" style="width: 80%" />
  46. <a-button type="link" @click="addQuestion(titleItem)" style="margin-left: 20px">添加题目</a-button>
  47. <a-button type="link" danger @click="removeTitleItem(index)">删除</a-button>
  48. <a-card v-if="titleItem.questionItems.length !== 0" class="exampaper-item-box">
  49. <template v-for="(questionItem, questionIndex) in titleItem.questionItems" :key="questionIndex">
  50. <a-form-item :label="'题目' + (questionIndex + 1)" style="margin-bottom: 15px">
  51. <a-row>
  52. <a-col :span="23">
  53. <QuestionShow :qType="questionItem.questionType" :question="questionItem" />
  54. </a-col>
  55. <a-col :span="1">
  56. <a-button type="link" danger @click="removeQuestion(titleItem, questionIndex)">删除</a-button>
  57. </a-col>
  58. </a-row>
  59. </a-form-item>
  60. </template>
  61. </a-card>
  62. </a-form-item>
  63. </template>
  64. <a-form-item label="建议时长" name="suggestTime" :rules="rules.suggestTime">
  65. <a-input v-model:value="form.suggestTime" placeholder="分钟" />
  66. </a-form-item>
  67. <a-form-item>
  68. <a-space>
  69. <a-button type="primary" @click="submitForm">提交</a-button>
  70. <a-button @click="resetForm">重置</a-button>
  71. <a-button type="dashed" @click="addTitle">添加标题</a-button>
  72. </a-space>
  73. </a-form-item>
  74. </a-form>
  75. <a-modal
  76. v-model:visible="questionPage.showDialog"
  77. title="选择题目"
  78. width="70%"
  79. @ok="confirmQuestionSelect"
  80. @cancel="() => (questionPage.showDialog = false)"
  81. >
  82. <a-form layout="inline">
  83. <a-form-item label="ID">
  84. <a-input v-model:value="questionPage.queryParam.id" allow-clear />
  85. </a-form-item>
  86. <a-form-item label="题型">
  87. <a-select v-model:value="questionPage.queryParam.questionType" allow-clear style="width: 120px">
  88. <a-select-option v-for="item in questionTypeEnum" :key="item.key" :value="item.key">
  89. {{ item.value }}
  90. </a-select-option>
  91. </a-select>
  92. </a-form-item>
  93. <a-form-item label="题库类型">
  94. <a-select v-model:value="questionPage.queryParam.bankType" allow-clear style="width: 120px">
  95. <a-select-option v-for="item in bankTypeEnum" :key="item.key" :value="item.key">
  96. {{ item.value }}
  97. </a-select-option>
  98. </a-select>
  99. </a-form-item>
  100. <a-form-item>
  101. <a-button type="primary" @click="queryForm">查询</a-button>
  102. </a-form-item>
  103. </a-form>
  104. <a-table
  105. :dataSource="questionPage.tableData"
  106. :loading="questionPage.listLoading"
  107. rowKey="id"
  108. :rowSelection="rowSelection"
  109. :pagination="false"
  110. style="margin-top: 16px"
  111. >
  112. <a-table-column type="selection" width="35" />
  113. <a-table-column title="Id" dataIndex="id" width="60" />
  114. <a-table-column title="题型" dataIndex="questionType" :customRender="questionTypeFormatter" width="70" />
  115. <a-table-column title="题干" dataIndex="shortTitle" ellipsis />
  116. </a-table>
  117. <a-pagination
  118. v-if="questionPage.total > 0"
  119. :total="questionPage.total"
  120. :current="questionPage.queryParam.pageIndex"
  121. :pageSize="questionPage.queryParam.pageSize"
  122. @change="onPageChange"
  123. style="margin-top: 16px; text-align: right"
  124. />
  125. </a-modal>
  126. </div>
  127. </template>
  128. <script setup>
  129. import { ref, reactive, computed, onMounted } from 'vue'
  130. import { useExamStore } from '@/store/exam'
  131. import examPaperApi from '@/api/exam/paper/examPaperApi'
  132. import questionApi from '@/api/exam/question/tQuestionApi'
  133. import QuestionShow from '@/views/exm/question/components/Show.vue'
  134. import { message } from 'ant-design-vue'
  135. const bankTypeEnum = computed(() => examStore.getBankTypeEnum)
  136. const props = defineProps({
  137. id: {
  138. type: Number,
  139. default: 0
  140. }
  141. })
  142. const emit = defineEmits(['success'])
  143. const formRef = ref()
  144. const examStore = useExamStore()
  145. const form = reactive({
  146. id: null,
  147. // level: null,
  148. // subjectId: null,
  149. paperType: 1,
  150. limitDateTime: [],
  151. name: '',
  152. suggestTime: null,
  153. titleItems: []
  154. })
  155. const subjectFilter = ref([])
  156. const formLoading = ref(false)
  157. const rules = {
  158. level: [{ required: true, message: '请选择年级', trigger: 'change' }],
  159. subjectId: [{ required: true, message: '请选择学科', trigger: 'change' }],
  160. paperType: [{ required: true, message: '请选择试卷类型', trigger: 'change' }],
  161. name: [{ required: true, message: '请输入试卷名称', trigger: 'blur' }],
  162. suggestTime: [{ required: true, message: '请输入建议时长', trigger: 'blur' }]
  163. }
  164. const questionPage = reactive({
  165. multipleSelection: [],
  166. showDialog: false,
  167. queryParam: {
  168. id: null,
  169. questionType: null,
  170. subjectId: 1,
  171. pageIndex: 1,
  172. pageSize: 5
  173. },
  174. listLoading: false,
  175. tableData: [],
  176. total: 0
  177. })
  178. const currentTitleItem = ref(null)
  179. const levelEnum = computed(() => examStore.levelEnum)
  180. const paperTypeEnum = computed(() => examStore.paperTypeEnum)
  181. const questionTypeEnum = computed(() => examStore.questionTypeEnum)
  182. const subjects = computed(() => examStore.subjects)
  183. onMounted(async () => {
  184. await examStore.initSubject()
  185. subjectFilter.value = subjects.value
  186. const id = props.id
  187. if (id && parseInt(id) !== 0) {
  188. formLoading.value = true
  189. const re = await examPaperApi.select(id)
  190. Object.assign(form, re)
  191. formLoading.value = false
  192. }
  193. })
  194. function submitForm() {
  195. formRef.value
  196. .validate()
  197. .then(async () => {
  198. formLoading.value = true
  199. const re = await examPaperApi.edit(form)
  200. if (re) {
  201. emit('success')
  202. } else {
  203. message.error(re.message)
  204. formLoading.value = false
  205. }
  206. })
  207. .catch(() => {})
  208. }
  209. function addTitle() {
  210. form.titleItems.push({ name: '', questionItems: [] })
  211. }
  212. function addQuestion(titleItem) {
  213. currentTitleItem.value = titleItem
  214. questionPage.showDialog = true
  215. search()
  216. }
  217. function removeTitleItem(index) {
  218. form.titleItems.splice(index, 1)
  219. }
  220. function removeQuestion(titleItem, questionIndex) {
  221. titleItem.questionItems.splice(questionIndex, 1)
  222. }
  223. function queryForm() {
  224. questionPage.queryParam.pageIndex = 1
  225. search()
  226. }
  227. function confirmQuestionSelect() {
  228. Promise.all(questionPage.multipleSelection.map((q) => questionApi.select(q.id))).then((resArr) => {
  229. resArr.forEach((re) => {
  230. currentTitleItem.value.questionItems.push(re)
  231. })
  232. questionPage.showDialog = false
  233. })
  234. }
  235. function levelChange() {
  236. form.subjectId = null
  237. subjectFilter.value = subjects.value.filter((data) => data.level === form.level)
  238. }
  239. function search() {
  240. questionPage.queryParam.subjectId = form.subjectId
  241. questionPage.listLoading = true
  242. const params = {
  243. ...questionPage.queryParam,
  244. size: questionPage.queryParam.pageSize,
  245. current: questionPage.queryParam.pageIndex
  246. }
  247. questionApi.pageList(params).then((data) => {
  248. const re = data
  249. questionPage.tableData = re.records
  250. questionPage.total = re.total
  251. questionPage.queryParam.pageIndex = re.current
  252. questionPage.listLoading = false
  253. })
  254. }
  255. function onPageChange(page) {
  256. questionPage.queryParam.pageIndex = page
  257. search()
  258. }
  259. const rowSelection = {
  260. selectedRowKeys: computed(() => questionPage.multipleSelection.map((q) => q.id)),
  261. onChange: (selectedRowKeys, selectedRows) => {
  262. questionPage.multipleSelection = selectedRows
  263. }
  264. }
  265. function questionTypeFormatter({ text }) {
  266. return examStore.enumFormat(questionTypeEnum.value, text)
  267. }
  268. function resetForm() {
  269. const lastId = form.id
  270. Object.assign(form, {
  271. id: null,
  272. // level: null,
  273. // subjectId: null,
  274. paperType: 1,
  275. limitDateTime: [],
  276. name: '',
  277. suggestTime: null,
  278. titleItems: []
  279. })
  280. form.id = lastId
  281. }
  282. </script>
  283. <style lang="less" scoped>
  284. .app-container {
  285. padding: 24px;
  286. }
  287. .exampaper-item-box {
  288. margin-top: 12px;
  289. padding-right: 20px;
  290. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  291. .q-title {
  292. margin: 0 5px;
  293. }
  294. }
  295. </style>