upload-image.uvue 7.1 KB

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