ImportFile.vue 7.0 KB

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