详情小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
3.4 KiB

  1. <template>
  2. <view class="upload-images">
  3. <view class="upload-image-item"
  4. :style="{order: uploadButton == 'front' ? '1' : '2'}"
  5. @click="uploadImage"
  6. v-if="(count == -1) || (image_list.length < count)">
  7. <text class="lf-iconfont lf-icon-jia upload-image-item-after"></text>
  8. </view>
  9. <view class="upload-image-item"
  10. :style="{order: uploadButton == 'front' ? '2' : '1'}"
  11. v-for="(item, index) in image_list" :key="index"
  12. @click="lookImage(index)">
  13. <image :src="item" mode="aspectFill"></image>
  14. <view class="remove-image" @click.stop="removeInage(index)" v-if="showDelete">
  15. <text class="lf-iconfont lf-icon-shanchu"></text>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: {
  23. uploadButton: {
  24. type: String, // 上传按钮显示在哪里,front图片前面,behind图片后面
  25. default: 'front'
  26. },
  27. showDelete: {
  28. type: Boolean, // 是否可删除图片
  29. default: true
  30. },
  31. count: {
  32. type: Number, // 可上传多少张图, 默认6张
  33. default: 6
  34. },
  35. size: {
  36. type: Number, // 限制单张图上传大小,单位M
  37. default: 10
  38. },
  39. drag: {
  40. type: Boolean, // TODO 是否可拖拽排序图片
  41. default: false
  42. }
  43. },
  44. data(){
  45. return {
  46. image_list: []
  47. }
  48. },
  49. onLoad(){
  50. },
  51. methods: {
  52. // 上传凭证图片
  53. uploadImage(){
  54. let current_count = this.$props.count - this.image_list.length;
  55. if(this.$props.count == -1){
  56. current_count = 9;
  57. }
  58. if(current_count == 0) return;
  59. uni.chooseImage({
  60. count: current_count,
  61. complete: result => {
  62. if(result.errMsg == "chooseImage:fail cancel"){
  63. return; // 取消选择图片
  64. }
  65. let tempFiles = result.tempFiles;
  66. let image_list = [];
  67. let overstep = false;
  68. tempFiles.map(item => {
  69. // 上传的图片大小
  70. let size = Math.floor((Number(this.$props.size) || 1) * 1024 * 1024);
  71. if(item.size < size){
  72. image_list.push(item.path);
  73. }else{
  74. overstep = true;
  75. }
  76. })
  77. if(overstep){
  78. uni.showModal({
  79. title: '温馨提示',
  80. content: '您上传的图片含有超出10M大小的限制,请优化大小后再上传!',
  81. showCancel: false
  82. })
  83. }
  84. this.image_list.push(...image_list);
  85. }
  86. })
  87. },
  88. // 预览图片
  89. lookImage(current){
  90. if(this.image_list.length <= 0) return;
  91. this.$u.throttle(() => {
  92. uni.previewImage({
  93. urls: this.image_list,
  94. current: current
  95. });
  96. }, 500);
  97. },
  98. // 移除图片
  99. removeInage(current){
  100. this.image_list.splice(current, 1);
  101. },
  102. // 返回已上传的图片列表
  103. getUploadImage(){
  104. return this.image_list;
  105. }
  106. }
  107. }
  108. </script>
  109. <style lang="scss" scoped="scoped">
  110. .upload-images{
  111. display: flex;
  112. flex-wrap: wrap;
  113. margin-top: 30rpx;
  114. margin-bottom: 18rpx;
  115. .upload-image-item{
  116. width: 220rpx;
  117. height: 220rpx;
  118. background: #DDDDDD;
  119. position: relative;
  120. margin-right: 12rpx;
  121. &:nth-child(3n){
  122. margin-right: 0rpx;
  123. }
  124. &:nth-child(n+4){
  125. margin-top: 12rpx;
  126. }
  127. image{
  128. width: 100%;
  129. height: 100%;
  130. }
  131. .remove-image{
  132. position: absolute;
  133. right: -4rpx;
  134. top: -18rpx;
  135. color: #e74c3c;
  136. font-size: 40rpx;
  137. padding: 8rpx;
  138. }
  139. }
  140. .upload-image-item-after{
  141. position: absolute;
  142. width: 100%;
  143. height: 100%;
  144. display: flex;
  145. justify-content: center;
  146. align-items: center;
  147. font-size: 60rpx;
  148. color: #999999;
  149. }
  150. }
  151. </style>