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