Files
admin-sjzx/src/views/system/icd/form.vue

169 lines
4.8 KiB
Vue
Raw Normal View History

2025-04-16 13:55:57 +08:00
<template>
<el-dialog draggable width="400px" v-model="dialogVisible" :title="title">
2025-04-16 13:55:57 +08:00
<el-form :inline="false" :model="form" label-width="auto" class="form-one" :rules="rules" ref="formRef">
<el-form-item label="icd文件">
2025-04-16 13:55:57 +08:00
<el-upload
v-model:file-list="reportPath"
2025-04-16 13:55:57 +08:00
ref="uploadRef"
action=""
:limit="1"
2025-04-22 09:08:35 +08:00
accept=".xml"
2025-04-16 13:55:57 +08:00
:on-exceed="handleExceed"
:auto-upload="false"
:on-remove="removeFile"
2025-04-22 09:08:35 +08:00
:on-change="choose"
2025-04-16 13:55:57 +08:00
>
<template #trigger>
<el-button type="primary">上传文件</el-button>
</template>
2025-04-22 09:08:35 +08:00
<template #tip>
2026-01-28 08:45:38 +08:00
<div>请上传xml文件文件名不能包含空格</div>
2025-04-22 09:08:35 +08:00
</template>
2025-04-16 13:55:57 +08:00
</el-upload>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="submit">确认</el-button>
</span>
</template>
</el-dialog>
</template>
<script lang="ts" setup>
import { ref, inject, onMounted } from 'vue'
import { uploadFile } from '@/api/system-boot/file'
2025-04-16 13:55:57 +08:00
import { reactive } from 'vue'
import { UploadInstance, UploadProps, UploadRawFile, ElMessage, genFileId } from 'element-plus'
import { addIcdPath, updateIcdPath } from '@/api/device-boot/icd'
2025-04-16 13:55:57 +08:00
const emit = defineEmits(['submit'])
const dialogVisible = ref(false)
const title = ref('')
// 注意不要和表单ref的命名冲突
const form = reactive<anyObj>({
fileName: '',
filePath: '',
id: ''
2025-04-16 13:55:57 +08:00
})
const reportPath: any = ref([])
2025-04-16 13:55:57 +08:00
const formRef = ref()
const rules = {
path: [{ required: true, message: '请选择任务执行器', trigger: 'change' }],
fileName: [{ required: true, message: '请输入任务名称', trigger: 'blur' }]
2025-04-16 13:55:57 +08:00
}
const open = (text: string, data?: any) => {
reportPath.value = []
2025-04-16 13:55:57 +08:00
title.value = text
dialogVisible.value = true
if (data?.path) {
reportPath.value = [
{
name: data?.path.split('/icd/')[1]
}
]
2025-04-16 13:55:57 +08:00
}
form.fileName = data?.name
form.id = data?.id
form.filePath = data?.path
2025-04-16 13:55:57 +08:00
}
const uploadRef = ref()
2025-04-22 09:08:35 +08:00
/**
* 选择待上传文件
*/
const choose = (e: any) => {
const file = e.raw
if (!isValidFile(file)) {
uploadRef.value!.clearFiles()
form.filePath = []
return
}
form.filePath.file = file
}
/**
* 文件校验函数
*/
const isValidFile = (file: UploadRawFile) => {
const validExtensions = ['.xml']
const fileExtension = file.name.slice(((file.name.lastIndexOf('.') - 1) >>> 0) + 2)
if (!validExtensions.includes(`.${fileExtension}`)) {
ElMessage.error('文件类型不支持,请选择 .xml')
return false
}
2026-01-28 08:45:38 +08:00
// 检查文件名是否包含空格
if (file.name.includes(' ')) {
ElMessage.error('文件名不能包含空格,请重命名文件后再上传')
return false
}
2025-04-22 09:08:35 +08:00
return true
}
// 上传报告
2025-04-16 13:55:57 +08:00
const handleExceed: UploadProps['onExceed'] = files => {
2026-01-06 08:35:36 +08:00
2025-04-22 09:08:35 +08:00
2025-04-16 13:55:57 +08:00
uploadRef.value!.clearFiles()
const file = files[0] as UploadRawFile
file.uid = genFileId()
2026-01-28 08:45:38 +08:00
// 检查文件名是否包含空格
if (file.name.includes(' ')) {
ElMessage.error('文件名不能包含空格,请重命名文件后再上传')
return
}
2025-04-16 13:55:57 +08:00
uploadRef.value!.handleStart(file)
}
//移除文件上传
const removeFile = (file: any, uploadFiles: any) => {
form.filePath = ''
2025-04-16 13:55:57 +08:00
}
const submit = async () => {
2026-01-06 08:35:36 +08:00
if (reportPath.value.length == 0) {
return ElMessage.warning('请上传icd文件')
}
if (reportPath.value[0]?.raw != undefined) {
2026-01-28 08:45:38 +08:00
// 检查文件名是否包含空格
if (reportPath.value[0].raw.name.includes(' ')) {
ElMessage.error('文件名不能包含空格,请重命名文件后再上传')
return
}
await uploadFile(reportPath.value[0].raw, '/icd/').then(res => {
//治理工程验收报告
form.fileName = res.data.fileName.split('.')[0]
form.filePath = res.data.name
})
2025-04-22 09:08:35 +08:00
}
if (title.value == '新增icd文件') {
await addIcdPath(form).then(res => {
ElMessage.success('新增成功!')
dialogVisible.value = false
emit('submit')
})
} else {
await updateIcdPath(form).then(res => {
ElMessage.success('编辑成功!')
dialogVisible.value = false
emit('submit')
})
}
2025-04-16 13:55:57 +08:00
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
:deep(.el-upload-list__item) {
width: 300px;
}
</style>