tm-upload-img.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. this.$emit('changeFilePaths', fileList, this.pickIndex);
  71. })
  72. },
  73. error: e => {
  74. console.log('选择图片失败', e);
  75. }
  76. });
  77. },
  78. // 删除图片
  79. delImg(i) {
  80. this.$emit('changeFilePaths', this.filePaths.filter((item, index) => index != i), this.pickIndex);
  81. }
  82. }
  83. }
  84. </script>
  85. <style lang="less">
  86. .tm-upload-img {
  87. background-color: #fff;
  88. padding-left: 25rpx;
  89. .row {
  90. position: relative;
  91. display: flex;
  92. justify-content: space-between;
  93. align-items: center;
  94. // flex: 1;
  95. padding-right: 25rpx;
  96. .label-view {
  97. width: 175rpx;
  98. line-height: 22.5rpx;
  99. padding: 31.25rpx 0;
  100. text {
  101. font-size: 22.5rpx;
  102. color: #666F80;
  103. }
  104. }
  105. .placeholder {
  106. position: absolute;
  107. top: 31.25rpx;
  108. left: 175rpx;
  109. font-size: 22.5rpx;
  110. color: #B8BECC;
  111. }
  112. .img-icon {
  113. width: 30rpx;
  114. height: 25rpx;
  115. }
  116. }
  117. .img-preview {
  118. display: flex;
  119. flex-wrap: wrap;
  120. .img-item {
  121. position: relative;
  122. margin-right: 25rpx;
  123. margin-bottom: 25rpx;
  124. width: 300rpx;
  125. height: 225rpx;
  126. >image {
  127. width: 300rpx;
  128. height: 225rpx;
  129. }
  130. .del-close {
  131. position: absolute;
  132. top: -12.5rpx;
  133. right: -12.5rpx;
  134. width: 37.5rpx;
  135. height: 37.5rpx;
  136. }
  137. }
  138. }
  139. }
  140. </style>