<template>
|
<el-dialog
|
:title="type === 'add' ? '新增资料' : '编辑资料'"
|
:visible.sync="dialogVisible"
|
width="40%"
|
@close="handleClose"
|
>
|
<el-form ref="uploadForm" :model="form" :rules="rules" label-width="120px">
|
<el-form-item label="镇(街道)" prop="batchNo">
|
<el-input v-model="form.batchNo" placeholder="请输入" />
|
</el-form-item>
|
<el-form-item label="资料名称" prop="batchNo">
|
<el-input v-model="form.batchNo" placeholder="请输入" />
|
</el-form-item>
|
<el-form-item label="资料文件" prop="file">
|
<el-upload
|
class="upload-demo"
|
drag
|
action="#"
|
:auto-upload="false"
|
:on-change="handleFileChange"
|
:limit="1"
|
>
|
<i class="el-icon-upload"></i>
|
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
|
<div class="el-upload__tip" slot="tip">
|
支持扩展名:.doc .docx .pdf .jpg .png .xlsx .xls
|
</div>
|
</el-upload>
|
</el-form-item>
|
</el-form>
|
<div slot="footer" class="dialog-footer">
|
<el-button type="primary" @click="submitUpload">确 定</el-button>
|
<el-button @click="dialogVisible = false">取 消</el-button>
|
</div>
|
</el-dialog>
|
</template>
|
|
<script>
|
export default {
|
name: 'UploadDialog',
|
props: {
|
visible: {
|
type: Boolean,
|
default: false
|
},
|
type: {
|
type: String,
|
default: 'add',
|
validator: function(value) {
|
return ['add', 'batch'].indexOf(value) !== -1
|
}
|
}
|
},
|
data() {
|
return {
|
dialogVisible: false,
|
form: {
|
batchNo: '',
|
file: null
|
},
|
rules: {
|
batchNo: [
|
{ required: true, message: '请输入批次号', trigger: 'blur' }
|
],
|
file: [
|
{ required: true, message: '请选择上传文件', trigger: 'change' }
|
]
|
}
|
}
|
},
|
watch: {
|
visible(val) {
|
this.dialogVisible = val;
|
},
|
dialogVisible(val) {
|
this.$emit('update:visible', val);
|
}
|
},
|
methods: {
|
handleClose() {
|
this.form.batchNo = '';
|
this.form.file = null;
|
this.$refs.uploadForm && this.$refs.uploadForm.resetFields();
|
},
|
handleFileChange(file) {
|
this.form.file = file.raw;
|
// 手动触发文件字段验证
|
this.$refs.uploadForm.validateField('file');
|
},
|
submitUpload() {
|
this.$refs.uploadForm.validate(valid => {
|
if (!valid) {
|
return;
|
}
|
|
// 这里添加实际的上传逻辑
|
const formData = new FormData();
|
formData.append('file', this.form.file);
|
if (this.type === 'add') {
|
formData.append('batchNo', this.form.batchNo);
|
}
|
|
// 模拟上传
|
this.$message.info('正在上传,请稍候...');
|
setTimeout(() => {
|
this.$emit('success');
|
this.handleClose();
|
}, 1000);
|
});
|
}
|
}
|
}
|
</script>
|
|
<style scoped>
|
|
.el-upload {
|
width: 100%;
|
}
|
.el-upload-dragger {
|
width: 100%;
|
}
|
</style>
|