ImportFile.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <div class="import-file">
  3. <div class="tpl-download">
  4. <span>模板下载:</span>
  5. <svg-btn
  6. name="daoru"
  7. color="#6D5FF6"
  8. hoverBgColor="#fff"
  9. @click="toDownload"
  10. >下载</svg-btn
  11. >
  12. </div>
  13. <el-upload
  14. ref="UploadComp"
  15. drag
  16. :action="uploadUrl"
  17. :headers="headers"
  18. :max-size="maxSize"
  19. :accept="accept"
  20. :format="format"
  21. :data="uploadDataDict"
  22. :on-error="handleError"
  23. :on-success="handleSuccess"
  24. :on-change="fileChange"
  25. :http-request="upload"
  26. :disabled="loading"
  27. :auto-upload="false"
  28. :multiple="false"
  29. style="text-align: center"
  30. >
  31. <i class="el-icon-upload"></i>
  32. <div class="el-upload__text">
  33. {{ `点击或将文件拖拽到这里上传(只能上传${format.join("、")}文件)` }}
  34. </div>
  35. <!-- <el-button
  36. slot="trigger"
  37. size="small"
  38. type="primary"
  39. icon="icon icon-search-white"
  40. :disabled="loading"
  41. >
  42. 选择文件
  43. </el-button>
  44. <el-button
  45. size="small"
  46. type="primary"
  47. icon="icon icon-save-white"
  48. :loading="loading"
  49. :disabled="!fileValid"
  50. @click="submitUpload"
  51. >
  52. 确认上传
  53. </el-button>
  54. <el-button
  55. size="small"
  56. type="primary"
  57. icon="icon icon-delete-white"
  58. :disabled="loading"
  59. @click="removeFile"
  60. >
  61. 清空文件
  62. </el-button>
  63. <el-button
  64. v-if="templateUrl"
  65. size="small"
  66. type="primary"
  67. icon="icon icon-export-white"
  68. :loading="downloading"
  69. @click="toDownload"
  70. >
  71. 下载模板
  72. </el-button> -->
  73. </el-upload>
  74. <!-- <p v-if="filename" class="tips-info">{{ filename }}</p> -->
  75. <p v-if="!res.success" class="tips-info tips-error">{{ res.message }}</p>
  76. </div>
  77. </template>
  78. <script>
  79. import { fileMD5 } from "@/plugins/md5";
  80. import { $httpWithMsg } from "@/plugins/axios";
  81. import { downloadByApi } from "@/plugins/download";
  82. export default {
  83. name: "ImportFile",
  84. props: {
  85. format: {
  86. type: Array,
  87. default() {
  88. return ["xlsx", "xls"];
  89. },
  90. },
  91. accept: {
  92. type: String,
  93. default: null,
  94. },
  95. onlyFetchFile: {
  96. type: Boolean,
  97. default: false,
  98. },
  99. uploadUrl: {
  100. type: String,
  101. default: "",
  102. },
  103. uploadData: {
  104. type: Object,
  105. default() {
  106. return {};
  107. },
  108. },
  109. maxSize: {
  110. type: Number,
  111. default: 20 * 1024 * 1024,
  112. },
  113. addFilenameParam: {
  114. type: String,
  115. default: "filename",
  116. },
  117. templateUrl: {
  118. type: String,
  119. default: "",
  120. },
  121. },
  122. data() {
  123. return {
  124. headers: {
  125. md5: "",
  126. },
  127. res: {},
  128. loading: false,
  129. uploadDataDict: {},
  130. filename: "",
  131. fileValid: false,
  132. downloading: false,
  133. };
  134. },
  135. methods: {
  136. initData() {
  137. this.res = {};
  138. this.loading = false;
  139. this.uploadDataDict = {};
  140. this.filename = "";
  141. this.fileValid = false;
  142. },
  143. checkFileFormat(fileType) {
  144. const _file_format = fileType.split(".").pop().toLowerCase();
  145. return this.format.length
  146. ? this.format.some((item) => item.toLowerCase() === _file_format)
  147. : true;
  148. },
  149. fileChange(fileObj, fileList) {
  150. console.log("fileObj", fileObj);
  151. if (fileObj.status === "ready") {
  152. this.handleBeforeUpload(fileObj.raw).catch(() => {});
  153. if (fileList.length > 1) {
  154. fileList[0] = fileList[1];
  155. fileList.splice(1, 1);
  156. }
  157. }
  158. },
  159. async handleBeforeUpload(file) {
  160. this.res = {};
  161. this.filename = file.name;
  162. this.uploadDataDict = {
  163. ...this.uploadData,
  164. };
  165. this.uploadDataDict[this.addFilenameParam] = file.name;
  166. if (!this.checkFileFormat(file.name)) {
  167. this.handleFormatError();
  168. this.fileValid = false;
  169. return Promise.reject();
  170. }
  171. if (file.size > this.maxSize) {
  172. this.handleExceededSize();
  173. this.fileValid = false;
  174. return Promise.reject();
  175. }
  176. const md5 = await fileMD5(file);
  177. this.headers["md5"] = md5;
  178. this.fileValid = true;
  179. if (this.onlyFetchFile) {
  180. this.$emit("file-change", {
  181. file,
  182. md5,
  183. });
  184. }
  185. return true;
  186. },
  187. upload(options) {
  188. if (!options.file) return Promise.reject("文件丢失");
  189. let formData = new FormData();
  190. Object.entries(options.data).forEach(([k, v]) => {
  191. formData.append(k, v);
  192. });
  193. formData.append("file", options.file);
  194. return $httpWithMsg.post(options.action, formData, {
  195. headers: options.headers,
  196. });
  197. },
  198. handleError(error) {
  199. this.loading = false;
  200. this.res = {
  201. success: false,
  202. message: error.response.data.message,
  203. };
  204. this.uploadDataDict = {};
  205. this.filename = "";
  206. this.fileValid = false;
  207. this.$refs.UploadComp.clearFiles();
  208. },
  209. handleSuccess(res) {
  210. this.loading = false;
  211. this.res = {
  212. success: true,
  213. message: "导入成功!",
  214. };
  215. this.cancel();
  216. this.$emit("uploaded", res);
  217. },
  218. handleFormatError() {
  219. const content = "只支持文件格式为" + this.format.join("/");
  220. this.res = {
  221. success: false,
  222. message: content,
  223. };
  224. },
  225. handleExceededSize() {
  226. const content =
  227. "文件大小不能超过" + Math.floor(this.maxSize / (1024 * 1024)) + "M";
  228. this.res = {
  229. success: false,
  230. message: content,
  231. };
  232. },
  233. // action
  234. submitUpload() {
  235. if (this.onlyFetchFile) {
  236. this.$emit("confirm");
  237. return;
  238. }
  239. if (this.loading) return;
  240. this.$refs.UploadComp.submit();
  241. this.loading = true;
  242. },
  243. removeFile() {
  244. if (this.loading) return;
  245. this.uploadDataDict = {};
  246. this.filename = "";
  247. this.fileValid = false;
  248. this.res = {};
  249. this.$refs.UploadComp.clearFiles();
  250. },
  251. async toDownload() {
  252. if (this.downloading) return;
  253. this.downloading = true;
  254. const res = await downloadByApi(() => {
  255. return $httpWithMsg.get(this.templateUrl, {
  256. responseType: "blob",
  257. });
  258. }).catch((e) => {
  259. this.$message.error(e || "下载失败,请重新尝试!");
  260. });
  261. this.downloading = false;
  262. if (!res) return;
  263. this.$message.success("下载成功!");
  264. },
  265. setLoading(loading) {
  266. this.loading = loading;
  267. },
  268. },
  269. };
  270. </script>
  271. <style lang="scss" scoped>
  272. .import-file {
  273. .tpl-download {
  274. display: flex;
  275. align-items: center;
  276. margin-bottom: 15px;
  277. padding-left: 20px;
  278. }
  279. :deep(.el-upload.el-upload--text) {
  280. width: 100%;
  281. .el-upload-dragger {
  282. width: calc(100% - 40px);
  283. margin-left: 20px;
  284. }
  285. }
  286. }
  287. </style>