uploader.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view class="uploader-container flex wrap">
  3. <view class="upload-image-box" v-for="(item, index) in fileList" :key="index" :style="{width: previewSize, height: previewSize}">
  4. <u-image mode="aspectFit" class="img-preview" border-radius="10rpx" :src="item.url" :width="previewSize" :height="previewSize" />
  5. <view class="close-icon flex row-center" @click="deleteImage($event, index)">
  6. <u-icon name="close" size="30" color="white" />
  7. </view>
  8. </view>
  9. <view
  10. class="uplader-upload flex row-center"
  11. :style="{width: previewSize, height: previewSize}"
  12. @click="handleImage"
  13. v-show="(fileList.length == 0 || mutiple) && fileList.length < maxUpload"
  14. v-if="!useSlot"
  15. >
  16. <u-icon size="48" color="#dcdee0" name="camera" />
  17. <view type="image" accept="image/*" class="uploader-input" />
  18. </view>
  19. <view
  20. class="uplader-upload-slot flex row-center"
  21. @click="handleImage"
  22. :style="{width: previewSize, height: previewSize}"
  23. v-show="(fileList.length == 0 || mutiple) && fileList.length < maxUpload"
  24. v-else
  25. >
  26. <slot></slot>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. name: "uploader",
  33. props: {
  34. fileList: {
  35. type: Array,
  36. default: () => []
  37. },
  38. // 默认不允许多选图片
  39. mutiple: {
  40. type: Boolean,
  41. default: false
  42. },
  43. // 限制上传文件数量
  44. maxUpload: {
  45. type: Number,
  46. default: 1
  47. },
  48. previewSize: {
  49. type: String,
  50. default: "160rpx"
  51. },
  52. // 是否可删除
  53. deletable: {
  54. type: Boolean,
  55. default: false,
  56. },
  57. useSlot: {
  58. type: Boolean,
  59. default: false
  60. }
  61. },
  62. data() {
  63. return {
  64. }
  65. },
  66. create() {
  67. },
  68. methods: {
  69. handleImage() {
  70. uni.chooseImage({
  71. count: this.mutiple ? this.maxUpload : 1,
  72. success: (res) => {
  73. this.$emit("after-read", res.tempFiles)
  74. }
  75. })
  76. },
  77. deleteImage(e, index) {
  78. this.$emit('delete', index)
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss">
  84. .uploader-container {
  85. .upload-image-box {
  86. position: relative;
  87. margin-right: 8rpx;
  88. margin-bottom: 8rpx;
  89. .img-preview {
  90. }
  91. .close-icon {
  92. position: absolute;
  93. right: -20rpx;
  94. top: -15rpx;
  95. width: 40rpx;
  96. height: 40rpx;
  97. background-color: red;
  98. border-radius: 50%;
  99. z-index: 20;
  100. }
  101. }
  102. .uplader-upload {
  103. position: relative;
  104. width: 160rpx;
  105. height: 160rpx;
  106. border: 1px dashed $-color-border;
  107. .uploader-input {
  108. position: absolute;
  109. width: 100%;
  110. height: 100%;
  111. overflow: hidden;
  112. opacity: 0;
  113. top: 0;
  114. left: 0;
  115. z-index: 10;
  116. cursor: pointer;
  117. }
  118. }
  119. .uplader-upload-slot {
  120. position: relative;
  121. min-width: 160rpx;
  122. min-height: 160rpx;
  123. .uploader-input {
  124. position: absolute;
  125. width: 100%;
  126. height: 100%;
  127. overflow: hidden;
  128. opacity: 0;
  129. top: 0;
  130. left: 0;
  131. z-index: 10;
  132. cursor: pointer;
  133. }
  134. }
  135. }
  136. </style>