tm-upload-img.vue 3.8 KB

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