tm-upload-img.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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" @click="previewHandle(i)"></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. const pictureModule = uni.requireNativePlugin("Wlake-PictureView")
  28. const modal = uni.requireNativePlugin('modal');
  29. export default {
  30. props: {
  31. // 上传的图片路径列表
  32. filePaths: {
  33. type: Array,
  34. default: () => {
  35. return []
  36. }
  37. },
  38. // 是否多选 (默认单选)
  39. isMultiple: {
  40. type: Boolean,
  41. default: false
  42. },
  43. // 是否禁用
  44. disabled: {
  45. type: Boolean,
  46. default: false
  47. },
  48. label: {
  49. type: String,
  50. default: '上传图片'
  51. },
  52. // 下标(多个动态的图片上传组件, 遍历的下标)
  53. pickIndex: {
  54. type: Number,
  55. default: 0
  56. }
  57. },
  58. mounted() {
  59. },
  60. methods: {
  61. previewHandle(index) {
  62. console.log(this.filePaths[0],index);
  63. const picList = this.filePaths;
  64. pictureModule.PictureViewerMain({
  65. 'listPic': picList,
  66. 'position':index, // 0 开始算 最大值为 listPic 数组数量 减一
  67. },
  68. (ret) => {
  69. modal.toast({
  70. message: ret,
  71. duration: 1.5
  72. });
  73. });
  74. },
  75. // 上传图片
  76. uploadPicture() {
  77. if (this.disabled) return;
  78. uni.chooseImage({
  79. count: this.isMultiple ? 9 : 1, // 是否多选
  80. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  81. // sourceType: ['camera'], //从相册选择,默认时可以选择从相机和相册中选取
  82. success: res1 => {
  83. uploadImage(res1).then(fileList => {
  84. if (this.isMultiple) {
  85. this.$emit('changeFilePaths', {
  86. files: [...this.filePaths, ...fileList],
  87. index: this.pickIndex
  88. });
  89. } else {
  90. this.$emit('changeFilePaths', {
  91. files: fileList,
  92. index: this.pickIndex
  93. });
  94. }
  95. })
  96. },
  97. error: e => {
  98. console.log('选择图片失败', e);
  99. }
  100. });
  101. },
  102. // 删除图片
  103. delImg(i) {
  104. const currentIndex = this.pickIndex;
  105. this.$emit('changeFilePaths', {
  106. files: this.filePaths.filter((item, index) => index != i),
  107. index: currentIndex
  108. });
  109. }
  110. }
  111. }
  112. </script>
  113. <style lang="less">
  114. .tm-upload-img {
  115. background-color: #fff;
  116. padding-left: 25rpx;
  117. .row {
  118. position: relative;
  119. display: flex;
  120. justify-content: space-between;
  121. align-items: center;
  122. // flex: 1;
  123. padding-right: 25rpx;
  124. margin-bottom: 30rpx;
  125. .label-view {
  126. width: 175rpx;
  127. line-height: 22.5rpx;
  128. padding: 31.25rpx 0;
  129. text {
  130. font-size: 22.5rpx;
  131. color: #666F80;
  132. }
  133. }
  134. .placeholder {
  135. position: absolute;
  136. top: 31.25rpx;
  137. left: 175rpx;
  138. font-size: 22.5rpx;
  139. color: #B8BECC;
  140. }
  141. .img-icon {
  142. width: 30rpx;
  143. height: 25rpx;
  144. }
  145. }
  146. .img-preview {
  147. display: flex;
  148. flex-wrap: wrap;
  149. flex-direction: row;
  150. justify-content: flex-start;
  151. img {
  152. opacity: 1 !important;
  153. }
  154. .img-item {
  155. position: relative;
  156. margin-bottom: 25rpx;
  157. width: 150rpx;
  158. height: 150rpx;
  159. margin-right:35rpx;
  160. >image {
  161. width:100%;
  162. height:100%;
  163. opacity: 1;
  164. }
  165. .imgItem>img {
  166. opacity: 1;
  167. }
  168. .del-close {
  169. position: absolute;
  170. top: -12.5rpx;
  171. right: -12.5rpx;
  172. width: 37.5rpx;
  173. height: 37.5rpx;
  174. }
  175. &:nth-child(4n) {
  176. margin-right: 0;
  177. }
  178. }
  179. }
  180. }
  181. </style>