123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <template>
- <div class="import-file">
- <div class="tpl-download">
- <span>模板下载:</span>
- <svg-btn
- name="daoru"
- color="#6D5FF6"
- hoverBgColor="#fff"
- @click="toDownload"
- >下载</svg-btn
- >
- </div>
- <el-upload
- ref="UploadComp"
- drag
- :action="uploadUrl"
- :headers="headers"
- :max-size="maxSize"
- :accept="accept"
- :format="format"
- :data="uploadDataDict"
- :on-error="handleError"
- :on-success="handleSuccess"
- :on-change="fileChange"
- :http-request="upload"
- :disabled="loading"
- :auto-upload="false"
- :multiple="false"
- style="text-align: center"
- >
- <i class="el-icon-upload"></i>
- <div class="el-upload__text">
- {{ `点击或将文件拖拽到这里上传(只能上传${format.join("、")}文件)` }}
- </div>
- <!-- <el-button
- slot="trigger"
- size="small"
- type="primary"
- icon="icon icon-search-white"
- :disabled="loading"
- >
- 选择文件
- </el-button>
- <el-button
- size="small"
- type="primary"
- icon="icon icon-save-white"
- :loading="loading"
- :disabled="!fileValid"
- @click="submitUpload"
- >
- 确认上传
- </el-button>
- <el-button
- size="small"
- type="primary"
- icon="icon icon-delete-white"
- :disabled="loading"
- @click="removeFile"
- >
- 清空文件
- </el-button>
- <el-button
- v-if="templateUrl"
- size="small"
- type="primary"
- icon="icon icon-export-white"
- :loading="downloading"
- @click="toDownload"
- >
- 下载模板
- </el-button> -->
- </el-upload>
- <!-- <p v-if="filename" class="tips-info">{{ filename }}</p> -->
- <p v-if="!res.success" class="tips-info tips-error">{{ res.message }}</p>
- </div>
- </template>
- <script>
- import { fileMD5 } from "@/plugins/md5";
- import { $httpWithMsg } from "@/plugins/axios";
- import { downloadByApi } from "@/plugins/download";
- export default {
- name: "ImportFile",
- props: {
- format: {
- type: Array,
- default() {
- return ["xlsx", "xls"];
- },
- },
- accept: {
- type: String,
- default: null,
- },
- onlyFetchFile: {
- type: Boolean,
- default: false,
- },
- uploadUrl: {
- type: String,
- default: "",
- },
- uploadData: {
- type: Object,
- default() {
- return {};
- },
- },
- maxSize: {
- type: Number,
- default: 20 * 1024 * 1024,
- },
- addFilenameParam: {
- type: String,
- default: "filename",
- },
- templateUrl: {
- type: String,
- default: "",
- },
- },
- data() {
- return {
- headers: {
- md5: "",
- },
- res: {},
- loading: false,
- uploadDataDict: {},
- filename: "",
- fileValid: false,
- downloading: false,
- };
- },
- methods: {
- initData() {
- this.res = {};
- this.loading = false;
- this.uploadDataDict = {};
- this.filename = "";
- this.fileValid = false;
- },
- checkFileFormat(fileType) {
- const _file_format = fileType.split(".").pop().toLowerCase();
- return this.format.length
- ? this.format.some((item) => item.toLowerCase() === _file_format)
- : true;
- },
- fileChange(fileObj, fileList) {
- console.log("fileObj", fileObj);
- if (fileObj.status === "ready") {
- this.handleBeforeUpload(fileObj.raw).catch(() => {});
- if (fileList.length > 1) {
- fileList[0] = fileList[1];
- fileList.splice(1, 1);
- }
- }
- },
- async handleBeforeUpload(file) {
- this.res = {};
- this.filename = file.name;
- this.uploadDataDict = {
- ...this.uploadData,
- };
- this.uploadDataDict[this.addFilenameParam] = file.name;
- if (!this.checkFileFormat(file.name)) {
- this.handleFormatError();
- this.fileValid = false;
- return Promise.reject();
- }
- if (file.size > this.maxSize) {
- this.handleExceededSize();
- this.fileValid = false;
- return Promise.reject();
- }
- const md5 = await fileMD5(file);
- this.headers["md5"] = md5;
- this.fileValid = true;
- if (this.onlyFetchFile) {
- this.$emit("file-change", {
- file,
- md5,
- });
- }
- 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);
- return $httpWithMsg.post(options.action, formData, {
- headers: options.headers,
- });
- },
- handleError(error) {
- this.loading = false;
- this.res = {
- success: false,
- message: error.response.data.message,
- };
- this.uploadDataDict = {};
- this.filename = "";
- this.fileValid = false;
- this.$refs.UploadComp.clearFiles();
- },
- handleSuccess(res) {
- this.loading = false;
- this.res = {
- success: true,
- message: "导入成功!",
- };
- this.cancel();
- this.$emit("uploaded", res);
- },
- handleFormatError() {
- const content = "只支持文件格式为" + this.format.join("/");
- this.res = {
- success: false,
- message: content,
- };
- },
- handleExceededSize() {
- const content =
- "文件大小不能超过" + Math.floor(this.maxSize / (1024 * 1024)) + "M";
- this.res = {
- success: false,
- message: content,
- };
- },
- // action
- submitUpload() {
- if (this.onlyFetchFile) {
- this.$emit("confirm");
- return;
- }
- if (this.loading) return;
- this.$refs.UploadComp.submit();
- this.loading = true;
- },
- removeFile() {
- if (this.loading) return;
- this.uploadDataDict = {};
- this.filename = "";
- this.fileValid = false;
- this.res = {};
- this.$refs.UploadComp.clearFiles();
- },
- async toDownload() {
- if (this.downloading) return;
- this.downloading = true;
- const res = await downloadByApi(() => {
- return $httpWithMsg.get(this.templateUrl, {
- responseType: "blob",
- });
- }).catch((e) => {
- this.$message.error(e || "下载失败,请重新尝试!");
- });
- this.downloading = false;
- if (!res) return;
- this.$message.success("下载成功!");
- },
- setLoading(loading) {
- this.loading = loading;
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .import-file {
- .tpl-download {
- display: flex;
- align-items: center;
- margin-bottom: 15px;
- padding-left: 20px;
- }
- :deep(.el-upload.el-upload--text) {
- width: 100%;
- .el-upload-dragger {
- width: calc(100% - 40px);
- margin-left: 20px;
- }
- }
- }
- </style>
|