upload-image.uvue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <view class="upload-image">
  3. <view class="image-list">
  4. <view v-for="(image, index) in imageUrls" :key="index" class="image-item">
  5. <image class="image" :src="image" mode="aspectFill" @click="handlePreview(index)"></image>
  6. <view class="image-delete" @click="handleDelete(index)">
  7. <image class="delete-icon" src="/static/images/common/3.png" mode="aspectFit"></image>
  8. </view>
  9. </view>
  10. <!-- 上传按钮 -->
  11. <view v-if="imageList.length < maxCount && !uploading" class="upload-box" @click="handleChooseImage">
  12. <text class="upload-icon">+</text>
  13. <text class="upload-text">上传图片</text>
  14. </view>
  15. <!-- 上传中 -->
  16. <view v-if="uploading" class="upload-box uploading">
  17. <text class="upload-text">上传中...</text>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script setup lang="uts">
  23. import { ref, computed } from 'vue'
  24. import { uploadImage } from '../../api/upload/image'
  25. import type { UploadResponse } from '../../types/workbench'
  26. // Props
  27. type Props = {
  28. modelValue?: UploadResponse[]
  29. maxCount?: number
  30. maxSize?: number // MB
  31. businessType?: string
  32. }
  33. const props = withDefaults(defineProps<Props>(), {
  34. modelValue: () => [],
  35. maxCount: 9,
  36. maxSize: 50,
  37. businessType: 'image'
  38. })
  39. // Emits
  40. const emit = defineEmits<{
  41. (e: 'update:modelValue', images: UploadResponse[]): void
  42. (e: 'change', images: UploadResponse[]): void
  43. }>()
  44. // 图片列表
  45. const imageList = ref<UploadResponse[]>(props.modelValue)
  46. const uploading = ref<boolean>(false)
  47. // 计算图片 URL 数组(用于显示和预览)
  48. const imageUrls = computed<string[]>(() => {
  49. const urls: string[] = []
  50. for (let i = 0; i < imageList.value.length; i++) {
  51. urls.push(imageList.value[i].url)
  52. }
  53. return urls
  54. })
  55. // 上传图片
  56. const handleUpload = async (filePaths: string[]): Promise<void> => {
  57. try {
  58. uploading.value = true
  59. for (let i = 0; i < filePaths.length; i++) {
  60. const filePath = filePaths[i]
  61. // 上传图片,获取完整的文件信息
  62. const result = await uploadImage(filePath, props.businessType)
  63. // 存储完整的上传响应对象
  64. imageList.value.push(result)
  65. }
  66. emit('update:modelValue', imageList.value)
  67. emit('change', imageList.value)
  68. uni.showToast({
  69. title: '上传成功',
  70. icon: 'success'
  71. })
  72. } catch (e: any) {
  73. uni.showToast({
  74. title: e.message ?? '上传失败',
  75. icon: 'none'
  76. })
  77. } finally {
  78. uploading.value = false
  79. }
  80. }
  81. // 选择图片
  82. const handleChooseImage = (): void => {
  83. if (imageList.value.length >= props.maxCount) {
  84. uni.showToast({
  85. title: `最多上传${props.maxCount}张图片`,
  86. icon: 'none'
  87. })
  88. return
  89. }
  90. uni.chooseImage({
  91. count: props.maxCount - imageList.value.length,
  92. sizeType: ['compressed'],
  93. sourceType: ['album', 'camera'],
  94. success: (res) => {
  95. const tempFilePaths = res.tempFilePaths as string[]
  96. handleUpload(tempFilePaths)
  97. }
  98. })
  99. }
  100. // 预览图片
  101. const handlePreview = (index: number): void => {
  102. uni.previewImage({
  103. urls: imageUrls.value,
  104. current: index
  105. })
  106. }
  107. // 删除图片
  108. const handleDelete = (index: number): void => {
  109. uni.showModal({
  110. title: '确认删除',
  111. content: '确定要删除这张图片吗?',
  112. success: (res) => {
  113. const confirm = res.confirm as boolean
  114. if (confirm) {
  115. imageList.value.splice(index, 1)
  116. emit('update:modelValue', imageList.value)
  117. emit('change', imageList.value)
  118. }
  119. }
  120. })
  121. }
  122. </script>
  123. <style lang="scss">
  124. .image {
  125. &-list {
  126. flex-direction: row;
  127. flex-wrap: wrap;
  128. }
  129. &-item {
  130. background-color: #f5f7fa;
  131. border-radius: 8rpx;
  132. position: relative;
  133. width: 160rpx;
  134. height: 160rpx;
  135. margin-right: 20rpx;
  136. margin-bottom: 20rpx;
  137. }
  138. width: 160rpx;
  139. height: 160rpx;
  140. border-radius: 8rpx;
  141. &-delete {
  142. position: absolute;
  143. top: 2rpx;
  144. right: 2rpx;
  145. width: 44rpx;
  146. height: 44rpx;
  147. justify-content: center;
  148. align-items: center;
  149. }
  150. }
  151. .delete-icon {
  152. width: 32rpx;
  153. height: 32rpx;
  154. }
  155. .upload {
  156. &-box {
  157. background-color: #f5f7fa;
  158. border-radius: 8rpx;
  159. width: 160rpx;
  160. height: 160rpx;
  161. margin-right: 20rpx;
  162. margin-bottom: 20rpx;
  163. border: 2rpx dashed #d0d0d0;
  164. border-radius: 8rpx;
  165. justify-content: center;
  166. align-items: center;
  167. }
  168. &-icon {
  169. font-size: 60rpx;
  170. color: #999999;
  171. line-height: 60rpx;
  172. margin-bottom: 10rpx;
  173. }
  174. &-text {
  175. font-size: 24rpx;
  176. color: #999999;
  177. }
  178. }
  179. .uploading {
  180. border-style: solid;
  181. border-color: #d0d0d0;
  182. background-color: #f5f7fa;
  183. }
  184. </style>