巴青农资商城

ImageUploadGrid.vue 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="img-grid">
  3. <text v-if="label" class="img-grid__label">{{ label }}</text>
  4. <view class="img-grid__list">
  5. <view v-for="(url, index) in modelValue" :key="index" class="img-grid__item">
  6. <image-preview class="img-grid__pic" :src="url" preview />
  7. <view class="img-grid__del" @click="removeAt(index)">×</view>
  8. </view>
  9. <view
  10. v-if="modelValue.length < max"
  11. class="img-grid__add"
  12. @click="chooseImages"
  13. >
  14. <u-icon name="camera-fill" color="#9a938c" size="36" />
  15. <text class="img-grid__tip">{{ uploading ? '上传中' : '添加图片' }}</text>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script setup>
  21. import { ref } from 'vue'
  22. import { uploadFile } from '@/utils/upload'
  23. const props = defineProps({
  24. modelValue: {
  25. type: Array,
  26. default: () => []
  27. },
  28. label: {
  29. type: String,
  30. default: ''
  31. },
  32. max: {
  33. type: Number,
  34. default: 9
  35. }
  36. })
  37. const emit = defineEmits(['update:modelValue'])
  38. const uploading = ref(false)
  39. function removeAt(index) {
  40. const next = [...props.modelValue]
  41. next.splice(index, 1)
  42. emit('update:modelValue', next)
  43. }
  44. function chooseImages() {
  45. if (uploading.value) return
  46. const left = props.max - props.modelValue.length
  47. if (left <= 0) return
  48. uni.chooseImage({
  49. count: left,
  50. sizeType: ['compressed'],
  51. success: async (res) => {
  52. const paths = res.tempFilePaths || []
  53. if (!paths.length) return
  54. uploading.value = true
  55. const uploaded = [...props.modelValue]
  56. try {
  57. for (const path of paths) {
  58. if (uploaded.length >= props.max) break
  59. const url = await uploadFile(path)
  60. uploaded.push(url)
  61. }
  62. emit('update:modelValue', uploaded)
  63. } catch (e) {
  64. uni.showToast({ title: (e && e.message) || '上传失败', icon: 'none' })
  65. } finally {
  66. uploading.value = false
  67. }
  68. }
  69. })
  70. }
  71. </script>
  72. <style lang="scss" scoped>
  73. .img-grid {
  74. padding: 16rpx 0;
  75. }
  76. .img-grid__label {
  77. display: block;
  78. margin-bottom: 12rpx;
  79. font-size: 28rpx;
  80. color: #333;
  81. }
  82. .img-grid__list {
  83. display: flex;
  84. flex-wrap: wrap;
  85. gap: 16rpx;
  86. }
  87. .img-grid__item,
  88. .img-grid__add {
  89. position: relative;
  90. width: 160rpx;
  91. height: 160rpx;
  92. border-radius: 12rpx;
  93. overflow: hidden;
  94. }
  95. .img-grid__pic {
  96. width: 100%;
  97. height: 100%;
  98. background: #eee;
  99. }
  100. .img-grid__del {
  101. position: absolute;
  102. top: 0;
  103. right: 0;
  104. width: 40rpx;
  105. height: 40rpx;
  106. line-height: 36rpx;
  107. text-align: center;
  108. background: rgba(0, 0, 0, 0.5);
  109. color: #fff;
  110. font-size: 28rpx;
  111. }
  112. .img-grid__add {
  113. display: flex;
  114. flex-direction: column;
  115. align-items: center;
  116. justify-content: center;
  117. gap: 8rpx;
  118. background: #f5f2ef;
  119. border: 1rpx dashed #ddd;
  120. }
  121. .img-grid__tip {
  122. font-size: 22rpx;
  123. color: #9a938c;
  124. }
  125. </style>