tm-upload-img.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <view class="tm-upload-img">
  3. <view class="row" @click="uploadPicture">
  4. <view class="label-view">
  5. <text>{{ label }}</text>
  6. </view>
  7. <text class="placeholder" v-show="filePaths.length === 0">点击上传图片</text>
  8. <image class="img-icon" src="/static/img-icon.png"></image>
  9. </view>
  10. <!-- 预览图片 -->
  11. <view class="img-preview">
  12. <view class="img-item" v-for="(src, i) in filePaths" :key="i">
  13. <image :src="src"></image>
  14. <image
  15. class="del-close"
  16. src="/static/del-close.png"
  17. v-if="!disabled"
  18. @click="delImg(i)" />
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. /**
  25. * 图片上传(支持单张图片上传 和 多张图片上传)
  26. * 芦荟
  27. * 2021.2.3
  28. * props:属性说明看tm-radio-gruop.vue
  29. */
  30. import uploadImage from "../../utils/uploadImg.js";
  31. export default {
  32. props: {
  33. // 上传的图片路径列表
  34. filePaths: {
  35. type: Array,
  36. default: () => {
  37. return []
  38. }
  39. },
  40. // 是否多选 (默认单选)
  41. isMultiple: {
  42. type: Boolean,
  43. default: false
  44. },
  45. // 是否禁用
  46. disabled: {
  47. type: Boolean,
  48. default: false
  49. },
  50. label: {
  51. type: String,
  52. default: '上传图片'
  53. },
  54. // 下标(多个动态的图片上传组件, 遍历的下标)
  55. pickIndex: {
  56. type: Number,
  57. default: 0
  58. }
  59. },
  60. methods: {
  61. // 上传图片
  62. uploadPicture() {
  63. if(this.disabled) return;
  64. uni.chooseImage({
  65. count: this.isMultiple ? 9 : 1, // 是否多选
  66. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  67. sourceType: ['album'], //从相册选择
  68. success: res1 => {
  69. uploadImage(res1).then(fileList => {
  70. if(this.isMultiple){
  71. this.$emit('changeFilePaths', [...this.filePaths, ...fileList], this.pickIndex);
  72. }else {
  73. this.$emit('changeFilePaths', fileList, this.pickIndex);
  74. }
  75. })
  76. },
  77. error: e => {
  78. console.log('选择图片失败', e);
  79. }
  80. });
  81. },
  82. // 删除图片
  83. delImg(i) {
  84. this.$emit('changeFilePaths', this.filePaths.filter((item, index) => index != i), this.pickIndex);
  85. }
  86. }
  87. }
  88. </script>
  89. <style lang="less">
  90. .tm-upload-img {
  91. background-color: #fff;
  92. padding-left: 25rpx;
  93. .row {
  94. position: relative;
  95. display: flex;
  96. justify-content: space-between;
  97. align-items: center;
  98. // flex: 1;
  99. padding-right: 25rpx;
  100. .label-view {
  101. width: 175rpx;
  102. line-height: 22.5rpx;
  103. padding: 31.25rpx 0;
  104. text {
  105. font-size: 22.5rpx;
  106. color: #666F80;
  107. }
  108. }
  109. .placeholder {
  110. position: absolute;
  111. top: 31.25rpx;
  112. left: 175rpx;
  113. font-size: 22.5rpx;
  114. color: #B8BECC;
  115. }
  116. .img-icon {
  117. width: 30rpx;
  118. height: 25rpx;
  119. }
  120. }
  121. .img-preview {
  122. display: flex;
  123. flex-wrap: wrap;
  124. .img-item {
  125. position: relative;
  126. margin-right: 25rpx;
  127. margin-bottom: 25rpx;
  128. width: 300rpx;
  129. height: 225rpx;
  130. >image {
  131. width: 300rpx;
  132. height: 225rpx;
  133. }
  134. .del-close {
  135. position: absolute;
  136. top: -12.5rpx;
  137. right: -12.5rpx;
  138. width: 37.5rpx;
  139. height: 37.5rpx;
  140. }
  141. }
  142. }
  143. }
  144. </style>