Upload.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!-- -->
  2. <template>
  3. <div class="upload">
  4. <el-upload
  5. :accept="accept"
  6. style="width: 500px"
  7. class="upload-demo"
  8. :action="url"
  9. :on-preview="handlePreview"
  10. multiple
  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. >
  21. <el-button size="mini" type="primary">
  22. <svg-icon icon-class="upload"></svg-icon>
  23. 上传
  24. </el-button>
  25. <div
  26. slot="tip"
  27. class="el-upload__tip"
  28. >
  29. {{ tips || uploadTip }}
  30. </div>
  31. </el-upload>
  32. <!-- <div class="zhezhao" v-loading.fullscreen.lock="fullscreenLoading"></div> -->
  33. </div>
  34. </template>
  35. <script>
  36. import { getToken } from "../utils/auth";
  37. export default {
  38. components: {},
  39. props: [
  40. "uploadType",
  41. "filleNumber",
  42. "datafileList",
  43. "changeFillId",
  44. "fileName",
  45. "showList",
  46. "tips"
  47. ],
  48. data() {
  49. return {
  50. fileList: [],
  51. accept: "",
  52. fileTypeName: "",
  53. uploadName: "",
  54. uploadTip:"",
  55. loading: false,
  56. };
  57. },
  58. computed: {
  59. url() {
  60. let userInfor = getToken();
  61. let access_token = "";
  62. if (userInfor) {
  63. let user = JSON.parse(getToken());
  64. access_token = user.access_token;
  65. }
  66. return (
  67. process.env.VUE_APP_BASE_API +
  68. "/FileServer/WebFileUpload?AccessToken=" +
  69. access_token +
  70. "&SecurityLevel=High"
  71. );
  72. },
  73. },
  74. watch: {
  75. datafileList: {
  76. handler(val, oldVal) {
  77. this.initUpload();
  78. },
  79. // 深度观察监听
  80. deep: true,
  81. },
  82. },
  83. // 生命周期 - 创建完成(可以访问当前this实例)
  84. created() {},
  85. // 生命周期 - 挂载完成(可以访问DOM元素)
  86. mounted() {
  87. this.initUpload();
  88. },
  89. beforeCreate() {}, // 生命周期 - 创建之前
  90. beforeMount() {}, // 生命周期 - 挂载之前
  91. beforeUpdate() {}, // 生命周期 - 更新之前
  92. updated() {}, // 生命周期 - 更新之后
  93. beforeDestroy() {}, // 生命周期 - 销毁之前
  94. destroyed() {}, // 生命周期 - 销毁完成
  95. activated() {},
  96. // 方法集合
  97. methods: {
  98. handleChange(file, fileList) {},
  99. handleSuccess(response, file, fileList) {
  100. if (response.status == 1) {
  101. response.duration = response.file_info_list[0].media_duration
  102. ? response.file_info_list[0].media_duration
  103. : 10;
  104. this.$message.success("用户上传成功");
  105. if(this.fileName){
  106. this.changeFillId(fileList, this.fileName);
  107. }else{
  108. this.changeFillId(fileList);
  109. }
  110. this.loading.close();
  111. } else {
  112. this.fileList = [];
  113. this.$message.warning(response.msg);
  114. this.loading.close();
  115. }
  116. },
  117. handlebeforeUpload(file) {
  118. if (this.uploadType === 'image') {
  119. if (file.size > 2 * 1024 * 1024) {
  120. this.$message.warning('上传图片大小不能超过2M');
  121. return false; // 必须返回false
  122. }
  123. } else if (this.uploadType === 'mp4') {
  124. if (file.size > 500 * 1024 * 1024) {
  125. this.$message.warning('上传视频大小不能超过500M');
  126. return false; // 必须返回false
  127. }
  128. } else if (file.size > 20 * 1024 * 1024) {
  129. this.$message.warning('上传文件大小不能超过20M');
  130. return false; // 必须返回false
  131. }
  132. this.loading = this.$loading({
  133. lock: true,
  134. text: "Loading",
  135. spinner: "el-icon-loading",
  136. background: "rgba(0, 0, 0, 0.7)",
  137. });
  138. },
  139. handleRemove(file, fileList) {
  140. this.changeFillId(fileList, this.fileName);
  141. this.$message.success("移除成功");
  142. },
  143. beforeRemove(file, fileList) {
  144. return this.$confirm(`确定移除 ${file.name}?`);
  145. },
  146. handlePreview(file) {},
  147. handleExceed(files, fileList) {
  148. this.$message.warning(
  149. `当前限制选择 ${
  150. this.filleNumber ? this.filleNumber : 1
  151. } 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
  152. files.length + fileList.length
  153. } 个文件`
  154. );
  155. },
  156. initUpload() {
  157. this.fileList = this.datafileList || [];
  158. let name = "只能上传";
  159. switch (this.uploadType) {
  160. case "image":
  161. this.accept = ".jpg, .JPG, .jpeg, .JPEG .png, .PNG";
  162. this.fileTypeName = "图片";
  163. this.uploadName = name + "图片";
  164. this.uploadTip = "只能上传.jpg, .jpeg, .png文件,大小不超过2MB"
  165. break;
  166. case "mp3":
  167. this.accept = ".mp3,.MP3,.wav,.WAV";
  168. this.fileTypeName = "音频";
  169. this.uploadName = name + "音频";
  170. this.uploadTip = "只能上传音频文件,大小不超过20MB"
  171. break;
  172. case "mp4":
  173. this.accept = "video/*";
  174. this.fileTypeName = "视频";
  175. this.uploadName = name + "视频";
  176. this.uploadTip = "只能上传视频文件,大小不超 2GB"
  177. break;
  178. case "pdf":
  179. this.accept = ".pdf";
  180. this.fileTypeName = "pdf";
  181. this.uploadName = name + "pdf";
  182. this.uploadTip = "只能上传pdf文件,大小不超过20MB"
  183. break;
  184. case "xls":
  185. this.accept = ".xls,.xlsx";
  186. this.fileTypeName = "表格";
  187. this.uploadName = name + "表格";
  188. this.uploadTip = "只能上传excel文件,大小不超过20MB"
  189. break;
  190. case "lrc":
  191. this.accept = ".lrc";
  192. this.fileTypeName = "lrc";
  193. this.uploadName = name + "lrc";
  194. this.uploadTip = "只能上传lrc文件,大小不超过20MB"
  195. break;
  196. default:
  197. this.accept = "*";
  198. this.fileTypeName = "文件";
  199. this.uploadName = "";
  200. break;
  201. }
  202. },
  203. }, // 如果页面有keep-alive缓存功能,这个函数会触发
  204. };
  205. </script>
  206. <style lang="scss" scoped>
  207. .zhezhao {
  208. position: fixed;
  209. top: 0;
  210. left: 0;
  211. width: 100%;
  212. height: 100vh;
  213. background: rgba(0, 0, 0, 0.4);
  214. z-index: 10000;
  215. }
  216. </style>