ImageUpload.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div>
  3. <div v-if="mode === 'DESIGN'">
  4. <div class="design">
  5. <i class="el-icon-plus"></i>
  6. </div>
  7. <p>{{ placeholder }} {{ sizeTip }}</p>
  8. </div>
  9. <div v-else>
  10. <el-upload
  11. :file-list="_value"
  12. action="#"
  13. :limit="maxSize"
  14. with-credentials
  15. :multiple="maxSize > 0"
  16. :data="uploadParams"
  17. list-type="picture-card"
  18. :before-upload="beforeUpload"
  19. >
  20. <i slot="default" class="el-icon-plus"></i>
  21. <div slot="file" slot-scope="{ file }">
  22. <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
  23. <span class="el-upload-list__item-actions">
  24. <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
  25. <i class="el-icon-zoom-in"></i>
  26. </span>
  27. <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
  28. <i class="el-icon-download"></i>
  29. </span>
  30. <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
  31. <i class="el-icon-delete"></i>
  32. </span>
  33. </span>
  34. </div>
  35. <div slot="tip" class="el-upload__tip">{{ placeholder }} {{ sizeTip }}</div>
  36. </el-upload>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. import componentMinxins from '../ComponentMinxins';
  42. import { upLoadFileApi, downLoadFileApi } from '@/api/design';
  43. import { downloadFileBlob } from '@/utils/index';
  44. export default {
  45. mixins: [componentMinxins],
  46. name: 'ImageUpload',
  47. components: {},
  48. props: {
  49. value: {
  50. type: Array,
  51. default: () => {
  52. return [];
  53. },
  54. },
  55. placeholder: {
  56. type: String,
  57. default: '请选择图片',
  58. },
  59. maxSize: {
  60. type: Number,
  61. default: 5,
  62. },
  63. maxNumber: {
  64. type: Number,
  65. default: 10,
  66. },
  67. enableZip: {
  68. type: Boolean,
  69. default: true,
  70. },
  71. },
  72. computed: {
  73. sizeTip() {
  74. return this.maxSize > 0 ? `| 每张图不超过${this.maxSize}MB` : '';
  75. },
  76. },
  77. data() {
  78. return {
  79. disabled: false,
  80. uploadParams: {},
  81. };
  82. },
  83. methods: {
  84. // 覆盖默认的上传行为
  85. requestUpload() {
  86. },
  87. beforeUpload(file) {
  88. const alows = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'];
  89. if (alows.indexOf(file.type) === -1) {
  90. this.$message.warning('存在不支持的图片格式');
  91. } else if (this.maxSize > 0 && file.size / 1024 / 1024 > this.maxSize) {
  92. this.$message.warning(`单张图片最大不超过 ${this.maxSize}MB`);
  93. } else {
  94. //上传文件的需要formdata类型;所以要转
  95. let FormDatas = new FormData();
  96. FormDatas.append('file', file);
  97. upLoadFileApi(FormDatas).then(res => {
  98. console.log('uploadFile', res);
  99. if (res.data.result) {
  100. this._value.push(res.data.result); //成功过后手动将文件添加到展示列表里
  101. console.log(" {{_value}}",this._value)
  102. this.$emit('input', this._value);
  103. }
  104. });
  105. return true;
  106. }
  107. return false;
  108. },
  109. handleRemove(file, fileList) {
  110. console.log(file, fileList);
  111. },
  112. handlePictureCardPreview(file) {
  113. console.log(file);
  114. },
  115. handleDownload(file) {
  116. //上传文件的需要formdata类型;所以要转
  117. let FormDatas = new FormData();
  118. FormDatas.append('name', file.name);
  119. downLoadFileApi(FormDatas).then(res => {
  120. if (res.data) {
  121. downloadFileBlob(res.data,file.name)
  122. }
  123. });
  124. },
  125. },
  126. };
  127. </script>
  128. <style lang="less" scoped>
  129. .design {
  130. i {
  131. padding: 10px;
  132. font-size: xx-large;
  133. background: white;
  134. border: 1px dashed #8c8c8c;
  135. }
  136. }
  137. /deep/ .el-upload--picture-card {
  138. width: 80px;
  139. height: 80px;
  140. line-height: 87px;
  141. }
  142. /deep/ .el-upload-list__item {
  143. width: 80px;
  144. height: 80px;
  145. .el-upload-list__item-actions {
  146. & > span + span {
  147. margin: 1px;
  148. }
  149. }
  150. }
  151. </style>