tm-upload-img.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. methods: {
  46. // 上传图片
  47. uploadPicture() {
  48. uni.chooseImage({
  49. count: this.isMultiple ? 0 : 1, // 是否多选
  50. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  51. sourceType: ['album'], //从相册选择
  52. success: res1 => {
  53. let filePathStr = res1.tempFilePaths.join(',');
  54. console.log(filePathStr)
  55. uni.uploadFile({
  56. url: `${URL}/eduscreen/upload/picture`,
  57. fileType: 'image',
  58. filePath: filePathStr,
  59. name: 'file',
  60. success: res2 => {
  61. if (res2.statusCode == 200) {
  62. let data = JSON.parse(res2.data || '{}');
  63. if (parseInt(data.meta.code, 10) === 200) {
  64. const { viewUrl, picture } = data.body;
  65. this.$emit('changeFilePaths', [...this.filePaths, viewUrl])
  66. this.picture = picture;
  67. } else {
  68. uni.showToast({
  69. position: 'top',
  70. icon: 'none',
  71. title: '上传图片失败',
  72. duration: 1000
  73. });
  74. }
  75. }
  76. },
  77. error(e) {
  78. uni.showToast({
  79. position: 'top',
  80. icon: 'none',
  81. title: '上传图片失败',
  82. duration: 1000
  83. });
  84. }
  85. });
  86. },
  87. error: e => {
  88. console.log('选择图片失败', e);
  89. }
  90. });
  91. },
  92. // 删除图片
  93. delImg(i) {
  94. this.$emit('changeFilePaths', this.filePaths.filter((item, index) => index != i))
  95. }
  96. }
  97. }
  98. </script>
  99. <style lang="less">
  100. .tm-upload-img {
  101. background-color: #fff;
  102. padding-left: 25rpx;
  103. .row {
  104. position: relative;
  105. display: flex;
  106. justify-content: space-between;
  107. align-items: center;
  108. // flex: 1;
  109. padding-right: 25rpx;
  110. .label-view {
  111. width: 175rpx;
  112. line-height: 22.5rpx;
  113. padding: 31.25rpx 0;
  114. text {
  115. font-size: 22.5rpx;
  116. color: #666F80;
  117. }
  118. }
  119. .placeholder {
  120. position: absolute;
  121. top: 31.25rpx;
  122. left: 175rpx;
  123. font-size: 22.5rpx;
  124. color: #B8BECC;
  125. }
  126. .img-icon {
  127. width: 30rpx;
  128. height: 25rpx;
  129. }
  130. }
  131. .img-preview {
  132. display: flex;
  133. flex-wrap: wrap;
  134. .img-item {
  135. position: relative;
  136. margin-right: 25rpx;
  137. margin-bottom: 25rpx;
  138. width: 300rpx;
  139. height: 225rpx;
  140. >image {
  141. width: 300rpx;
  142. height: 225rpx;
  143. }
  144. .del-close {
  145. position: absolute;
  146. top: -12.5rpx;
  147. right: -12.5rpx;
  148. width: 37.5rpx;
  149. height: 37.5rpx;
  150. }
  151. }
  152. }
  153. }
  154. </style>