uploadList.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div class="clickUpload">
  3. <div
  4. class="clickUpText ulrTrue"
  5. :class="!typeVideo(item) && !typeIcon(item) ? 'fileName' : ''"
  6. v-for="(item, index) in uploadArr"
  7. :key="item"
  8. >
  9. <i class="el-icon-delete deletes" @click="delet(item, index)"></i>
  10. <video v-if="typeVideo(item)" :src="item"></video>
  11. <img v-else-if="typeIcon(item)" class="dataImg" :src="item" />
  12. <div v-else>{{ item }}</div>
  13. </div>
  14. <div class="clickUpText" v-if="uploadArr.length < limit">
  15. <div class="addImg" @click="clickFile"><i class="el-icon-plus ashText"></i></div>
  16. </div>
  17. <el-upload
  18. style="display: none"
  19. :headers="token"
  20. ref="upload"
  21. :limit="limit"
  22. :action="uploadUrl"
  23. :on-success="uploadsuccess"
  24. :before-upload="beforeAvatarUpload"
  25. :auto-upload="true"
  26. :name="name"
  27. >
  28. </el-upload>
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'bzUpload',
  34. data() {
  35. return {
  36. token: {
  37. [localStorage.getItem('SC_token') && 'Authorization']: 'Bearer ' + localStorage.getItem('SC_token')
  38. }
  39. };
  40. },
  41. props: {
  42. uploadUrl: {
  43. type: String,
  44. default: '/sc-community/upload/uploadFile'
  45. },
  46. type: {
  47. type: String,
  48. default: 'img,video'
  49. },
  50. className: {
  51. type: Boolean,
  52. default: false
  53. },
  54. name: {
  55. type: String,
  56. default: 'file'
  57. },
  58. uploadArr: {
  59. type: Array,
  60. default: () => []
  61. },
  62. limit: {
  63. type: Number,
  64. default: 3
  65. }
  66. },
  67. methods: {
  68. uploadsuccess(response, file, fileList) {
  69. this.$refs.upload.clearFiles();
  70. if (0 === response.status) {
  71. this.uploadArr.push(response.data);
  72. }
  73. },
  74. beforeAvatarUpload(file) {
  75. let isType = true;
  76. if (this.type == 'video') {
  77. isType = ['video/mp4'].includes(file.type);
  78. if (!isType) {
  79. this.$message.error('请上传mp4视频!');
  80. }
  81. } else if (this.type == 'img') {
  82. isType = ['image/jpeg', 'image/jpg', 'image/png'].includes(file.type);
  83. if (!isType) {
  84. this.$message.error('请上传图片!');
  85. }
  86. }
  87. return isType;
  88. },
  89. typeVideo(str) {
  90. let type = str.slice(str.lastIndexOf('.') + 1, str.length);
  91. let videoType = ['mp4'];
  92. return videoType.includes(type);
  93. },
  94. typeIcon(str) {
  95. let type = str.slice(str.lastIndexOf('.') + 1, str.length);
  96. let imgType = ['png', 'jpeg', 'jpg'];
  97. return imgType.includes(type);
  98. },
  99. clickFile() {
  100. this.$refs['upload'].$children[0].$refs.input.click();
  101. },
  102. delet(item, index) {
  103. this.uploadArr.splice(index, 1);
  104. }
  105. }
  106. };
  107. </script>
  108. <style lang="scss" scoped>
  109. .clickUpload {
  110. display: flex;
  111. .clickUpText {
  112. height: 60px;
  113. width: 80px;
  114. border: 1px solid #e0e1e3;
  115. box-sizing: border-box;
  116. background: white;
  117. border-radius: 5px;
  118. margin-right: 5px;
  119. z-index: 1;
  120. position: relative;
  121. img.dataImg {
  122. width: 100%;
  123. height: 100%;
  124. object-fit: cover;
  125. border-radius: 5px;
  126. }
  127. .addImg {
  128. line-height: 65px;
  129. text-align: center;
  130. cursor: pointer;
  131. & > i {
  132. font-size: 30px;
  133. }
  134. }
  135. video {
  136. width: 100%;
  137. height: 100%;
  138. }
  139. }
  140. .deletes {
  141. display: none;
  142. position: absolute;
  143. top: 50%;
  144. left: 50%;
  145. transform: translate(-50%, -50%);
  146. cursor: pointer;
  147. }
  148. .ulrTrue {
  149. &:hover {
  150. .deletes {
  151. display: block;
  152. }
  153. }
  154. }
  155. .fileName {
  156. height: initial !important;
  157. width: initial !important;
  158. border: none;
  159. }
  160. }
  161. </style>