tm-upload-img.vue 3.6 KB

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