multi-uploader.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div class="multi-uploader" :class="fileList && fileList.length >= limit ? 'multi-uploader-limit' : ''">
  3. <el-upload
  4. action="/upload_image"
  5. list-type="picture-card"
  6. :limit="limit"
  7. accept=".jpg,.jpeg,.png,.JPG,.PNG,.JPEG"
  8. ref="multiUploader"
  9. with-credentials
  10. :file-list="fileList"
  11. :before-upload="beforeUpload"
  12. :on-preview="handlePictureCardPreview"
  13. :on-remove="handleRemove"
  14. :on-success="handleSuccess"
  15. >
  16. <i class="el-icon-plus"></i>
  17. </el-upload>
  18. <el-dialog :visible.sync="dialogVisible">
  19. <img width="100%" :src="dialogImageUrl" alt/>
  20. </el-dialog>
  21. <div slot="tip" v-if="showTips" class="el-upload__tip" :class="fileList.length ? 'el-upload__tip-active' : ''">最多添加9张作品图片</div>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. name: "multi-uploader",
  27. props: {
  28. value: {
  29. type: Array
  30. },
  31. showTips: {
  32. type: Boolean,
  33. default: true
  34. },
  35. limit: {
  36. type: Number,
  37. default: 9
  38. }
  39. },
  40. model: {
  41. prop: "value",
  42. event: "change"
  43. },
  44. components: {},
  45. data() {
  46. return {
  47. uploading: false,
  48. fileList: this.value,
  49. dialogImageUrl: "",
  50. dialogVisible: false
  51. };
  52. },
  53. watch: {
  54. value: {//深度监听,可监听到对象、数组的变化
  55. handler(val, oldVal) {
  56. console.log('watch1:', val)
  57. this.fileList = val
  58. },
  59. deep: true //true 深度监听
  60. },
  61. fileList: {//深度监听,可监听到对象、数组的变化
  62. handler(val, oldVal) {
  63. console.log('watch2:', val)
  64. this.$emit("change", val);
  65. },
  66. deep: true //true 深度监听
  67. }
  68. },
  69. computed: {},
  70. mounted() {
  71. this.fileList = this.value;
  72. },
  73. methods: {
  74. beforeUpload(file) {
  75. console.log("beforeUpload", file);
  76. console.log("before", this.$refs.multiUploader);
  77. let fileType = file.type ? file.type : (file.raw ? file.raw.type : "");
  78. if (fileType !== "image/png" && fileType !== "image/jpg" && fileType !== "image/jpeg" && fileType !== "image/gif") {
  79. this.$message.error("请上传png,jp(e)g,gif格式的图片");
  80. return false;
  81. }
  82. if (file.size / 1024 / 1024 > 5) {
  83. this.$message.error("图片大小不可超过5M");
  84. return false;
  85. }
  86. return true;
  87. },
  88. handleRemove(file, fileList) {
  89. console.log(file, fileList);
  90. this.fileList = fileList;
  91. },
  92. handlePictureCardPreview(file) {
  93. this.dialogImageUrl = file.url;
  94. this.dialogVisible = true;
  95. },
  96. handleSuccess(response, file, fileList) {
  97. console.log(response, file, fileList);
  98. if (!response.error && response.filename) {
  99. console.log("上传成功");
  100. const list = [];
  101. fileList.map(fileItem => {
  102. list.push({
  103. name: fileItem.name,
  104. url: fileItem.response ? fileItem.response.filename : fileItem.url
  105. });
  106. });
  107. this.fileList = list;
  108. } else {
  109. this.$refs.multiUploader.abort(file);
  110. const list = [];
  111. fileList.map(tmpFile => {
  112. if (file.uid !== tmpFile.uid) {
  113. list.push({
  114. name: file.name,
  115. url: file.response ? file.response.filename : file.url
  116. });
  117. }
  118. });
  119. this.fileList = list;
  120. console.log("上传失败 abort");
  121. }
  122. },
  123. }
  124. };
  125. </script>
  126. <style lang="scss">
  127. .multi-uploader {
  128. position: relative;
  129. background: #fff;
  130. }
  131. .multi-uploader-limit {
  132. .el-upload--picture-card {
  133. display: none;
  134. }
  135. }
  136. .el-upload__tip {
  137. line-height: 20px;
  138. // margin-top: 10px;
  139. font-size: 14px;
  140. color: #999999;
  141. }
  142. .el-upload__tip-active {
  143. margin-top: -1px;
  144. }
  145. </style>