Upload.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <!-- -->
  2. <template>
  3. <div class="upload">
  4. <el-upload
  5. :accept="accept"
  6. style="width: 500px"
  7. class="upload-demo"
  8. ref="upload"
  9. :action="url"
  10. :on-preview="handlePreview"
  11. :limit="filleNumber ? filleNumber : 1"
  12. :before-upload="handlebeforeUpload"
  13. :on-exceed="handleExceed"
  14. :on-success="handleSuccess"
  15. :on-change="handleChange"
  16. :file-list="fileList"
  17. :before-remove="beforeRemove"
  18. :on-remove="handleRemove"
  19. :show-file-list="showList ? false : true"
  20. :on-progress="uploadVideoProcess"
  21. multiple
  22. :headers="{ 'Content-Type': 'multipart/form-data' }"
  23. >
  24. <template v-if="styleType === 'upload'">
  25. <div class="upload-style">
  26. <i class="el-icon-plus avatar-uploader-icon"></i>
  27. <br />
  28. Upload
  29. </div>
  30. </template>
  31. <template v-else>
  32. <el-button size="mini" type="primary">
  33. <svg-icon icon-class="upload"></svg-icon>
  34. 上传
  35. </el-button>
  36. </template>
  37. <div slot="tip" class="el-upload__tip">
  38. {{ tips || uploadTip }}
  39. </div>
  40. </el-upload>
  41. <!-- <div class="zhezhao" v-loading.fullscreen.lock="fullscreenLoading"></div> -->
  42. <el-progress
  43. style="width: 500px"
  44. v-if="progressFlag"
  45. :percentage="loadProgress"
  46. ></el-progress>
  47. </div>
  48. </template>
  49. <script>
  50. import { getToken } from "../utils/auth";
  51. export default {
  52. components: {},
  53. props: [
  54. "uploadType",
  55. "filleNumber",
  56. "datafileList",
  57. "changeFillId",
  58. "fileName",
  59. "showList",
  60. "tips",
  61. "styleType",
  62. ],
  63. data() {
  64. return {
  65. fileList: [],
  66. accept: "",
  67. fileTypeName: "",
  68. uploadName: "",
  69. uploadTip: "",
  70. loading: false,
  71. loadProgress: 0, // 动态显示进度条
  72. progressFlag: false, // 关闭进度条
  73. };
  74. },
  75. computed: {
  76. url() {
  77. let userInfor = getToken();
  78. let access_token = "";
  79. if (userInfor) {
  80. let user = JSON.parse(getToken());
  81. access_token = user.access_token;
  82. }
  83. return (
  84. process.env.VUE_APP_BASE_API +
  85. "/FileServer/WebFileUpload?AccessToken=" +
  86. access_token +
  87. "&SecurityLevel=High"
  88. );
  89. },
  90. },
  91. watch: {
  92. datafileList: {
  93. handler(val, oldVal) {
  94. this.initUpload();
  95. },
  96. // 深度观察监听
  97. deep: true,
  98. },
  99. },
  100. // 生命周期 - 创建完成(可以访问当前this实例)
  101. created() {},
  102. // 生命周期 - 挂载完成(可以访问DOM元素)
  103. mounted() {
  104. this.initUpload();
  105. },
  106. beforeCreate() {}, // 生命周期 - 创建之前
  107. beforeMount() {}, // 生命周期 - 挂载之前
  108. beforeUpdate() {}, // 生命周期 - 更新之前
  109. updated() {}, // 生命周期 - 更新之后
  110. beforeDestroy() {}, // 生命周期 - 销毁之前
  111. destroyed() {}, // 生命周期 - 销毁完成
  112. activated() {},
  113. // 方法集合
  114. methods: {
  115. handleChange(file, fileList) {},
  116. uploadVideoProcess(event, file, fileList) {
  117. this.progressFlag = true; // 显示进度条
  118. this.loadProgress = parseInt(event.percent); // 动态获取文件上传进度
  119. if (this.loadProgress >= 100) {
  120. this.loadProgress = 100;
  121. setTimeout(() => {
  122. this.progressFlag = false;
  123. }, 1000); // 一秒后关闭进度条
  124. }
  125. },
  126. handleSuccess(response, file, fileList) {
  127. if (response.status == 1) {
  128. response.duration = response.file_info_list[0].media_duration
  129. ? response.file_info_list[0].media_duration
  130. : 10;
  131. if (fileList.every(({ response }) => response?.status)) {
  132. this.$refs.upload.clearFiles();
  133. this.$message.success("用户上传成功");
  134. if (this.fileName) {
  135. this.changeFillId(fileList, this.fileName, file);
  136. } else {
  137. this.changeFillId(fileList, file);
  138. }
  139. }
  140. // this.loading.close();
  141. } else {
  142. this.fileList = [];
  143. this.$message.warning(response.msg);
  144. // this.loading.close();
  145. }
  146. },
  147. handlebeforeUpload(file) {
  148. if (this.uploadType === "image") {
  149. if (file.size > 2 * 1024 * 1024) {
  150. this.$message.warning("上传图片大小不能超过2M");
  151. return false; // 必须返回false
  152. }
  153. } else if (this.uploadType === "mp4") {
  154. if (file.size > 4000 * 1024 * 1024) {
  155. this.$message.warning("上传视频大小不能超过4G");
  156. return false; // 必须返回false
  157. }
  158. } else if (this.uploadType === "mp3") {
  159. if (file.size > 20 * 1024 * 1024) {
  160. this.$message.warning("上传音频大小不能超过20M");
  161. return false; // 必须返回false
  162. }
  163. } else if (this.uploadType === "video&radio") {
  164. if (file.size > 500 * 1024 * 1024) {
  165. this.$message.warning("上传文件大小不能超过500M");
  166. return false; // 必须返回false
  167. }
  168. } else if (file.size > 200 * 1024 * 1024) {
  169. this.$message.warning("上传文件大小不能超过200M");
  170. return false; // 必须返回false
  171. } else if (this.uploadType === "txt") {
  172. let index = file.name.lastIndexOf(".");
  173. let names = file.name;
  174. let type = names.substring(index + 1).toLowerCase();
  175. if (type !== "txt") {
  176. this.$message.warning("请上传txt文件");
  177. return false;
  178. }
  179. }
  180. // this.loading = this.$loading({
  181. // lock: true,
  182. // text: "Loading",
  183. // spinner: "el-icon-loading",
  184. // background: "rgba(0, 0, 0, 0.7)",
  185. // });
  186. },
  187. handleRemove(file, fileList) {
  188. this.changeFillId(fileList, this.fileName);
  189. this.$message.success("移除成功");
  190. },
  191. beforeRemove(file, fileList) {
  192. return this.$confirm(`确定移除 ${file.name}?`);
  193. },
  194. handlePreview(file) {},
  195. handleExceed(files, fileList) {
  196. this.$message.warning(
  197. `当前限制选择 ${
  198. this.filleNumber ? this.filleNumber : 1
  199. } 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
  200. files.length + fileList.length
  201. } 个文件`
  202. );
  203. },
  204. initUpload() {
  205. this.fileList = this.datafileList || [];
  206. let name = "只能上传";
  207. switch (this.uploadType) {
  208. case "image":
  209. this.accept = ".jpg, .JPG, .jpeg, .JPEG .png, .PNG";
  210. this.fileTypeName = "图片";
  211. this.uploadName = name + "图片";
  212. this.uploadTip = "只能上传.jpg, .jpeg, .png文件,大小不超过2MB";
  213. break;
  214. case "mp3":
  215. this.accept = ".mp3,.MP3,.wav,.WAV";
  216. this.fileTypeName = "音频";
  217. this.uploadName = name + "音频";
  218. this.uploadTip = "只能上传音频文件,大小不超过20MB";
  219. break;
  220. case "mp4":
  221. this.accept = ".mp4,.MP4,.mov,.MOV";
  222. this.fileTypeName = "视频";
  223. this.uploadName = name + "视频";
  224. this.uploadTip = "只能上传视频文件,大小不超 4GB";
  225. break;
  226. case "pdf":
  227. this.accept = ".pdf";
  228. this.fileTypeName = "pdf";
  229. this.uploadName = name + "pdf";
  230. this.uploadTip = "只能上传pdf文件,大小不超过20MB";
  231. break;
  232. case "xls":
  233. this.accept = ".xls,.xlsx";
  234. this.fileTypeName = "表格";
  235. this.uploadName = name + "表格";
  236. this.uploadTip = "只能上传excel文件,大小不超过20MB";
  237. break;
  238. case "lrc":
  239. this.accept = ".lrc";
  240. this.fileTypeName = "lrc";
  241. this.uploadName = name + "lrc";
  242. this.uploadTip = "只能上传lrc文件,大小不超过20MB";
  243. break;
  244. case "video&radio":
  245. this.accept = ".mp3,.MP3,.wav,.WAV,.mp4,.MP4,.mov,.MOV";
  246. this.uploadTip = "";
  247. break;
  248. case "txt":
  249. this.accept = ".txt,.TXT";
  250. this.uploadTip = "";
  251. break;
  252. default:
  253. this.accept = "*";
  254. this.fileTypeName = "文件";
  255. this.uploadName = "";
  256. break;
  257. }
  258. },
  259. }, // 如果页面有keep-alive缓存功能,这个函数会触发
  260. };
  261. </script>
  262. <style lang="scss" scoped>
  263. .upload-style {
  264. padding: 8px;
  265. border-radius: 2px;
  266. border: 1px dashed #e5e6eb;
  267. background: #f2f3f5;
  268. font-size: 14px;
  269. font-weight: 500;
  270. line-height: 22px;
  271. }
  272. </style>