2024-09-06 15:10:30 +08:00
|
|
|
<template>
|
2024-09-13 18:31:35 +08:00
|
|
|
<el-dialog draggable class="cn-operate-dialog" v-model="dialogVisible" :title="title" width="700px">
|
2024-09-06 15:10:30 +08:00
|
|
|
<el-scrollbar>
|
2024-09-13 18:31:35 +08:00
|
|
|
<el-form :inline="false" :model="form" label-width="auto" :rules="rules" ref="formRef">
|
|
|
|
|
<el-form-item label="模版名称" prop="name">
|
|
|
|
|
<el-input v-model="form.name" placeholder="请输入模版名称" />
|
2024-09-06 15:10:30 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
|
2024-09-13 18:31:35 +08:00
|
|
|
<el-form-item label="模版">
|
2024-09-24 16:23:55 +08:00
|
|
|
<el-upload v-model:file-list="urlList" ref="upload" action="" :limit="1" :auto-upload="false"
|
|
|
|
|
:on-exceed="handleExceed">
|
2024-09-13 18:31:35 +08:00
|
|
|
<el-button type="primary">上传</el-button>
|
2024-09-06 15:10:30 +08:00
|
|
|
</el-upload>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</el-form>
|
|
|
|
|
</el-scrollbar>
|
|
|
|
|
<template #footer>
|
|
|
|
|
<span class="dialog-footer">
|
|
|
|
|
<el-button @click="dialogVisible = false">取消</el-button>
|
2024-10-17 13:29:51 +08:00
|
|
|
<el-button type="primary" @click="submit" :loading="loading">确认</el-button>
|
2024-09-06 15:10:30 +08:00
|
|
|
</span>
|
|
|
|
|
</template>
|
|
|
|
|
</el-dialog>
|
|
|
|
|
</template>
|
|
|
|
|
<script lang="ts" setup>
|
|
|
|
|
import { ref, inject } from 'vue'
|
|
|
|
|
import { reactive } from 'vue'
|
|
|
|
|
import { ElMessage } from 'element-plus'
|
2024-09-13 18:31:35 +08:00
|
|
|
import { libtemplateAdd, updateTemplate } from '@/api/supervision-boot/database/index'
|
|
|
|
|
import { uploadFile, getFileNameAndFilePath } from '@/api/system-boot/file'
|
2024-09-24 16:23:55 +08:00
|
|
|
import { genFileId } from 'element-plus'
|
|
|
|
|
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
|
|
|
|
|
|
|
|
|
|
const upload = ref<UploadInstance>()
|
2024-09-06 15:10:30 +08:00
|
|
|
const dialogVisible = ref(false)
|
|
|
|
|
const title = ref('')
|
2024-09-24 16:23:55 +08:00
|
|
|
const emit = defineEmits(['onSubmit'])
|
2024-09-06 15:10:30 +08:00
|
|
|
const formRef = ref()
|
|
|
|
|
// 注意不要和表单ref的命名冲突
|
|
|
|
|
const form = reactive<anyObj>({
|
|
|
|
|
name: '',
|
2024-09-13 18:31:35 +08:00
|
|
|
url: ''
|
2024-09-06 15:10:30 +08:00
|
|
|
})
|
2024-09-13 18:31:35 +08:00
|
|
|
const urlList: any = ref([])
|
2024-10-17 13:29:51 +08:00
|
|
|
const loading=ref(false)
|
2024-09-13 18:31:35 +08:00
|
|
|
const defaultProps = {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'name',
|
|
|
|
|
value: 'id'
|
|
|
|
|
}
|
2024-09-06 15:10:30 +08:00
|
|
|
const rules = {
|
2024-09-24 16:23:55 +08:00
|
|
|
name: [{ required: true, message: '请输入模版名称', trigger: 'blur' }]
|
2024-09-06 15:10:30 +08:00
|
|
|
}
|
2024-09-13 18:31:35 +08:00
|
|
|
const dataTree: any = ref([])
|
2024-09-06 15:10:30 +08:00
|
|
|
const open = (text: string, data?: anyObj) => {
|
|
|
|
|
title.value = text
|
2024-09-13 18:31:35 +08:00
|
|
|
urlList.value = []
|
2024-09-06 15:10:30 +08:00
|
|
|
dialogVisible.value = true
|
|
|
|
|
if (data) {
|
|
|
|
|
// 表单赋值
|
|
|
|
|
for (let key in form) {
|
|
|
|
|
form[key] = data[key]
|
|
|
|
|
}
|
2024-09-13 18:31:35 +08:00
|
|
|
form.id = data.id
|
|
|
|
|
|
|
|
|
|
if (data.url?.length > 0) {
|
|
|
|
|
getFileNameAndFilePath({ filePath: data.url }).then(res => {
|
|
|
|
|
urlList.value.push({
|
|
|
|
|
name: res.data.fileName,
|
|
|
|
|
url: res.data.name
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-09-06 15:10:30 +08:00
|
|
|
} else {
|
|
|
|
|
// 在此处恢复默认表单
|
|
|
|
|
for (let key in form) {
|
|
|
|
|
form[key] = ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-24 16:23:55 +08:00
|
|
|
const handleExceed: UploadProps['onExceed'] = (files) => {
|
|
|
|
|
upload.value!.clearFiles()
|
|
|
|
|
const file = files[0] as UploadRawFile
|
|
|
|
|
file.uid = genFileId()
|
|
|
|
|
upload.value!.handleStart(file)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 18:31:35 +08:00
|
|
|
|
2024-09-06 15:10:30 +08:00
|
|
|
const submit = () => {
|
2024-10-17 13:29:51 +08:00
|
|
|
loading.value = true
|
2024-09-06 15:10:30 +08:00
|
|
|
formRef.value.validate(async (valid: boolean) => {
|
|
|
|
|
if (valid) {
|
2024-09-13 18:31:35 +08:00
|
|
|
if (urlList.value.length > 0) {
|
|
|
|
|
const promises = urlList.value.map(async (item: any) => {
|
|
|
|
|
if (urlList.value[0].raw) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
uploadFile(item.raw, '/supervision/')
|
|
|
|
|
.then((res: any) => {
|
|
|
|
|
resolve(res.data.name)
|
|
|
|
|
})
|
|
|
|
|
.catch(reject)
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
return item.url
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const fileNames = await Promise.all(promises)
|
|
|
|
|
form.url = fileNames.join(',') + ''
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('上传文件出错', error)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-09-06 15:10:30 +08:00
|
|
|
} else {
|
2024-09-13 18:31:35 +08:00
|
|
|
form.url = ''
|
2024-09-06 15:10:30 +08:00
|
|
|
}
|
2024-09-13 18:31:35 +08:00
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (title.value == '新增模版') {
|
|
|
|
|
libtemplateAdd({
|
|
|
|
|
name: form.name,
|
|
|
|
|
url: form.url
|
|
|
|
|
}).then(res => {
|
|
|
|
|
ElMessage.success('新增成功')
|
|
|
|
|
handleClose()
|
|
|
|
|
dialogVisible.value = false
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
updateTemplate({
|
|
|
|
|
name: form.name,
|
|
|
|
|
url: form.url,
|
|
|
|
|
id: form.id
|
|
|
|
|
}).then(res => {
|
|
|
|
|
ElMessage.success('修改成功')
|
|
|
|
|
handleClose()
|
|
|
|
|
dialogVisible.value = false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}, 100)
|
2024-09-06 15:10:30 +08:00
|
|
|
}
|
|
|
|
|
})
|
2024-10-17 13:29:51 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
loading.value = false
|
|
|
|
|
}, 1000)
|
2024-09-06 15:10:30 +08:00
|
|
|
}
|
2024-09-13 18:31:35 +08:00
|
|
|
const handleClose = () => {
|
|
|
|
|
urlList.value = []
|
|
|
|
|
emit('onSubmit')
|
|
|
|
|
dialogVisible.value = false
|
|
|
|
|
}
|
2024-09-06 15:10:30 +08:00
|
|
|
defineExpose({ open })
|
|
|
|
|
</script>
|
2024-09-13 18:31:35 +08:00
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
:deep(.el-upload-list__item) {
|
|
|
|
|
width: 400px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|