Commit c1cc6b48d8a2fff9ec3c4e0e6602e17004644b39

Authored by Bujidao
1 parent 6646aafa

feat(codegen-vue3): add excel import flow to generated list template

yudao-module-infra/src/main/resources/codegen/vue3/api/api.ts.vm
@@ -98,6 +98,16 @@ export const ${simpleClassName}Api = { @@ -98,6 +98,16 @@ export const ${simpleClassName}Api = {
98 export${simpleClassName}: async (params) => { 98 export${simpleClassName}: async (params) => {
99 return await request.download({ url: `${baseURL}/export-excel`, params }) 99 return await request.download({ url: `${baseURL}/export-excel`, params })
100 }, 100 },
  101 +
  102 + // 下载${table.classComment}导入模板
  103 + import${simpleClassName}Template: async () => {
  104 + return await request.download({ url: `${baseURL}/get-import-template` })
  105 + },
  106 +
  107 + // 导入${table.classComment}
  108 + import${simpleClassName}: async (data: FormData) => {
  109 + return await request.upload({ url: `${baseURL}/import`, data })
  110 + },
101 ## 特殊:主子表专属逻辑 111 ## 特殊:主子表专属逻辑
102 #foreach ($subTable in $subTables) 112 #foreach ($subTable in $subTables)
103 #set ($index = $foreach.count - 1) 113 #set ($index = $foreach.count - 1)
yudao-module-infra/src/main/resources/codegen/vue3/views/index.vue.vm
@@ -93,6 +93,14 @@ @@ -93,6 +93,14 @@
93 <Icon icon="ep:plus" class="mr-5px" /> 新增 93 <Icon icon="ep:plus" class="mr-5px" /> 新增
94 </el-button> 94 </el-button>
95 <el-button 95 <el-button
  96 + type="warning"
  97 + plain
  98 + @click="handleImport"
  99 + v-hasPermi="['${permissionPrefix}:import']"
  100 + >
  101 + <Icon icon="ep:upload" class="mr-5px" /> 导入
  102 + </el-button>
  103 + <el-button
96 type="success" 104 type="success"
97 plain 105 plain
98 @click="handleExport" 106 @click="handleExport"
@@ -238,6 +246,43 @@ @@ -238,6 +246,43 @@
238 /> 246 />
239 </ContentWrap> 247 </ContentWrap>
240 248
  249 + <!-- 导入弹窗 -->
  250 + <Dialog v-model="importDialogVisible" title="${table.classComment}导入" width="400">
  251 + <el-upload
  252 + ref="uploadRef"
  253 + v-model:file-list="fileList"
  254 + :auto-upload="false"
  255 + :disabled="importFormLoading"
  256 + :limit="1"
  257 + :on-exceed="handleImportExceed"
  258 + accept=".xlsx, .xls"
  259 + action="none"
  260 + drag
  261 + >
  262 + <Icon icon="ep:upload" />
  263 + <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  264 + <template #tip>
  265 + <div class="el-upload__tip text-center">
  266 + <span>仅允许导入 xls、xlsx 格式文件。</span>
  267 + <el-link
  268 + :underline="false"
  269 + style="font-size: 12px; vertical-align: baseline"
  270 + type="primary"
  271 + @click="handleImportTemplate"
  272 + >
  273 + 下载模板
  274 + </el-link>
  275 + </div>
  276 + </template>
  277 + </el-upload>
  278 + <template #footer>
  279 + <el-button :disabled="importFormLoading" type="primary" @click="submitImportForm">
  280 + 确 定
  281 + </el-button>
  282 + <el-button @click="importDialogVisible = false">取 消</el-button>
  283 + </template>
  284 + </Dialog>
  285 +
241 <!-- 表单弹窗:添加/修改 --> 286 <!-- 表单弹窗:添加/修改 -->
242 <${simpleClassName}Form ref="formRef" @success="getList" /> 287 <${simpleClassName}Form ref="formRef" @success="getList" />
243 ## 特殊:主子表专属逻辑 288 ## 特殊:主子表专属逻辑
@@ -263,6 +308,7 @@ @@ -263,6 +308,7 @@
263 import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict' 308 import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
264 import { isEmpty } from '@/utils/is' 309 import { isEmpty } from '@/utils/is'
265 import { dateFormatter } from '@/utils/formatTime' 310 import { dateFormatter } from '@/utils/formatTime'
  311 +import type { UploadUserFile } from 'element-plus'
266 ## 特殊:树表专属逻辑 312 ## 特殊:树表专属逻辑
267 #if ( $table.templateType == 2 ) 313 #if ( $table.templateType == 2 )
268 import { handleTree } from '@/utils/tree' 314 import { handleTree } from '@/utils/tree'
@@ -308,6 +354,10 @@ const queryParams = reactive({ @@ -308,6 +354,10 @@ const queryParams = reactive({
308 }) 354 })
309 const queryFormRef = ref() // 搜索的表单 355 const queryFormRef = ref() // 搜索的表单
310 const exportLoading = ref(false) // 导出的加载中 356 const exportLoading = ref(false) // 导出的加载中
  357 +const importDialogVisible = ref(false) // 导入弹窗
  358 +const importFormLoading = ref(false) // 导入的加载中
  359 +const uploadRef = ref()
  360 +const fileList = ref<UploadUserFile[]>([])
311 361
312 /** 查询列表 */ 362 /** 查询列表 */
313 const getList = async () => { 363 const getList = async () => {
@@ -345,6 +395,58 @@ const openForm = (type: string, id?: number) =&gt; { @@ -345,6 +395,58 @@ const openForm = (type: string, id?: number) =&gt; {
345 formRef.value.open(type, id) 395 formRef.value.open(type, id)
346 } 396 }
347 397
  398 +/** 导入按钮操作 */
  399 +const handleImport = async () => {
  400 + importDialogVisible.value = true
  401 + await resetImportForm()
  402 +}
  403 +
  404 +/** 提交导入 */
  405 +const submitImportForm = async () => {
  406 + if (fileList.value.length === 0) {
  407 + message.error('请上传文件')
  408 + return
  409 + }
  410 + importFormLoading.value = true
  411 + try {
  412 + const formData = new FormData()
  413 + formData.append('file', fileList.value[0].raw as Blob)
  414 + const res = await ${simpleClassName}Api.import${simpleClassName}(formData)
  415 + const data = res.data
  416 + let text = '导入成功数量:' + data.successCount + ';导入失败数量:' + data.failureCount + ';'
  417 + if (data.failureCount > 0) {
  418 + for (const rowNo in data.failureRows) {
  419 + text += '< 第' + rowNo + '行: ' + data.failureRows[rowNo] + ' >'
  420 + }
  421 + }
  422 + message.alert(text)
  423 + importDialogVisible.value = false
  424 + await getList()
  425 + } catch {
  426 + } finally {
  427 + importFormLoading.value = false
  428 + await resetImportForm()
  429 + }
  430 +}
  431 +
  432 +/** 下载导入模板 */
  433 +const handleImportTemplate = async () => {
  434 + const data = await ${simpleClassName}Api.import${simpleClassName}Template()
  435 + download.excel(data, '${table.classComment}导入模板.xls')
  436 +}
  437 +
  438 +/** 导入文件超限 */
  439 +const handleImportExceed = (): void => {
  440 + message.error('最多只能上传一个文件!')
  441 +}
  442 +
  443 +/** 重置导入表单 */
  444 +const resetImportForm = async () => {
  445 + fileList.value = []
  446 + await nextTick()
  447 + uploadRef.value?.clearFiles()
  448 +}
  449 +
348 /** 删除按钮操作 */ 450 /** 删除按钮操作 */
349 const handleDelete = async (id: number) => { 451 const handleDelete = async (id: number) => {
350 try { 452 try {
@@ -421,4 +523,4 @@ const toggleExpandAll = async () =&gt; { @@ -421,4 +523,4 @@ const toggleExpandAll = async () =&gt; {
421 onMounted(() => { 523 onMounted(() => {
422 getList() 524 getList()
423 }) 525 })
424 -</script>  
425 \ No newline at end of file 526 \ No newline at end of file
  527 +</script>