upload-image.uvue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. let cameraInput: HTMLInputElement | null = null
  34. const createNativeInput = () => {
  35. if (cameraInput) return cameraInput
  36. // 创建原生 input 元素
  37. cameraInput = document.createElement('input')
  38. cameraInput.type = 'file'
  39. cameraInput.accept = 'image/*'
  40. cameraInput.style.position = 'fixed'
  41. cameraInput.style.top = '-1000px'
  42. cameraInput.style.left = '-1000px'
  43. cameraInput.style.opacity = '0'
  44. cameraInput.style.pointerEvents = 'none'
  45. cameraInput.setAttribute('capture', 'camera')
  46. // 添加到 body
  47. document.body.appendChild(cameraInput)
  48. return cameraInput
  49. }
  50. const props = withDefaults(defineProps<Props>(), {
  51. modelValue: () => [],
  52. maxCount: 9,
  53. maxSize: 50,
  54. businessType: 'image'
  55. })
  56. // Emits
  57. const emit = defineEmits<{
  58. (e: 'update:modelValue', images: UploadResponse[]): void
  59. (e: 'change', images: UploadResponse[]): void
  60. }>()
  61. // 图片列表
  62. const imageList = ref<UploadResponse[]>(props.modelValue)
  63. const uploading = ref<boolean>(false)
  64. // 计算图片 URL 数组(用于显示和预览)
  65. const imageUrls = computed<string[]>(() => {
  66. const urls: string[] = []
  67. for (let i = 0; i < imageList.value.length; i++) {
  68. urls.push(imageList.value[i].url)
  69. }
  70. return urls
  71. })
  72. // 上传图片
  73. const handleUpload = async (filePaths: string[]): Promise<void> => {
  74. try {
  75. uploading.value = true
  76. for (let i = 0; i < filePaths.length; i++) {
  77. const filePath = filePaths[i]
  78. // 上传图片,获取完整的文件信息
  79. const result = await uploadImage(filePath, props.businessType)
  80. // 存储完整的上传响应对象
  81. imageList.value.push(result)
  82. }
  83. emit('update:modelValue', imageList.value)
  84. emit('change', imageList.value)
  85. uni.showToast({
  86. title: '上传成功',
  87. icon: 'success'
  88. })
  89. } catch (e: any) {
  90. uni.showToast({
  91. title: e.message ?? '上传失败',
  92. icon: 'none'
  93. })
  94. } finally {
  95. uploading.value = false
  96. }
  97. }
  98. const openCamera = () => {
  99. const input = createNativeInput()
  100. // 清除事件监听器(避免重复绑定)
  101. input.onchange = null
  102. input.onchange = (event: Event) => {
  103. const target = event.target as HTMLInputElement
  104. if (target.files && target.files.length > 0) {
  105. const file = target.files[0]
  106. handleFile(file)
  107. }
  108. // 清除值以便下次选择
  109. input.value = ''
  110. }
  111. // 触发点击
  112. input.click()
  113. }
  114. const handleFile = (file: File) => {
  115. const blobUrl = URL.createObjectURL(file)
  116. const result = {
  117. tempFilePaths: [blobUrl], // 类似: blob:http://localhost:8080/550e8400-e29b-41d4-a716-446655440000
  118. tempFiles: [{
  119. path: blobUrl,
  120. size: file.size,
  121. name: file.name,
  122. type: file.type,
  123. lastModified: file.lastModified
  124. }]
  125. }
  126. handleUpload(result.tempFilePaths)
  127. }
  128. // 选择图片
  129. const handleChooseImage = (): void => {
  130. // let inputHtml = createNativeInput();
  131. if (imageList.value.length >= props.maxCount) {
  132. uni.showToast({
  133. title: `最多上传${props.maxCount}张图片`,
  134. icon: 'none'
  135. })
  136. return
  137. }
  138. uni.chooseImage({
  139. count: props.maxCount - imageList.value.length,
  140. sizeType: ['compressed'],
  141. sourceType: ['album','camera'],
  142. success: (res) => {
  143. const tempFilePaths = res.tempFilePaths as string[]
  144. handleUpload(tempFilePaths)
  145. }
  146. })
  147. }
  148. // 预览图片
  149. const handlePreview = (index: number): void => {
  150. uni.previewImage({
  151. urls: imageUrls.value,
  152. current: index
  153. })
  154. }
  155. // 删除图片
  156. const handleDelete = (index: number): void => {
  157. emit('delete', index, imageList.value)
  158. }
  159. </script>
  160. <style lang="scss">
  161. .image {
  162. &-list {
  163. display: flex;
  164. flex-direction: row;
  165. flex-wrap: wrap;
  166. }
  167. &-item {
  168. background-color: #f5f7fa;
  169. border-radius: 8rpx;
  170. position: relative;
  171. width: 160rpx;
  172. height: 160rpx;
  173. margin-right: 30rpx;
  174. margin-bottom: 30rpx;
  175. }
  176. width: 100%;
  177. height: 100%;
  178. border-radius: 8rpx;
  179. &-delete {
  180. position: absolute;
  181. top: 2rpx;
  182. right: 2rpx;
  183. width: 44rpx;
  184. height: 44rpx;
  185. display: flex;
  186. justify-content: center;
  187. align-items: center;
  188. }
  189. }
  190. .delete-icon {
  191. width: 32rpx;
  192. height: 32rpx;
  193. }
  194. .upload {
  195. &-box {
  196. background-color: #f5f7fa;
  197. border-radius: 8rpx;
  198. width: 160rpx;
  199. height: 160rpx;
  200. border: 2rpx dashed #d0d0d0;
  201. border-radius: 8rpx;
  202. display: flex;
  203. flex-direction: column;
  204. justify-content: center;
  205. align-items: center;
  206. margin-right: 30rpx;
  207. margin-bottom: 30rpx;
  208. }
  209. &-icon {
  210. font-size: 60rpx;
  211. color: #999999;
  212. line-height: 60rpx;
  213. margin-bottom: 10rpx;
  214. }
  215. &-text {
  216. font-size: 24rpx;
  217. color: #999999;
  218. }
  219. }
  220. .uploading {
  221. border-style: solid;
  222. border-color: #d0d0d0;
  223. background-color: #f5f7fa;
  224. }
  225. </style>