tm-upload-img.vue 3.4 KB

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