tm-upload-img.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 :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. label: {
  50. type: String,
  51. default: '上传图片'
  52. },
  53. // 下标
  54. pickIndex: {
  55. type: Number,
  56. default: 0
  57. }
  58. },
  59. methods: {
  60. // 上传图片
  61. uploadPicture() {
  62. if(this.disabled) return;
  63. uni.chooseImage({
  64. count: this.isMultiple ? 9 : 1, // 是否多选
  65. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  66. sourceType: ['album'], //从相册选择
  67. success: res1 => {
  68. console.log(filePathStr)
  69. // res1.tempFilePaths.map(item => {
  70. // })
  71. uni.uploadFile({
  72. // url: `${URL}/eduscreen/upload/picture`,
  73. url: `${URL}/imed/pfm/file/uploadImg`,
  74. fileType: 'image',
  75. filePath: res1.tempFilePaths[0],
  76. name: 'file',
  77. onUploadProgress(progressEvent) {
  78. var percentCompleted = Math.round(
  79. (progressEvent.loaded * 100) / progressEvent.total
  80. );
  81. console.log('进度条:', percentCompleted, progressEvent);
  82. },
  83. success: res2 => {
  84. if (res2.statusCode == 200) {
  85. let data = JSON.parse(res2.data || '{}');
  86. if (parseInt(data.meta.code, 10) === 200) {
  87. const { viewUrl, picture } = data.body;
  88. this.$emit('changeFilePaths', [...this.filePaths, viewUrl], this.pickIndex)
  89. this.picture = picture;
  90. } else {
  91. uni.showToast({
  92. position: 'top',
  93. icon: 'none',
  94. title: '上传图片失败',
  95. duration: 1000
  96. });
  97. }
  98. }
  99. },
  100. error(e) {
  101. uni.showToast({
  102. position: 'top',
  103. icon: 'none',
  104. title: '上传图片失败',
  105. duration: 1000
  106. });
  107. }
  108. });
  109. },
  110. error: e => {
  111. console.log('选择图片失败', e);
  112. }
  113. });
  114. },
  115. // 删除图片
  116. delImg(i) {
  117. if(this.disabled) return;
  118. this.$emit('changeFilePaths', this.filePaths.filter((item, index) => index != i), this.pickIndex);
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="less">
  124. .tm-upload-img {
  125. background-color: #fff;
  126. padding-left: 25rpx;
  127. .row {
  128. position: relative;
  129. display: flex;
  130. justify-content: space-between;
  131. align-items: center;
  132. // flex: 1;
  133. padding-right: 25rpx;
  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. .img-item {
  159. position: relative;
  160. margin-right: 25rpx;
  161. margin-bottom: 25rpx;
  162. width: 300rpx;
  163. height: 225rpx;
  164. >image {
  165. width: 300rpx;
  166. height: 225rpx;
  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. }
  176. }
  177. }
  178. </style>