index.vue 4.28 KB
<!-- 文件导入弹框通用组件 -->
<template>
  <d2-dialog :is-visible="isVisible"
             :title="title"
             @closeDialog="handleClose"
             @confirmDialog="handleConfirm">
    <el-form ref="fileFormRef"
             label-width="120px"
             :model="form"
             :rules="rules">
      <slot name="prepend" />
      <el-form-item prop="fileName"
                    :label="formItemLabel">
        <el-col :span="16">
          <el-input v-model="form.fileName"
                    readonly />
          <slot name="tip" />
        </el-col>
        <el-col class="m-l-md"
                :span="4">
          <el-button class="import-btn">
            导入文件
            <input ref="fileInput"
                   class="import-input"
                   type="file"
                   :accept="accept"
                   @change="onFileChange"
                   @click="$event.target.value = null">
            <el-link class="import-template"
                     icon="el-icon-link"
                     type="primary"
                     @click="$emit('downloadTemplate')">
              {{ templateName }}
            </el-link>
          </el-button>
        </el-col>
      </el-form-item>
    </el-form>
  </d2-dialog>
</template>

<script>
export default {
	name: "D2FileImportDialog",
	props: {
		isVisible: {
			type: Boolean,
			default: false,
		},
		title: {
			type: String,
			default: "批量导入",
		},
		accept: {
			type: String,
			default: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel",
		},
		maxSize: {
			type: Number,
			default: 9,
		},
		templateName: {
			type: String,
			default: "模版下载",
		},
		formItemLabel: {
			type: String,
			default: "导入文件:",
		},
		formData: {
			type: Object,
			default: null,
		},
		formRule: {
			type: Object,
			default: null,
		},
	},
	data() {
		return {
			form: { fileName: "" },
			fileData: null,
			rules: {
				fileName: [{ required: true, message: "请上传文件", trigger: "change" }],
			},
		};
	},
	watch: {
		isVisible(val) {
			if (!val) {
				this.resetForm();
				return;
			}
			this.syncRules();
			this.syncFormData();
		},
		formData: {
			handler(val) {
				this.syncFormData(val, true);
			},
			deep: true,
		},
		formRule: {
			handler() {
				this.syncRules();
			},
			deep: true,
		},
	},
	methods: {
		syncRules() {
			if (this.formRule) {
				Object.assign(this.rules, this.formRule);
			}
		},
		syncFormData(data = this.formData, clearValidate = false) {
			if (!data) {
				return;
			}
			Object.keys(data).forEach((key) => {
				this.$set(this.form, key, data[key]);
			});
			if (clearValidate && this.$refs.fileFormRef) {
				this.$nextTick(() => {
					this.$refs.fileFormRef && this.$refs.fileFormRef.clearValidate(Object.keys(data));
				});
			}
		},
		resetForm() {
			this.$refs.fileFormRef && this.$refs.fileFormRef.resetFields();
			this.fileData = null;
			if (this.$refs.fileInput) {
				this.$refs.fileInput.value = "";
			}
		},
		handleClose() {
			this.resetForm();
			this.$emit("closeDialog");
		},
		handleConfirm() {
			this.syncFormData();
			this.$nextTick(() => {
				this.$refs.fileFormRef.validate((valid) => {
					if (valid && this.fileData) {
						this.$emit("confirmDialog", {
							fileName: this.form.fileName,
							fileData: this.fileData,
						});
					}
				});
			});
		},
		onFileChange(e) {
			if (!e.target.files || !e.target.files[0]) {
				return;
			}
			const file = e.target.files[0];
			const size = file.size / 1024 / 1024;
			e.target.value = "";
			if (size < this.maxSize) {
				this.form.fileName = file.name;
				const reader = new FileReader();
				reader.readAsDataURL(file);
				reader.onload = (event) => {
					const imgFile = event.target.result;
					const arrBase64 = imgFile.split(",");
					const ext = file.name.split(".").pop();
					this.fileData = ext + "@" + arrBase64[1];
				};
			} else {
				this.notice.errorTips(`上传文件不可超过${this.maxSize}M`);
			}
		},
	},
};
</script>

<style scoped>
.m-l-md {
	margin-left: 20px;
}
.import-btn {
	position: relative;
}
.import-input {
	opacity: 0;
	position: absolute;
	right: 0;
	top: 0;
	width: 98px;
	height: 36px;
	cursor: pointer;
	font-size: 0;
}
.import-template {
	position: absolute;
	height: 20px;
	top: 50px;
	left: 0;
}
</style>