tm-upload-img.vue 4.5 KB

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