|
@@ -0,0 +1,164 @@
|
|
|
+<template>
|
|
|
+ <el-upload
|
|
|
+ :action="uploadUrl"
|
|
|
+ :headers="headers"
|
|
|
+ :max-size="maxSize"
|
|
|
+ :format="format"
|
|
|
+ :accept="accept"
|
|
|
+ :data="uploadDataDict"
|
|
|
+ :before-upload="handleBeforeUpload"
|
|
|
+ :on-error="handleError"
|
|
|
+ :on-success="handleSuccess"
|
|
|
+ :http-request="upload"
|
|
|
+ :disabled="disabled"
|
|
|
+ :show-file-list="false"
|
|
|
+ style="display: inline-block; margin: 0 10px"
|
|
|
+ ref="UploadComp"
|
|
|
+ >
|
|
|
+ <el-button
|
|
|
+ :type="btnType"
|
|
|
+ :icon="btnIcon"
|
|
|
+ :loading="loading"
|
|
|
+ :disabled="disabled"
|
|
|
+ >{{ btnContent }}</el-button
|
|
|
+ >
|
|
|
+ </el-upload>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+import { fileMD5 } from "@/plugins/md5";
|
|
|
+import { $httpWithMsg } from "@/plugins/axios";
|
|
|
+
|
|
|
+export default {
|
|
|
+ name: "UploadButton",
|
|
|
+ props: {
|
|
|
+ btnIcon: {
|
|
|
+ type: String,
|
|
|
+ },
|
|
|
+ btnType: {
|
|
|
+ type: String,
|
|
|
+ default: "default",
|
|
|
+ },
|
|
|
+ btnContent: {
|
|
|
+ type: String,
|
|
|
+ },
|
|
|
+ accept: {
|
|
|
+ type: String,
|
|
|
+ },
|
|
|
+ format: {
|
|
|
+ type: Array,
|
|
|
+ default() {
|
|
|
+ return ["xlsx", "xls"];
|
|
|
+ },
|
|
|
+ },
|
|
|
+ uploadUrl: {
|
|
|
+ type: String,
|
|
|
+ required: true,
|
|
|
+ },
|
|
|
+ uploadData: {
|
|
|
+ type: Object,
|
|
|
+ default() {
|
|
|
+ return {};
|
|
|
+ },
|
|
|
+ },
|
|
|
+ maxSize: {
|
|
|
+ type: Number,
|
|
|
+ default: 20 * 1024 * 1024,
|
|
|
+ },
|
|
|
+ addFilenameParam: {
|
|
|
+ type: String,
|
|
|
+ default: "filename",
|
|
|
+ },
|
|
|
+ disabled: { type: Boolean, default: false },
|
|
|
+ },
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ headers: {
|
|
|
+ md5: "",
|
|
|
+ },
|
|
|
+ res: {},
|
|
|
+ loading: false,
|
|
|
+ uploadDataDict: {},
|
|
|
+ };
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ checkFileFormat(fileType) {
|
|
|
+ const _file_format = fileType.split(".").pop().toLocaleLowerCase();
|
|
|
+ return this.format.length
|
|
|
+ ? this.format.some((item) => item.toLocaleLowerCase() === _file_format)
|
|
|
+ : true;
|
|
|
+ },
|
|
|
+ async handleBeforeUpload(file) {
|
|
|
+ this.uploadDataDict = {
|
|
|
+ ...this.uploadData,
|
|
|
+ };
|
|
|
+ this.uploadDataDict[this.addFilenameParam] = file.name;
|
|
|
+
|
|
|
+ if (!this.checkFileFormat(file.name)) {
|
|
|
+ this.handleFormatError();
|
|
|
+ return Promise.reject();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (file.size > this.maxSize) {
|
|
|
+ this.handleExceededSize();
|
|
|
+ return Promise.reject();
|
|
|
+ }
|
|
|
+
|
|
|
+ const md5 = await fileMD5(file);
|
|
|
+ this.headers["md5"] = md5;
|
|
|
+ this.loading = true;
|
|
|
+
|
|
|
+ return true;
|
|
|
+ },
|
|
|
+ upload(options) {
|
|
|
+ if (!options.file) return Promise.reject("文件丢失");
|
|
|
+
|
|
|
+ let formData = new FormData();
|
|
|
+ Object.entries(options.data).forEach(([k, v]) => {
|
|
|
+ formData.append(k, v);
|
|
|
+ });
|
|
|
+ formData.append("file", options.file);
|
|
|
+ this.$emit("uploading");
|
|
|
+
|
|
|
+ return $httpWithMsg.post(options.action, formData, {
|
|
|
+ headers: options.headers,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ handleError(error) {
|
|
|
+ this.loading = false;
|
|
|
+ this.res = {
|
|
|
+ success: false,
|
|
|
+ message: error.response.data.desc,
|
|
|
+ };
|
|
|
+ this.uploadDataDict = {};
|
|
|
+ this.$refs.UploadComp.clearFiles();
|
|
|
+ this.$emit("upload-error", error);
|
|
|
+ },
|
|
|
+ handleSuccess(res) {
|
|
|
+ this.loading = false;
|
|
|
+ this.res = {
|
|
|
+ success: true,
|
|
|
+ message: "导入成功!",
|
|
|
+ };
|
|
|
+ this.$emit("upload-success", res);
|
|
|
+ },
|
|
|
+ handleFormatError() {
|
|
|
+ const content = "只支持文件格式为" + this.format.join("/");
|
|
|
+ this.res = {
|
|
|
+ success: false,
|
|
|
+ message: content,
|
|
|
+ };
|
|
|
+ this.$emit("valid-error", this.res);
|
|
|
+ },
|
|
|
+ handleExceededSize() {
|
|
|
+ const content =
|
|
|
+ "文件大小不能超过" + Math.floor(this.maxSize / (1024 * 1024)) + "M";
|
|
|
+ this.res = {
|
|
|
+ success: false,
|
|
|
+ message: content,
|
|
|
+ };
|
|
|
+ this.$emit("valid-error", this.res);
|
|
|
+ },
|
|
|
+ },
|
|
|
+};
|
|
|
+</script>
|