2024-08-26 20:05:04 +08:00
|
|
|
|
<template>
|
2025-01-16 19:45:48 +08:00
|
|
|
|
<el-dialog v-model='dialogVisible' :title='`批量添加${parameter.title}`' :destroy-on-close='true' width='580px'
|
|
|
|
|
|
draggable>
|
|
|
|
|
|
<el-form class='drawer-multiColumn-form' label-width='100px'>
|
|
|
|
|
|
<el-form-item label='模板下载 :'>
|
|
|
|
|
|
<el-button type='primary' :icon='Download' @click='downloadTemp'> 点击下载</el-button>
|
2024-08-26 20:05:04 +08:00
|
|
|
|
</el-form-item>
|
2025-01-16 19:45:48 +08:00
|
|
|
|
<el-form-item label='文件上传 :'>
|
2024-08-26 20:05:04 +08:00
|
|
|
|
<el-upload
|
2025-01-16 19:45:48 +08:00
|
|
|
|
action='#'
|
|
|
|
|
|
class='upload'
|
|
|
|
|
|
:drag='true'
|
|
|
|
|
|
:limit='excelLimit'
|
|
|
|
|
|
:multiple='true'
|
|
|
|
|
|
:show-file-list='true'
|
|
|
|
|
|
:http-request='uploadExcel'
|
|
|
|
|
|
:before-upload='beforeExcelUpload'
|
|
|
|
|
|
:on-exceed='handleExceed'
|
2024-08-26 20:05:04 +08:00
|
|
|
|
:accept="parameter.fileType!.join(',')"
|
|
|
|
|
|
>
|
2025-01-16 19:45:48 +08:00
|
|
|
|
<slot name='empty'>
|
|
|
|
|
|
<el-icon class='el-icon--upload'>
|
2024-08-26 20:05:04 +08:00
|
|
|
|
<upload-filled />
|
|
|
|
|
|
</el-icon>
|
2025-01-16 19:45:48 +08:00
|
|
|
|
<div class='el-upload__text'>将文件拖到此处,或<em>点击上传</em></div>
|
2024-08-26 20:05:04 +08:00
|
|
|
|
</slot>
|
|
|
|
|
|
<template #tip>
|
2025-01-16 19:45:48 +08:00
|
|
|
|
<slot name='tip'>
|
|
|
|
|
|
<div class='el-upload__tip'>请上传 .xls , .xlsx 标准格式文件,文件最大为 {{ parameter.fileSize }}M</div>
|
2024-08-26 20:05:04 +08:00
|
|
|
|
</slot>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</el-upload>
|
|
|
|
|
|
</el-form-item>
|
2025-01-16 19:45:48 +08:00
|
|
|
|
<el-form-item v-if='parameter.showCover' label='数据覆盖 :'>
|
|
|
|
|
|
<el-switch v-model='isCover' />
|
2024-08-26 20:05:04 +08:00
|
|
|
|
</el-form-item>
|
|
|
|
|
|
</el-form>
|
|
|
|
|
|
</el-dialog>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
2025-01-16 19:45:48 +08:00
|
|
|
|
<script setup lang='ts' name='ImportExcel'>
|
|
|
|
|
|
import { ref } from 'vue'
|
|
|
|
|
|
import { useDownload } from '@/hooks/useDownload'
|
|
|
|
|
|
import { Download } from '@element-plus/icons-vue'
|
|
|
|
|
|
import { ElNotification, UploadRequestOptions, UploadRawFile, ElMessage } from 'element-plus'
|
2024-08-26 20:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
export interface ExcelParameterProps {
|
|
|
|
|
|
title: string; // 标题
|
2024-11-07 19:24:29 +08:00
|
|
|
|
showCover?: boolean; // 是否显示”数据覆盖“选项
|
2024-12-17 11:12:21 +08:00
|
|
|
|
patternId?: string; // 模式ID
|
2025-07-21 13:47:56 +08:00
|
|
|
|
planId?: string | null ;//计划ID
|
2024-08-26 20:05:04 +08:00
|
|
|
|
fileSize?: number; // 上传文件的大小
|
|
|
|
|
|
fileType?: File.ExcelMimeType[]; // 上传文件的类型
|
|
|
|
|
|
tempApi?: (params: any) => Promise<any>; // 下载模板的Api
|
|
|
|
|
|
importApi?: (params: any) => Promise<any>; // 批量导入的Api
|
|
|
|
|
|
getTableList?: () => void; // 获取表格数据的Api
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 是否覆盖数据
|
2025-01-16 19:45:48 +08:00
|
|
|
|
const isCover = ref(false)
|
2024-08-26 20:05:04 +08:00
|
|
|
|
// 最大文件上传数
|
2025-01-16 19:45:48 +08:00
|
|
|
|
const excelLimit = ref(1)
|
2024-08-26 20:05:04 +08:00
|
|
|
|
// dialog状态
|
2025-01-16 19:45:48 +08:00
|
|
|
|
const dialogVisible = ref(false)
|
2024-08-26 20:05:04 +08:00
|
|
|
|
// 父组件传过来的参数
|
|
|
|
|
|
const parameter = ref<ExcelParameterProps>({
|
2025-01-16 19:45:48 +08:00
|
|
|
|
title: '',
|
2024-08-26 20:05:04 +08:00
|
|
|
|
fileSize: 5,
|
2025-01-16 19:45:48 +08:00
|
|
|
|
fileType: ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
|
|
|
|
|
|
})
|
2024-08-26 20:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 接收父组件参数
|
|
|
|
|
|
const acceptParams = (params: ExcelParameterProps) => {
|
2025-01-16 19:45:48 +08:00
|
|
|
|
parameter.value = { ...parameter.value, ...params }
|
|
|
|
|
|
dialogVisible.value = true
|
|
|
|
|
|
}
|
2024-08-26 20:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
// Excel 导入模板下载
|
|
|
|
|
|
const downloadTemp = () => {
|
2025-01-16 19:45:48 +08:00
|
|
|
|
if (!parameter.value.tempApi) return
|
2025-07-09 20:12:40 +08:00
|
|
|
|
useDownload(parameter.value.tempApi, `${parameter.value.title}模板`, {'pattern':parameter.value.patternId}, false)
|
2025-01-16 19:45:48 +08:00
|
|
|
|
}
|
2024-08-26 20:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 文件上传
|
|
|
|
|
|
const uploadExcel = async (param: UploadRequestOptions) => {
|
2025-01-16 19:45:48 +08:00
|
|
|
|
let excelFormData = new FormData()
|
|
|
|
|
|
excelFormData.append('file', param.file)
|
|
|
|
|
|
if (parameter.value.patternId) {
|
|
|
|
|
|
excelFormData.append('patternId', parameter.value.patternId)
|
2024-12-17 11:12:21 +08:00
|
|
|
|
}
|
2025-07-21 13:47:56 +08:00
|
|
|
|
|
|
|
|
|
|
excelFormData.append('planId', parameter.value.planId)
|
|
|
|
|
|
|
2025-01-16 19:45:48 +08:00
|
|
|
|
isCover.value && excelFormData.append('isCover', isCover.value as unknown as Blob)
|
2025-01-16 15:43:58 +08:00
|
|
|
|
//await parameter.value.importApi!(excelFormData);
|
|
|
|
|
|
await parameter.value.importApi!(excelFormData)
|
2025-01-16 19:45:48 +08:00
|
|
|
|
.then(res => handleImportResponse(res))
|
|
|
|
|
|
parameter.value.getTableList && parameter.value.getTableList()
|
|
|
|
|
|
dialogVisible.value = false
|
|
|
|
|
|
}
|
2025-01-16 15:43:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-01-16 19:45:48 +08:00
|
|
|
|
async function handleImportResponse(res: any) {
|
2025-02-18 15:58:38 +08:00
|
|
|
|
console.log(res)
|
|
|
|
|
|
|
2025-01-16 19:45:48 +08:00
|
|
|
|
if (res.type === 'application/json') {
|
|
|
|
|
|
const fileReader = new FileReader()
|
|
|
|
|
|
fileReader.onloadend = () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const jsonData = JSON.parse(fileReader.result)
|
|
|
|
|
|
if (jsonData.code === 'A0000') {
|
|
|
|
|
|
ElMessage.success('导入成功')
|
|
|
|
|
|
} else {
|
|
|
|
|
|
ElMessage.error(jsonData.message)
|
2025-01-16 15:43:58 +08:00
|
|
|
|
}
|
2025-01-16 19:45:48 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log(err)
|
|
|
|
|
|
}
|
2025-01-16 15:43:58 +08:00
|
|
|
|
}
|
2025-01-16 19:45:48 +08:00
|
|
|
|
fileReader.readAsText(res)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
ElMessage.error('导入失败,请查看下载附件!')
|
|
|
|
|
|
let blob = new Blob([res], { type: 'application/vnd.ms-excel' })
|
|
|
|
|
|
const url = window.URL.createObjectURL(blob)
|
|
|
|
|
|
const link = document.createElement('a')
|
|
|
|
|
|
link.href = url
|
2025-02-18 15:58:38 +08:00
|
|
|
|
link.download = '导入失败数据'
|
2025-01-16 19:45:48 +08:00
|
|
|
|
document.body.appendChild(link)
|
|
|
|
|
|
link.click()
|
|
|
|
|
|
link.remove()
|
|
|
|
|
|
}
|
2025-01-16 15:43:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-26 20:05:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @description 文件上传之前判断
|
|
|
|
|
|
* @param file 上传的文件
|
|
|
|
|
|
* */
|
|
|
|
|
|
const beforeExcelUpload = (file: UploadRawFile) => {
|
2025-01-16 19:45:48 +08:00
|
|
|
|
const isExcel = parameter.value.fileType!.includes(file.type as File.ExcelMimeType)
|
|
|
|
|
|
const fileSize = file.size / 1024 / 1024 < parameter.value.fileSize!
|
2024-08-26 20:05:04 +08:00
|
|
|
|
if (!isExcel)
|
|
|
|
|
|
ElNotification({
|
2025-01-16 19:45:48 +08:00
|
|
|
|
title: '温馨提示',
|
|
|
|
|
|
message: '上传文件只能是 xls / xlsx 格式!',
|
|
|
|
|
|
type: 'warning',
|
|
|
|
|
|
})
|
2024-08-26 20:05:04 +08:00
|
|
|
|
if (!fileSize)
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
ElNotification({
|
2025-01-16 19:45:48 +08:00
|
|
|
|
title: '温馨提示',
|
2024-08-26 20:05:04 +08:00
|
|
|
|
message: `上传文件大小不能超过 ${parameter.value.fileSize}MB!`,
|
2025-01-16 19:45:48 +08:00
|
|
|
|
type: 'warning',
|
|
|
|
|
|
})
|
|
|
|
|
|
}, 0)
|
|
|
|
|
|
return isExcel && fileSize
|
|
|
|
|
|
}
|
2024-08-26 20:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 文件数超出提示
|
|
|
|
|
|
const handleExceed = () => {
|
|
|
|
|
|
ElNotification({
|
2025-01-16 19:45:48 +08:00
|
|
|
|
title: '温馨提示',
|
|
|
|
|
|
message: '最多只能上传一个文件!',
|
|
|
|
|
|
type: 'warning',
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-08-26 20:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 上传错误提示
|
2025-01-16 19:45:48 +08:00
|
|
|
|
// const excelUploadError = () => {
|
|
|
|
|
|
// ElNotification({
|
|
|
|
|
|
// title: '温馨提示',
|
|
|
|
|
|
// message: `批量添加${parameter.value.title}失败,请您重新上传!`,
|
|
|
|
|
|
// type: 'error',
|
|
|
|
|
|
// })
|
|
|
|
|
|
// }
|
2024-08-26 20:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 上传成功提示
|
2025-01-16 19:45:48 +08:00
|
|
|
|
// const excelUploadSuccess = () => {
|
|
|
|
|
|
// ElNotification({
|
|
|
|
|
|
// title: '温馨提示',
|
|
|
|
|
|
// message: `批量添加${parameter.value.title}成功!`,
|
|
|
|
|
|
// type: 'success',
|
|
|
|
|
|
// })
|
|
|
|
|
|
// }
|
2024-08-26 20:05:04 +08:00
|
|
|
|
|
|
|
|
|
|
defineExpose({
|
2025-01-16 19:45:48 +08:00
|
|
|
|
acceptParams,
|
|
|
|
|
|
})
|
2024-08-26 20:05:04 +08:00
|
|
|
|
</script>
|
2025-01-16 19:45:48 +08:00
|
|
|
|
<style lang='scss' scoped>
|
2024-08-26 20:05:04 +08:00
|
|
|
|
@import "./index.scss";
|
|
|
|
|
|
</style>
|