Bladeren bron

feat(question): 添加Excel导入功能

实现题目Excel导入功能,包括前端上传组件和后端API接口
重构枚举类型处理函数,支持动态字典类型参数
移除未使用的defineExpose导入
tanshanming 7 maanden geleden
bovenliggende
commit
b1f9530034

+ 10 - 1
src/api/exam/question/tQuestionApi.js

@@ -6,5 +6,14 @@ export default {
 	pageList: (query) => request('api/admin/question/page', query, 'post'),
 	edit: (query) => request('api/admin/question/edit', query, 'post'),
 	select: (id) => request('api/admin/question/select/' + id, '', 'post'),
-	deleteQuestion: (id) => request('api/admin/question/delete/' + id, '', 'post')
+	deleteQuestion: (id) => request('api/admin/question/delete/' + id, '', 'post'),
+	importExcel: (file) => {
+		const formData = new FormData()
+		formData.append('file', file)
+		return request('api/admin/question/import', formData, 'post', {
+			headers: {
+				'Content-Type': 'multipart/form-data'
+			}
+		})
+	}
 }

+ 1 - 1
src/components/customPagination.vue

@@ -220,7 +220,7 @@
 		emit('change', currentPage.value)
 	}
 </script>
-<style scoped>
+<style scoped lang="less">
 	.pagination {
 		user-select: none;
 		display: flex;

+ 4 - 7
src/store/exam.js

@@ -1,8 +1,8 @@
 import { defineStore } from 'pinia'
 import subjectApi from '@/api/exam/paper/subject.js'
 import tool from '@/utils/tool'
-const funPaperType = function () {
-	return tool.dictList('PAPER_TYPE').map((item) => {
+const funPaperType = function (type) {
+	return tool.dictList(type).map((item) => {
 		return {
 			key: Number(item.value),
 			value: item.label
@@ -24,10 +24,7 @@ export const useExamStore = defineStore('exam', {
 		subjects: [],
 		// 枚举相关
 		user: {
-			bankTypeEnum: [
-				{ key: 1, value: '练习题目库' },
-				{ key: 2, value: '问卷题目库' }
-			],
+			bankTypeEnum: funPaperType('BANK_TYPE'),
 			sexEnum: [
 				{ key: 1, value: '男' },
 				{ key: 2, value: '女' }
@@ -76,7 +73,7 @@ export const useExamStore = defineStore('exam', {
 		},
 		exam: {
 			examPaper: {
-				paperTypeEnum: funPaperType()
+				paperTypeEnum: funPaperType('PAPER_TYPE')
 			},
 			examPaperAnswer: {
 				statusEnum: [

+ 23 - 1
src/views/exm/question/index.vue

@@ -53,6 +53,9 @@
 						</a-menu>
 					</template>
 				</a-dropdown>
+				<a-upload :customRequest="handleImportExcel" :showUploadList="false" accept=".xlsx,.xls" class="link-left">
+					<a-button type="primary">导入Excel</a-button>
+				</a-upload>
 			</a-form-item>
 		</a-form>
 		<a-table
@@ -155,7 +158,7 @@
 	import tQuestionApi from '@/api/exam/question/tQuestionApi'
 	import customPagination from '@/components/customPagination.vue'
 	import { StarFilled, StarOutlined } from '@ant-design/icons-vue'
-	import { Modal } from 'ant-design-vue'
+	import { Modal, message } from 'ant-design-vue'
 	import { parseTime } from '@/utils/exam'
 	const bankTypeEnum = computed(() => examStore.getBankTypeEnum)
 	const examStore = useExamStore()
@@ -287,6 +290,25 @@
 		queryParam.pageSize = size
 		search()
 	}
+
+	// 处理Excel导入
+	const handleImportExcel = (options) => {
+		const { file } = options
+		const loading = message.loading('正在导入中...', 0)
+
+		tQuestionApi
+			.importExcel(file)
+			.then((res) => {
+				loading()
+				message.success('导入成功')
+				search() // 刷新列表
+			})
+			.catch((error) => {
+				loading()
+				message.error('导入失败,请检查Excel文件格式是否正确')
+				console.error('导入失败:', error)
+			})
+	}
 </script>
 
 <style lang="less" scoped>

+ 1 - 1
src/views/myResource/file/box/uploadFile/Box.vue

@@ -75,7 +75,7 @@
 </template>
 
 <script setup>
-	import { ref, computed, defineExpose, nextTick, inject, getCurrentInstance, onMounted } from 'vue'
+	import { ref, computed, nextTick, inject, getCurrentInstance, onMounted } from 'vue'
 	import { message } from 'ant-design-vue'
 	import { useMyResourceStore } from '@/store/myResource'
 	import SparkMD5 from 'spark-md5'