Upload.vue 6.6 KB

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