form.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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="请选择试卷类型" @change="handPaperTypeChange">
  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: null,
  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. re.paperType = re.paperType.toString()
  191. Object.assign(form, re)
  192. formLoading.value = false
  193. }
  194. })
  195. function submitForm() {
  196. formRef.value
  197. .validate()
  198. .then(async () => {
  199. formLoading.value = true
  200. const re = await examPaperApi.edit(form)
  201. if (re) {
  202. emit('success')
  203. } else {
  204. message.error(re.message)
  205. formLoading.value = false
  206. }
  207. })
  208. .catch(() => {})
  209. }
  210. function addTitle() {
  211. form.titleItems.push({ name: '', questionItems: [] })
  212. }
  213. function addQuestion(titleItem) {
  214. currentTitleItem.value = titleItem
  215. questionPage.showDialog = true
  216. search()
  217. }
  218. function removeTitleItem(index) {
  219. form.titleItems.splice(index, 1)
  220. }
  221. function removeQuestion(titleItem, questionIndex) {
  222. titleItem.questionItems.splice(questionIndex, 1)
  223. }
  224. function queryForm() {
  225. questionPage.queryParam.pageIndex = 1
  226. search()
  227. }
  228. function confirmQuestionSelect() {
  229. Promise.all(questionPage.multipleSelection.map((q) => questionApi.select(q.id))).then((resArr) => {
  230. resArr.forEach((re) => {
  231. currentTitleItem.value.questionItems.push(re)
  232. })
  233. questionPage.showDialog = false
  234. })
  235. }
  236. function levelChange() {
  237. form.subjectId = null
  238. subjectFilter.value = subjects.value.filter((data) => data.level === form.level)
  239. }
  240. function search() {
  241. questionPage.queryParam.subjectId = form.subjectId
  242. questionPage.listLoading = true
  243. const params = {
  244. ...questionPage.queryParam,
  245. size: questionPage.queryParam.pageSize,
  246. current: questionPage.queryParam.pageIndex
  247. }
  248. questionApi.pageList(params).then((data) => {
  249. const re = data
  250. questionPage.tableData = re.records
  251. questionPage.total = re.total
  252. questionPage.queryParam.pageIndex = re.current
  253. questionPage.listLoading = false
  254. })
  255. }
  256. function onPageChange(page) {
  257. questionPage.queryParam.pageIndex = page
  258. search()
  259. }
  260. // 获取所有已选题目的ID
  261. const allSelectedQuestionIds = computed(() => {
  262. const ids = []
  263. form.titleItems.forEach((titleItem) => {
  264. titleItem.questionItems.forEach((question) => {
  265. ids.push(question.id)
  266. })
  267. })
  268. return ids
  269. })
  270. const rowSelection = {
  271. selectedRowKeys: computed(() => questionPage.multipleSelection.map((q) => q.id)),
  272. onChange: (selectedRowKeys, selectedRows) => {
  273. questionPage.multipleSelection = selectedRows
  274. },
  275. getCheckboxProps: (record) => ({
  276. disabled: allSelectedQuestionIds.value.includes(record.id),
  277. title: allSelectedQuestionIds.value.includes(record.id) ? '该题目已被选择' : ''
  278. })
  279. }
  280. const handPaperTypeChange = (paperType) => {
  281. if (['2', '3'].includes(paperType)) {
  282. questionPage.queryParam.bankType = '1'
  283. }
  284. if ('5' === paperType) {
  285. questionPage.queryParam.bankType = '2'
  286. }
  287. if ('6' === paperType) {
  288. questionPage.queryParam.bankType = '3'
  289. }
  290. }
  291. function questionTypeFormatter({ text }) {
  292. return examStore.enumFormat(questionTypeEnum.value, text)
  293. }
  294. function resetForm() {
  295. const lastId = form.id
  296. Object.assign(form, {
  297. id: null,
  298. // level: null,
  299. // subjectId: null,
  300. paperType: null,
  301. limitDateTime: [],
  302. name: '',
  303. suggestTime: null,
  304. titleItems: []
  305. })
  306. form.id = lastId
  307. }
  308. </script>
  309. <style lang="less" scoped>
  310. .app-container {
  311. padding: 24px;
  312. }
  313. .exampaper-item-box {
  314. margin-top: 12px;
  315. padding-right: 20px;
  316. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  317. .q-title {
  318. margin: 0 5px;
  319. }
  320. }
  321. </style>